我想+
用几种类型的数字测试简单的运算符:int
, float
, double
...
为此,我想使用 NunitTestCaseSource
属性。到目前为止,我发现这样做的唯一方法是:
public class TestAdditionExample
{
public static IEnumerable<TestCaseData> GetTestsCasesInts()
{
yield return new TestCaseData(1, 2, 3);
yield return new TestCaseData(1, -2, -1);
}
public static IEnumerable<TestCaseData> GetTestsCasesFloats()
{
yield return new TestCaseData(1.03f, 2.1f, 3.13f);
yield return new TestCaseData(1.03f, -2.1f, -1.07f);
}
public static IEnumerable<TestCaseData> GetTestsCasesDoubles()
{
yield return new TestCaseData(1.03, 2.1, 3.13);
yield return new TestCaseData(1.03, -2.1, -1.07);
}
[Test, TestCaseSource(nameof(GetTestsCasesInts))]
public void TestAdditionOfInts(int a, int b, int c)
{
Assert.AreEqual(a+b, c);
}
[Test, TestCaseSource(nameof(GetTestsCasesFloats))]
public void TestAdditionOfFloats(float a, float b, float c)
{
Assert.AreEqual(a+b, c);
}
[Test, TestCaseSource(nameof(GetTestsCasesDoubles))]
public void TestAdditionOfDoubles(double a, double b, double c)
{
Assert.AreEqual(a+b, c);
}
}
如您所见,因为参数的类型必须指定为测试函数参数,所以我必须创建三个相同的测试函数(参数类型除外),以及三组TestCaseSource
.
你会想出一个更好、更优雅的解决方案来做到这一点吗?