Home > Enterprise >  NUnit 3.13.3 does not run OneTimeSetup and OneTimeTeardown in SetupFixture of TestFixture derived cl
NUnit 3.13.3 does not run OneTimeSetup and OneTimeTeardown in SetupFixture of TestFixture derived cl

Time:07-15

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

CodePudding user response:

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.

  • Related