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

泄露秘密 提交于 2021-02-07 18:30:11

问题


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"]

回答1:


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.



来源:https://stackoverflow.com/questions/25725229/is-there-possibility-to-get-nunit-property-attribute-value-if-property-attri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!