mardi 19 mars 2019

Build parameterized nunit test with test attribute data

I am trying to create Parameterized tests where the parameters of the tests are constructed as follows:

  1. Load data from a file for all tests
  2. For each test, customize the previews data, by adding or removing data depending on the data or attribute of the test

Problem: How can I access the data from the test attributes when the class InputDataList is called?

I have already tried to find the data in the TestContext inside InputDataList constructor but does not appear there. e.g:

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

Code:

[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] }
        };
    }
}

Thanks,

Aucun commentaire:

Enregistrer un commentaire