C#:参数化具有多种数字类型的 NUnit 测试

C#: Parametrize an NUnit test with several number types

我想用几种类型的数字测试简单的 + 运算符:intfloatdouble ...

为此,我想使用 Nunit TestCaseSource 属性。 到目前为止,我发现的唯一方法如下:

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

你能想到一个更好、更优雅的解决方案来做到这一点吗?

你可以用动态来完成这个工作:

public class TestAdditionExample
{
    
    public static IEnumerable<dynamic> GetTestsCases()
    {
        yield return new TestCaseData(1, 2, 3);
        yield return new TestCaseData(1, -2, -1);
        yield return new TestCaseData(1.03f, 2.1f, 3.13f);
        yield return new TestCaseData(1.03f, -2.1f, -1.07f);
        yield return new TestCaseData(1.03, 2.1, 3.13);
        yield return new TestCaseData(1.03, -2.1, -1.07);
    }
    
    [Test, TestCaseSource(nameof(GetTestsCases))]
    public void TestAddition(dynamic a, dynamic b, dynamic c) 
    {
        Assert.AreEqual(a+b, c);
    }
}

这是我在不使用动力学的情况下找到的解决方案。 有趣的是,这个解决方案不能应用于我在该线程开头给出的示例,因为 + 不能与 Generics

一起使用
public static IEnumerable<TestCaseData> UintsPortsCases()
{
    foreach (var (key, value) in UintsProperties)
    {
        yield return new TestCaseData(key, value, value + 2).Returns(null);
        yield return new TestCaseData(key, value, value - 2).Returns(null);
    }
}

public static IEnumerable<TestCaseData> FloatsPortsCases()
{
    foreach (var (key, value) in FloatsProperties)
    {
        yield return new TestCaseData(key, value, value + 0.001f).Returns(null);
        yield return new TestCaseData(key, value, value - 0.001f).Returns(null);
    }
}


[UnityTest, TestCaseSource(nameof(UintsPortsCases)), TestCaseSource(nameof(FloatsPortsCases))]
public IEnumerator When_PortValueIsChanged_PointOutput_IsChanged<T>(string portName, T referenceValue, T modifiedValue)
{
    yield return RuntimeTools.WaitForFrameCount(5);
    CheckPropertyChangedCallback(portName, referenceValue, modifiedValue);
}

private void CheckPropertyChangedCallback<T>(string propertyName, T referenceValue, T modifiedValue) {};

仍然需要两个函数来声明测试用例(或每种类型的数字一个函数),但测试本身可以在一个函数中执行。