简体   繁体   中英

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. The base class assembly contains a SetupFixture class that is outside of any namespace. 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. However, the SetupFixture OneTimexxx methods are not being run when I execute a test within the derived class. 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. Since it is only examining the test assembly, it will never look at AssemblyA and therefore never find the fixture.

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. But this only pulls in the information from the base class, not other unreferenced classes like your SetUpFixture.

You should define a global SetUpFixture in each test assembly. It can reference more extensive code in another assembly if you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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