0

我想+用几种类型的数字测试简单的运算符: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.

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

4

2 回答 2

1

您可以使用动态来完成这项工作:

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);
    }
}
于 2022-02-24T03:01:29.317 回答
0

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

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) {};

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

于 2022-03-01T18:27:38.577 回答