Gabriel Candia
Gabriel Candia

Reputation: 105

How to use TestCaseSource and TestOf attributes in the same test?

I'm trying to define a parameterized test that receives data using the TestCaseSource attribute. I also need to define an attribute to relate each test with a Jira ticket, and as recommended here, I'm using the TestOf attribute as follows:

[TestOf("SomeId")]
[TestCaseSource(typeof(SomeProviderClass), "someMethod")]
public void SampleTest(dynamic myData)
{
    //do something with myData
    //assert something
}

However, when excecuting the test, at TearDown, the size of TestContext.CurrentContext.Test.Properties, that should have the TestOf value, is empty. How should I define the attributes to be both properly recognized by NUnit?

Upvotes: 1

Views: 599

Answers (1)

Chris
Chris

Reputation: 6072

When you use a TestCaseSource, you're actually creating a 'suite' of tests. NUnit test are structured in a tree. Normally, your assembly will be the top root of the tree, and each class within this a branch, which in turn branches to include all the individual test methods. i.e.

                           -Test1
              - TestClassA -Test2
                           -Test3
TestAssembly  
                           -Test4
              - TestClassB -Test5
                           -Test6             

You can see this structure if you take a look at the TestResults.xml.

When you use TestCaseSource you're actually creating another level on the tree. So now your tree looks like this.

                                                -Test1A
                           -TestCaseSourceSuite -Test1B
                                                -Test1C
              - TestClassA -Test2
                           -Test3
TestAssembly  
                           -Test4
              - TestClassB -Test5
                           -Test6             

In your example, TestCaseSourceSuite would be named SampleTest. The reason that what you currently have doesn't work, is that the TestOf attribute is currently applied to TestCaseSourceSuite, rather than Test1A, Test1B Test1C. (Properties like TestOf don't copy up or down the tree hierarchically.)


Anyway. How do we fix it? 😊 Using TestCaseData for your TestCaseSource seems like the best bet. TestOfAttribute doesn't actually get a much use, so there isn't a 'named property' for setting the value, but you can use the SetProperty method and PropertyNames class instead. Try something a little like this...

public class MyTests
{
    [TestCaseSource(typeof(MyDataClass), "TestCases")]
    public void Test(int n, int d)
    {
        Assert.IsTrue(true);
    }
}

public class MyDataClass
{
    public static IEnumerable TestCases
    {
        get
        {
            yield return new TestCaseData(12, 3).SetProperty(PropertyNames.TestOf, "MethodUnderTest");
            yield return new TestCaseData(12, 2).SetProperty(PropertyNames.TestOf, "MethodUnderTest");
        }
    }  
}

Hope that helps!

Upvotes: 2

Related Questions