在编写此类测试时,我喜欢使用 NUnit 提供的TestCaseSource属性。我相信这些测试被称为数据驱动测试。
这是我的测试版本的样子:
public class TestClass
{
public enum ABigEnum
{
A,
B,
C,
D
}
public IEnumerable TestValues
{
get
{
yield return new TestCaseData(ABigEnum.A, false).SetName("Assert A");
yield return new TestCaseData(ABigEnum.B, false).SetName("Assert B");
yield return new TestCaseData(ABigEnum.C, false).SetName("Assert C");
yield return new TestCaseData(ABigEnum.D, true).SetName("Assert D");
}
}
[Theory]
[TestCaseSource(typeof(TestClass), "TestValues")]
public void Test(ABigEnum enumValue, bool assertValue)
{
//generate someValue based upon enumValue
Assert.That(enumValue, Is.EqualTo(assertValue), "an amazing assertion message");
}
}
编辑 1
实际上,您可以将Datapoint
属性与Theory
. 像这样的东西:
public enum ABigEnum
{
A,
B,
C,
D
}
[Datapoint]
public Tuple<ABigEnum, bool>[] TestValues = new[]
{
new Tuple<ABigEnum, bool>(ABigEnum.A, false), new Tuple<ABigEnum, bool>(ABigEnum.B, false),
new Tuple<ABigEnum, bool>(ABigEnum.C, false), new Tuple<ABigEnum, bool>(ABigEnum.D, true)
};
[Theory]
public void Test(Tuple<ABigEnum,bool> data)
{
//generate someValue based upon enumValue
Assert.That(data.Item1, Is.EqualTo(data.Item2), "an amazing assertion message");
}