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:
- Ensured ExtentReportManager is a public class with a default constructor.
- Placed ExtentReportManager in the assembly namespace to target the entire assembly
- Verifies NUnit version (3.14.0) for runner configuration.
- 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:
- 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?
- How can I execute a method before every [TestFixture] starts, similar to [SetUp] for [Test] methods, but at the fixture level.
Steps I tried:
- Ensured ExtentReportManager is a public class with a default constructor.
- Placed ExtentReportManager in the assembly namespace to target the entire assembly
- Verifies NUnit version (3.14.0) for runner configuration.
- 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
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:
Notice the
Status
change whenOneTimeSetUp
has run. Also the timestamp logging, which will help us later when looking at the output.The VS test runner outputs the following:
And
nunit-console
: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.
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: