NikS
NikS

Reputation: 149

Is there possibility to get Nunit "Property" attribute value if "Property" attribute is set for TestFixture level

Here is a TestFixture class that I have:

namespace TestApplication.Tests
{
    [TestFixture]
    [Property("type","smoke")]
    public class LoginFixture
    {
        [Test]
        [Property("role","user")]
        public void can_login_with_valid_credentials() 
        {
            Console.WriteLine("Test")
        }
    }
}

As you can see I set Nunit "Property" Attribute for Test and TestFixture levels.

It is easy to get "Property" value from Test level:

var test = TestContext.CurrentContext.Test.Properties["role"]

But don't understand how to get "Property" value from TestFixture level. This doesn't work

TestContext.CurrentContext.Test.Properties["type"]

Upvotes: 5

Views: 6391

Answers (1)

hunch_hunch
hunch_hunch

Reputation: 2331

You need to keep track of the TestFixture context separately, which can be done using a [TestFixtureSetup] method:

[TestFixture]
[Property("type", "smoke")]
public class ContextText
{
    private TestContext _fixtureContext;

    [TestFixtureSetUp]
    public void TestFixtureSetup()
    {
        _fixtureContext = TestContext.CurrentContext;
    }

    [TestCase]
    public void TestFixtureContextTest()
    {
        // This succeeds
        Assert.That(_fixtureContext.Test.Properties["type"], Is.EqualTo("smoke"));
    }

    [TestCase]
    public void TestCaseContextTest()
    {
        // This fails
        Assert.That(TestContext.CurrentContext.Test.Properties["type"], Is.EqualTo("smoke"));
    }
}

The above example is similar to unit tests contained in the NUnit 2.6.3 source code. For the actual NUnit unit tests, see TestContextTests.cs in the downloadable source code.

Upvotes: 4

Related Questions