SetUpFixture is not working in .net core 3.1 its not calling OneTimeSetUp

468 Views Asked by At

I am trying to call [OneTimeSetUp], but it's not calling it and directly calling [TestFixture] SetUp. below is my packages that I am using

working fine in .NetFramework v4.X **

<PackageReference Include="NUnit" Version="3.13.1" /> 
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />

**

namespace Test.BaseClass
{

    [SetUpFixture]
    public class SetUpClass
    {
        public static ExtentReports extent;

        [OneTimeSetUp]
        public void RunBeforeAnyTests()
        {
            extent = new ExtentReports();

            string workingDirectory = Directory.GetCurrentDirectory();
            string basedir = Directory.GetParent(workingDirectory).Parent.Parent.FullName;
            string path = AppSettings.Settings[$"Log:FileLocation"];
            string dir = basedir + path;
            var htmlReporter = new ExtentHtmlReporter(dir);
            htmlReporter.Config.DocumentTitle = "Automation Testing Report";
            htmlReporter.Config.ReportName = "Regression Testing";
            htmlReporter.Config.Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Standard;

            extent.AttachReporter(htmlReporter);
            extent.AddSystemInfo("Application Under Test", TestContext.CurrentContext.Test.ClassName);
            extent.AddSystemInfo("Environment", AppSettings.env);
            extent.AddSystemInfo("Machine", Environment.MachineName);
            extent.AddSystemInfo("OS", Environment.OSVersion.VersionString);
        }

        [OneTimeTearDown]
        public void RunAfterAnyTests()
        {
            extent.Flush();
        }
    }
}

This is [SetUp]

namespace Test.BaseClass
{
     
    public class BaseTest
    {
        public static ExtentReports extent;
        public ExtentTest test;
        
        [SetUp]
        public void StartUpTest()
        {
            test = SetUpClass.extent.CreateTest(TestContext.CurrentContext.Test.Name);
            DriverHelper.InitBrowser();
            DriverHelper.BrowserMaximize();
            DriverHelper.DeleteAllCookies();
        }
        [TearDown]
        public void AfterTest()
        {
            try
            {
                var status = TestContext.CurrentContext.Result.Outcome.Status;
                var stacktrace = TestContext.CurrentContext.Result.StackTrace;
                var errorMessage = "<pre>" + TestContext.CurrentContext.Result.Message + "</pre>";
                Status logstatus;
                switch (status)
                {
                    case TestStatus.Failed:
                        logstatus = Status.Fail;
                        DateTime time = DateTime.Now;
                        String fileName = TestContext.CurrentContext.Test.Name;
                         
                        test.Log(Status.Fail, "Fail");
                         
                        break;
                    case TestStatus.Inconclusive:
                        logstatus = Status.Warning;
                        break;
                    case TestStatus.Skipped:
                        logstatus = Status.Skip;
                        break;
                    default:
                        logstatus = Status.Pass;
                        break;
                }
                test.Log(logstatus, "Test ended with " +logstatus + stacktrace);
            }
            catch (Exception e)
            {
                throw (e);
            }
            finally
            {
                DriverHelper.Close();
            }
        }
    }
}

When I run my test, it skipping the [SetUpFixture] and directly calling [TestFixture] I already tried putting both in a separate namespace

1

There are 1 best solutions below

0
On

From NUnit documentation, you need to install Microsoft.NET.Test.Sdk for .Net Core. I tried on demo project it executes OneTimeSetUp and OneTimeTearDown methods. You can examine in the following solution. But there is a bug in logging. You can see the issue from github.

namespace ConsoleApp1
{
    [SetUpFixture]
    public class SetUpClass
    {
        [OneTimeSetUp]
        public void RunBeforeAnyTests()
        {
            TestContext.Progress.WriteLine("One Time SetUp");
        }

        [OneTimeTearDown]
        public void RunAfterAnyTests()
        {
            TestContext.Progress.WriteLine("One Time TearDown");
        }
    }
}
namespace ConsoleApp1
{
    [TestFixture]
    public class BaseTest
    {
        [TearDown]
        public void Teardown()
        {
            Console.WriteLine("teardown");
        }
    }
}
namespace ConsoleApp1
{
    public class DemoTest : BaseTest
    {
        [SetUp]
        public void SetUp()
        {
            Console.WriteLine("setup");
        }

        [Test]
        public void Test()
        {
            Assert.True(true);
        }
    }
}

OUTPUT :

setup
teardown