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.
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.
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");
}
}
}
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
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
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
Steps I tried:
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
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.