使用测试属性数据构建参数化的nunit测试

时间:2019-03-19 10:57:42

标签: c# nunit

我正在尝试创建参数化测试,其中测试的参数构造如下:

  1. 从文件中加载所有测试的数据
  2. 对于每个测试,通过根据测试的数据或属性添加或删除数据来定制预览数据

问题:调用类InputDataList时,如何从测试属性访问数据?

我已经尝试在InputDataList构造函数内的TestContext中找到数据,但是没有出现在其中。 例如:

var test = TestContext.CurrentContext.Test; 
//gets Test Name: "AdhocTestMethod" and zero attributes

代码

[TestFixture]
public class MyTestSuite
{
    [Test]
    [MyCustomAtribuite("Data1", "Data2")]
    public void Test1([InputDataList] InputData inputData)
    {
        // The goal is to have the inputData build with the data from 
        // multiple MyCustomAtribuite + File data
        // e.g:
        Assert.Equals(inputData.OtherStuff, "Data1");
        // OR
        Assert.Equals(inputData.OtherStuff, "Data2");
    }
}

public class MyCustomAtribuiteAttribute : Attribute
{
    public string[] myTestData { get; set; }
    public MyCustomAtribuiteAttribute(params string[] mytestData)
    {
        this.myTestData = mytestData;
    }
}

public class InputDataList : ValueSourceAttribute
{
    private static List<InputData> iDataList;

    public InputDataList() : base(typeof(InputDataList), "iDataList")
    {
        List<string> myTestData = new List<string>();
        //How to get the MyCustomAtribuiteAttribute Data here? 
        //e.g: {"Data1", "Data2"}

        //part of this data is to be loaded from a file
        iDataList = new List<InputData> {
            new InputData() { Browser = "Chrome", Module = "MVP_Slide", OtherStuff = myTestData[0] },
            new InputData() { Browser = "IE", Module = "MVP_Slide", OtherStuff = myTestData[1] },
            new InputData() { Browser = "Chrome", Module = "MVP_Slide", OtherStuff = myTestData[1] },
            new InputData() { Browser = "IE", Module = "MVP_Slide", OtherStuff = myTestData[0] }
        };
    }
}

谢谢

0 个答案:

没有答案