evg02gsa3
evg02gsa3

Reputation: 591

NUnit's closest equivalent of MbUnit's [DynamicTestFactory] for dynamic tests

I'm looking for NUnit's closest equivalent to MbUnit's [DynamicTestFactory] so I can create dynamtic tests on runtime. Is there an equivalent in NUnit? Thank you.

Upvotes: 1

Views: 167

Answers (1)

hunch_hunch
hunch_hunch

Reputation: 2331

I haven't used MbUnit, but the closest thing to DynamicTestFactory that I know of in NUnit is TestCaseSource.

I found this example of DynamicTestFactory (from here):

[DynamicTestFactory]
public IEnumerable<Test> Should_Create_And_Execute_Dynamic_Tests()
{
   IEnumerable<int> list = new[] {1, 2, 3, 4, 5};
   foreach (int i in list) 
   { 
      yield return new TestCase(string.Format("Test {0}",i), 
         () => { Assert.IsTrue(MyFunction(i)); });
   }
}

This is how you would use NUnit's TestCaseSource (see here) to accomplish the same thing:

    [Test, TestCaseSource("SourceList")]
    public void MyFunctionTest(int i)
    {
        Assert.IsTrue(MyFunction(i));
    }

    private static readonly IEnumerable<int> SourceList = new[] { 1, 2, 3, 4, 5 };

Upvotes: 2

Related Questions