【问题标题】:Is there possibility to get Nunit "Property" attribute value if "Property" attribute is set for TestFixture level如果为 TestFixture 级别设置了“Property”属性,是否有可能获得 Nunit“Property”属性值
【发布时间】:2014-09-08 13:20:19
【问题描述】:

这是我拥有的一个 TestFixture 类:

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 “Property”属性。

从测试级别很容易获得“属性”值:

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

但不明白如何从 TestFixture 级别获取“属性”值。这不起作用

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

【问题讨论】:

    标签: c# testing nunit


    【解决方案1】:

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

    [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 单元测试,请参阅 downloadable source code 中的 TestContextTests.cs。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多