ValeriF21
ValeriF21

Reputation: 474

C# ExtentReport shows only last test class (NUnit3)

i've got an issue with ExtentReport, i have few classes with tests and i want to generate a report with all the tests included in it. I have created a BaseTest class with extent report initialization the the test classes has inhertied it and using the static variables to create test, my issue is the BaseTest class test has an [OneTimeTearDown] method in it with extent.Flush() and it called after each of the classes is finished the tests in it and then the result is the last class has overrides the classes before it. Thank you in advance !

Base Class:

[SetUpFixture]
public class BaseClass
{
    public static ExtentReports extent;
    public static ExtentHtmlReporter htmlReporter;
    public static ExtentTest extentTest;
    private string path = ""

[OneTimeSetUp]
public void SetUp()
{
    htmlReporter = new ExtentHtmlReporter(path);

    extent = new ExtentReports();
    extent.AttachReporter(htmlReporter);

    htmlReporter.Config.DocumentTitle = "Tests Report";
    htmlReporter.Config.ReportName = "Issta Tests Report";

    extent.AddSystemInfo("OS", "Windows 10");
    extent.AddSystemInfo("Environment", "QA");
    extent.AddSystemInfo("User Name", "Valeri Rozenberg");
}

[OneTimeTearDown]
public void TearDown()
{
    extent.Flush();
    // Email.sendEmail(path);
}
}

Test class:

namespace AutomationTests
{
    [TestFixture]
    public class AutomaticDeals: BaseClass
    {
        DriverManager driverManager;
        public IWebDriver driver;
        public string url = ""

        [SetUp]
        public void SetUpTests()
        {
            driverManager = 
DriverManagerFactory.GetDriverManager(DriverType.Chrome);
            driver = driverManager.GetWebDriver();
            driver.Url = url;
        }

        [Test]
        public void TestLinks()
        {
            extentTest = extent.CreateTest("TestLinks");
            AutomaticDealsPage aDeals = new AutomaticDealsPage(driver);
            Assert.IsTrue(aDeals.CheckEqualUrls(1));
            extentTest.Log(Status.Pass, "Url's in the automatic deals 
page are equal.");
        }

        [Test]
        public void TestPrices()
        {
            extentTest = extent.CreateTest("TestPrices");
            AutomaticDealsPage aDeals = new AutomaticDealsPage(driver);
            Assert.IsTrue(aDeals.allPricesEqual());
            extentTest.Log(Status.Pass, "Prices in the automatic deals 
page are equal.");
        }
    }
}

Upvotes: 0

Views: 934

Answers (1)

Charlie
Charlie

Reputation: 13726

Simplifying the problem statement:

  1. You have an action (initializing the extent report), which you want to perform before any tests run.
  2. You have another action (flushing the extent report), which you want to perform after all the tests have run.

If these actions are made part of a base class, the code is run repeatedly, either once for each test method if you use '[SetUp]and[TearDown]or once for each test fixture class using[OneTimeSetUp]` and '[OneTimeTearDown]'. So what you want to do can't be accomplished in a base class.

Actually, the first part (initialization) can be done in the base class, using a static flag so that you only initialize the first time. However, there's no way for your code to know that it is being called for the last time, so the second part is impossible.

This kind of situation is what SetUpFixtureAttribute is intended to deal with.

  1. Create a new class marked as a [SetUpFixture]. Place the class either in a top-level namespace, which contains all your tests, or (simpler) outside of any namespace.

  2. Give that class [OneTimeSetUp] and [OneTimeTearDown] methods. Move the actions you want to perform before and after running tests into those methods, respectively.

Defined in a SetUpFixture outside of any namespace, the initialization actions will happen before any tests in the assembly are run and the teardown after all of them have completed.

If the one-time initialization leaves behind any info for your tests to use, save that info in static properties of the class.

Upvotes: 1

Related Questions