skip to Main Content

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

2

Answers


  1. This is an interesting one. It looks like the OneTimeSetUp/TearDown are actually run properly, but the Console.Out buffer isn’t consolidated in the right order.

    Consider the following:

    namespace SamplePractice.TestCases;
    
    [SetUpFixture]
    public class OneTime()
    {
      public static string Status = "";
    
      [OneTimeSetUp]
      public void OneTimeSetUp()
      {
        Status = "ONE TIME SETUP ALREADY RUN";
        Console.WriteLine($"{DateTime.Now.TimeOfDay} Global One Time SetUp executing");
      }
    
      [OneTimeTearDown]
      public void OneTimeTearDown()
      {
        Console.WriteLine($"{DateTime.Now.TimeOfDay} Global One Time Tear Down executing");
      }
    }
    

    Notice the Status change when OneTimeSetUp has run. Also the timestamp logging, which will help us later when looking at the output.

    namespace SamplePractice.TestCases;
    public class BaseClass
    {
      [SetUp]
      public void Init() => Console.WriteLine($"{DateTime.Now.TimeOfDay} Setup Executing");
    
      [TearDown]
      public void Cleanup() => Console.WriteLine($"{DateTime.Now.TimeOfDay} Tear Down Executing");
    }
    
    [TestFixture]
    public class Class1 : BaseClass
    {
      [Test]
      public void Class1Test1() => Console.WriteLine(
        $"{DateTime.Now.TimeOfDay} Test 1 executing {OneTime.Status}");
    }
    

    The VS test runner outputs the following:

    17:05:16.0569102 Setup Executing
    17:05:16.0572182 Test 1 executing ONE TIME SETUP ALREADY RUN
    17:05:16.0575411 Tear Down Executing
    

    And nunit-console:

    17:13:21.2253622 Setup Executing
    17:13:21.2257681 Test 1 executing ONE TIME SETUP ALREADY RUN
    17:13:21.2260992 Tear Down Executing
    17:13:21.2200130 Global One Time SetUp executing
    17:13:21.2290333 Global One Time Tear Down executing
    

    We can see that that – although in one case the OneTime outputs were swallowed, and in the other they were out of order – they are clearly executed.

    And the timestamps confirmed that though the OneTime stuff were printed last, they were actually run in the right order.

    Login or Signup to reply.
  2. Console.WriteLine is a write-standard output stream Console.Out, which may be buffered, that is, when multiple outputs are written, they are not rendered immediately, but are saved in the buffer until the buffer is full or the program execution ends.

    Manually call ‘Console.Out.Flush();’ after each ‘Console.WriteLine;’ can force the output buffer to be flushed immediately, reducing the likelihood of out-of-order output:

    Console.WriteLine("Global One Time SetUp executing");
    Console.Out.Flush(); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search