Search code examples
c#.net-4.0nunitvisual-studio-2022nunit-console

SetupFixture methods are not executing in expected order


Description:

The [SetupFixture] class methods [OneTimeSetup] and [OneTimeTearDown] are not executing as expected. Instead of running once before and after all tests in the namespace, they appear to be skipped entirely or executed in the wrong order.

Background Scenario:

I am developing an Automation Test Project where I aim to use [SetupFixture] class methods [OneTimeSetup] and [OneTimeTearDown] to initialize and flush Extent Reports at global level. The goal is to cover multiple [TestFixture] classes within a single report in a test > node format. To understand how these annotations operate, I created this sample setup to test the behaviour of these attributes.

Additionally, I am looking for a way to execute a method before every [TestFixture] starts, similar to how [Setup] works for [Test] methods, but at the fixture level. I haven't added the logic for that in the code, but if someone can suggest a way for it would be really great.

Code Example:

ExtentReportManager.cs

namespace SamplePractice.TestCases
{
    [SetUpFixture]
    public class ExtentReportManager
    {

        public ExtentReportManager() { }

        [OneTimeSetUp]
        public void OneTimeSetUp()
        {
            Console.WriteLine("Global One Time SetUp executing");
        }

        [OneTimeTearDown]
        public void OneTimeTearDown()
        {
            Console.WriteLine("Global One Time Tear Down executing");
        }
    }
}

BaseClass.cs

namespace SamplePractice.TestBase
{
    public class BaseClass
    {
        [SetUp]
        public void Init()
        {
            Console.WriteLine("Setup Executing");
        }

        [TearDown]
        public void Cleanup()
        {
            Console.WriteLine("Tear Down Executing");
        }
    }
}

Testcases:

using SamplePractice.TestBase;

namespace SamplePractice.TestCases
{
    [TestFixture, Order(1)]
    public class Class1 : BaseClass
    {
        [Test, Order(1)]
        public void Class1Test1()
        {
            Console.WriteLine("Class1 Test 1 executing");
        }

        [Test, Order(2)]
        public void Class1Test2()
        {
            Console.WriteLine("Class1 Test 2 executing");
        }
    }
}

using SamplePractice.TestBase;

namespace SamplePractice.TestCases
{
    [TestFixture, Order(2)]
    public class Class2 : BaseClass
    {
        [Test, Order(1)]
        public void Class2Test1()
        {
            Console.WriteLine("Class 2 Test 1 executing");
        }

        [Test, Order(2)]
        public void Class2Test2()
        {
            Console.WriteLine("Class 2 Test 2 executing");
        }
    }
}

Steps I tried:

  1. Ensured ExtentReportManager is a public class with a default constructor.
  2. Placed ExtentReportManager in the assembly namespace to target the entire assembly
  3. Verifies NUnit version (3.14.0) for runner configuration.
  4. Ran tests using both Visual Studio test runner and NUnit Console (nunit3-console).

Output from Visual Studio Test Runner:

Setup Executing
Class1 Test 1 executing
Tear Down Executing

Setup Executing
Class1 Test 2 executing
Tear Down Executing

Setup Executing
Class 2 Test 1 executing
Tear Down Executing

Setup Executing
Class 2 Test 2 executing
Tear Down Executing

Output from NUnit Console:

Setup Executing
Class1 Test 1 executing
Tear Down Executing
Setup Executing
Class1 Test 2 executing
Tear Down Executing
Setup Executing
Class 2 Test 1 executing
Tear Down Executing
Setup Executing
Class 2 Test 2 executing
Tear Down Executing
Global One Time SetUp executing
Global One Time Tear Down executing

Expected Output:

Global One Time SetUp executing
Setup Executing
Class1 Test 1 executing
Tear Down Executing
Setup Executing
Class1 Test 2 executing
Tear Down Executing
Setup Executing
Class 2 Test 1 executing
Tear Down Executing
Setup Executing
Class 2 Test 2 executing
Tear Down Executing
Global One Time Tear Down executing

Questions:

  1. Why is there a difference in the output between the Visual Studio test runner and the NUnit Console? Why are [OneTimeSetup] and [OneTimeTearDown] methods in [SetUpFixture] not executing as expected?
  2. How can I execute a method before every [TestFixture] starts, similar to [SetUp] for [Test] methods, but at the fixture level.

Steps I tried:

  1. Ensured ExtentReportManager is a public class with a default constructor.
  2. Placed ExtentReportManager in the assembly namespace to target the entire assembly
  3. Verifies NUnit version (3.14.0) for runner configuration.
  4. Ran tests using both Visual Studio test runner and NUnit Console (nunit3-console).

Expected Output: Global One Time SetUp executing Setup Executing Class1 Test 1 executing Tear Down Executing Setup Executing Class1 Test 2 executing Tear Down Executing Setup Executing Class 2 Test 1 executing Tear Down Executing Setup Executing Class 2 Test 2 executing Tear Down Executing Global One Time Tear Down executing


Solution

  • Found the solution.

    The reason they were not executing or visible in the output was because the ExtentReportManager class is not part of the NUnit Test runner. So I just replaced the Console.WriteLine() with Console.Error.WriteLine() and it worked.