简体   繁体   English

NUnit 3.13.3 在 TestFixture 派生类程序集的 SetupFixture 中不运行 OneTimeSetup 和 OneTimeTeardown

[英]NUnit 3.13.3 does not run OneTimeSetup and OneTimeTeardown in SetupFixture of TestFixture derived class assembly

I have a TestFixture class that derives from a base class located in a different assembly than the assembly of the TestFixture class.我有一个 TestFixture 类,它派生自位于与 TestFixture 类的程序集不同的程序集中的基类。 The base class assembly contains a SetupFixture class that is outside of any namespace.基类程序集包含一个位于任何命名空间之外的 SetupFixture 类。 I am expecting the SetupFixture's OneTimeSetup and OneTimeTearDown methods to get run once at the start of the run and end of the run respectively since the derived class inherits from the base class.我期望 SetupFixture 的 OneTimeSetup 和 OneTimeTearDown 方法分别在运行开始和运行结束时运行一次,因为派生类继承自基类。 However, the SetupFixture OneTimexxx methods are not being run when I execute a test within the derived class.但是,当我在派生类中执行测试时,没有运行 SetupFixture OneTimexxx 方法。 Why doesn't this work?为什么这不起作用?

[SetupFixture]
public class SetupFixture   // This is located in AssemblyA
{
    [OneTimeSetUp]
    public void MySetup()
    {
        Console.WriteLine("In Assembly Setup");
    }

    [OneTimeTearDown]
    public void MyTeardown()
    {
        Console.WriteLine("In Assembly Teardown");
    }
}

namespace AssemblyA
{
    [TestFixture]
    public class BaseTestClass    // This is also in AssemblyA
    {
        [Setup]
        public void MySetup()
        {
            Console.WriteLine("In Base Test Setup");
        }
        
        [TearDown]
        public void MyTeardown()
        {
            Console.WriteLine("In Base Test Teardown");
        }
    }
}

namespace AssemblyB
{
    [TestFixture]
    public class MyTestClass : BaseTestClass       // The derived class is in AssemblyB
    {
        [Test]
        public void MyTest()
        {
           Console.WriteLine("In MyTest");
        }
    }
}

The NUnit framework looks for SetUpFixtures in the test assembly itself. NUnit 框架在测试程序集本身中查找 SetUpFixtures。 Since it is only examining the test assembly, it will never look at AssemblyA and therefore never find the fixture.由于它只检查测试程序集,它永远不会查看 AssemblyA,因此永远不会找到夹具。

This is consistent with how other framework features work.这与其他框架功能的工作方式是一致的。 For example, TestFixtures must be found within the test assembly, even though they may inherit from a base class in another referenced assembly.例如,必须在测试程序集中找到 TestFixture,即使它们可能从另一个引用程序集中的基类继承。 But this only pulls in the information from the base class, not other unreferenced classes like your SetUpFixture.但这仅从基类中提取信息,而不是其他未引用的类,例如您的 SetUpFixture。

You should define a global SetUpFixture in each test assembly.您应该在每个测试程序集中定义一个全局 SetUpFixture。 It can reference more extensive code in another assembly if you like.如果您愿意,它可以在另一个程序集中引用更广泛的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM