2

我有一个测试方法和一堆测试用例,如下所示。

[TestCase(1, 2)]
[TestCase(3, 4)]
[TestCase(5, 6)]
public void Smack(int a, int b) { ... }

我在一个地区隐藏了一堆案件,但感觉不对。我已经尝试过官方网页上列出的其他属性,但并没有真正发挥作用(对正确的处理方法有点困惑)。此外,一位同事暗示使用排列等可能会导致问题。

下面是表达一堆测试用例的最佳方式,还是有什么更流畅、更专业的方法?

#region Bunch
[TestCase(1, 2)]
[TestCase(3, 4)]
[TestCase(5, 6)]
#endregion
public void Smack(int a, int b) { ... }
4

1 回答 1

3

您可以尝试使用TestCaseSource

[TestCaseSource("SmackTestCases")]
public void Smack(int a, int b) { ... }

static object[] SmackTestCases =
{
    new object[] { 1, 2 },
    new object[] { 3, 4 },
    new object[] { 5, 6 } 
};

您也可以在单独的类中实现它,如下所示:

[TestFixture]
public class MyTests
{
    [Test]
    [TestCaseSource(typeof(SmackTestDataProvider), "TestCases")]
    public void Smack(int a, int b) { ... }
}

public class SmackTestDataProvider
{
    public static IEnumerable TestCases
    {
        get
        {
            yield return new TestCaseData( 1, 2 );
            yield return new TestCaseData( 3, 4 );
            yield return new TestCaseData( 5, 6 );
        }
    }  
}
于 2014-10-01T15:43:17.077 回答