在类属性中访问NUnit测试名称

时间:2016-02-22 16:55:26

标签: unit-testing nunit

我尝试使用Nunit进行数据驱动测试。

// I'm actually using it like this:
public class Test
{
    [Test, TestCaseSource(typeof(Test), "TestCaseDataList")]
    public void Test_Sum(string paramA, string paramB, string result)
    {
        int pa = Convert.ToInt32(paramA);
        int pb = Convert.ToInt32(paramB);
        Soma soma = new Soma();
        Assert.AreEqual(result, soma.Sum(pa, pb).ToString());
    }
}

public IEnumerable TestCaseDataList
{
    get
    {
        List<TestCaseData> testCaseDataList = new TestDataReader().ReadExcelData(@"C:\Data\TestData.xls", @"Plan1");

        foreach (TestCaseData testCaseData in testCaseDataList)
        {
            yield return testCaseData;
        }
    }
}

// But i want to use like this:
public class Test
{
    [Test, TesteCaseAttribute(@"C:\Data\TestData.xls", @"Plan1")]
    public void Test_Sum(string paramA, string paramB, string result)
    {
        int pa = Convert.ToInt32(paramA);
        int pb = Convert.ToInt32(paramB);
        Soma soma = new Soma();
        Assert.AreEqual(result, soma.Sum(pa, pb).ToString());
    }
}

[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class TesteCaseAttribute : System.Attribute
{
    private string plan;
    private string ab;

    public TesteCaseAttribute(String plan, String ab)
    {
        this.plan = plan;
        this.ab = ab;
    }
}

public IEnumerable TestCaseDataList
{
    get
    {
        List<TestCaseData> testCaseDataList = new TestDataReader().ReadExcelData(this.plan, this.ab);

        foreach (TestCaseData testCaseData in testCaseDataList)
        {
            yield return testCaseData;
        }
    }
}

如何让TestCaseAttribute构造函数接收TestCaseDataList的返回值?

0 个答案:

没有答案