【问题标题】:NUnit global setup with different namespaces具有不同命名空间的 NUnit 全局设置
【发布时间】:2020-10-25 14:12:06
【问题描述】:

是否可以为整个程序集使用 NUnit OneTimeSetUp?

在我的项目中,我有一个位于命名空间 MyProject.Tests.Basics 中的 BaseTest 类,并且我有一个包含 [OneTimeSetUp] 方法的安装程序类。 SetUp 类位于命名空间MyProject.Tests

当我运行派生自 BaseTest 类并且位于命名空间 MyProject.Tests.Data 中的测试时,不会调用 OneTimeSetUp 方法。但是当我将我的 SetUp 类的命名空间更改为测试类的命名空间时,该方法被调用就好了,但当然只针对这个特定的测试类。

所以我的问题是,如何实现适用于整个程序集的 SetUp 类?

这是我的代码示例:

设置类:

namespace MyProject.Tests
{
    [SetUpFixture]
    public class SetUp
    {
        [OneTimeSetUp]
        public void Setup()
        {
            ...
        }
    }
}

基础测试:

namespace MyProject.Tests.Basics
{
    [TestFixture]
    public abstract class BaseTest
    {
        public BaseTest()
        {
            ...
        }
    }
}

测试类:

namespace MyProject.Tests.Data.IO.Files.DataFiles
{
    public class ExampleTest: BaseTest
    {
        [Test]
        public void Example()
        {
            ...
        }
    }
}

【问题讨论】:

    标签: c# unit-testing nunit


    【解决方案1】:

    为此,请将您的 SetUpFixture 类放在全局命名空间中,即不要将它放在任何命名空间中。

    这在 NUnit 文档中记录如下:

    任何命名空间之外的 SetUpFixture 为整个程序集提供 SetUp 和 TearDown。

    https://docs.nunit.org/articles/nunit/writing-tests/attributes/setupfixture.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      相关资源
      最近更新 更多