是否有可能获得 Nunit 的“财产"?属性值如果“属性"为 TestFixture 级别设置了属性 [英] Is there possibility to get Nunit "Property" attribute value if "Property" attribute is set for TestFixture level

查看:50
本文介绍了是否有可能获得 Nunit 的“财产"?属性值如果“属性"为 TestFixture 级别设置了属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我拥有的 TestFixture 类:

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")
        }
    }
}

如您所见,我为 Test 和 TestFixture 级别设置了 Nunit属性"属性.

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

但不明白如何从 TestFixture 级别获取属性"值.这不起作用

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

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

推荐答案

您需要单独跟踪 TestFixture 上下文,这可以使用 [TestFixtureSetup] 方法完成:

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"));
    }
}

以上示例类似于 NUnit 2.6.3 源代码中包含的单元测试.对于实际的 NUnit 单元测试,请参阅可下载的源代码中的 TestContextTests.cs.

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.

这篇关于是否有可能获得 Nunit 的“财产"?属性值如果“属性"为 TestFixture 级别设置了属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆