2

I have multiple test classes that implement an empty interface (ITest, expanded with IEnumerable).

Interface:

public interface ITest : IEnumerable
{
}

TestDataSource with a description:

[Description("TestSourceName")]
public class MyTests : ITest
{    
    public IEnumerator GetEnumerator()
    {
        yield return "123";
        yield return "456";
    }
}

TestDataProvider class that will be used as TestDataSource in NUnit, I'm using reflection to get the right TestDataSource object:

public class TestDataProvider : IEnumerable
{
    public string Name { get; set; }
    public IEnumerator GetEnumerator()
    {

        if (string.IsNullOrEmpty(Name))
            yield break;

        var testdata = System.Reflection.Assembly.GetExecutingAssembly()
                .GetTypes()
                .Where(mytype => mytype.GetInterfaces().Contains(typeof(ITest)))
                .Where(mytype => ((DescriptionAttribute)mytype.GetCustomAttributes(typeof(DescriptionAttribute), false)[0])
                .Description.ToLower() == Name.ToLower());

        yield return ((IEnumerable)testdata).GetEnumerator();

    }

        public IEnumerator GetEnumerator(string name)
        {
            Name = name;
            return GetEnumerator();
        }
    }

Now I wonder how I can pass TestSourceName to the TestDataSource for NUnit?

[TestCaseSource(typeof(TestDataProvider)), TestCaseSourceAttribute("TestSourceName")]
public void TestTestDataProvider()
{
    // here we will have the TestDataSource object given by TestDataProvider 
    // using parameter/attribute? "TestSourceName"
}
Arialdo Martini
  • 4,427
  • 3
  • 31
  • 42
grmbl
  • 2,514
  • 4
  • 29
  • 54
  • 1
    See if this can be of any help: [http://stackoverflow.com/a/30779861/47458][1] [1]: http://stackoverflow.com/a/30779861/47458 – Andrea Celin Jun 11 '15 at 11:49

1 Answers1

1

One approach you could take is to use NUnit TheoryAttribute. Theories are still part of the upcoming 3.0 release as well.

Roughly based on the examples you gave, here is an example of a working Theory based test which uses the data from the DataPointsAttribute as parameters to the test method.

[Description("TestSource2")]
public class TestData1 : ITest
{
    public IEnumerator GetEnumerator()
    {
        yield return "123";
        yield return "456";
    }
}

[Description("TestSource1")]
public class TestData2 : ITest
{
    public IEnumerator GetEnumerator()
    {
        yield return 33;
        yield return 44;
    }
}

public class TestDataProviderFixture
{
    [Datapoints] 
    public string[] SourceNames = {"TestSource1", "TestSource2"};

    [Theory]
    public void TestTestDataProvider(string sourceName)
    {
        var provider = new TestDataProvider();
        var data = provider.GetEnumerator(sourceName);

        if (sourceName == "TestSource1")
        {
            while (data.MoveNext())
            {
                //Do Stuff
            }
        }
        else if (sourceName == "TestSource2")
        {
            while (data.MoveNext())
            {
            //Do Stuff
            }
        }
        else
        {
            Assert.Fail();
        }
    }
}
BgRva
  • 1,521
  • 12
  • 26