How to generate report using extent reports in Page Object Model in C#



  • I have created a POM framework using NUnit in C#. I have created Reporting.cs file which creates reports for the test in that class. How I can generate reports for all tests in different classes without rewriting the reporting code.

    BaseTest
    - References
    - package
    - config
    - projects
    |- project_name
     |- pages
     |- tests
    |- utils
     |- reporting.cs
    

    In tests folder files with TestLoginModule.cs have functions

    [Test]
    public void TestFirst()
    {
        // test code
    }
    [Test]
    public void TestSecond()
    {
        // test code
    }
    

    In tests folder files with TestDashboardModule.cs have functions

    [Test]
    public void TestThird()
    {
        // test code
    }
    [Test]
    public void TestFourth()
    {
        // test code
    }
    

    In Reporting.cs file

    [OneTimeSetUp]
    public void BeforeClass()
    {
        // create a test report directory and attach reporter
        extent = new ExtentReports();
        var dir = AppDomain.CurrentDomain.BaseDirectory.Replace("/bin/Debug","");
                        DirectoryInfo di = Directory.CreateDirectory(dir + "//Test_Execution_Reports");
        var htmlReporter = new ExtentHtmlReporter(dir + "//Test_Execution_Reports" + "//Automation_Report" + ".html");
        extent.AttachReporter(htmlReporter);
    }
    
    [TearDown]
    public void AfterTest()
    {
        var status = TestContext.CurrentContext.Result.Outcome.Status;
        var stacktrace = "" + TestContext.CurrentContext.Result.StackTrace + "";
        var errorMessage = TestContext.CurrentContext.Result.Message;
        Status logstatus;
    
        switch(status)
        {
            case TestStatus.Failed:
                logstatus = Status.Fail;
                test.Log(logstatus, "Test ended with" + logstatus + "-" + errorMessage);
                test.Log(logstatus, "screenshot");
                break;
            case TestStatus.Skipped:
                logstatus = Status.Skip;
                break;
            default:
                logstatus = Status.Pass;
                break;
        }
    
        SB.Driver.Close();
        SB.Driver.Quit();
    }
    
    [OneTimeTearDown]
    public void AfterClass()
    {
        extent.Flush();
    }
    

    I want to call Reporting.cs OneTimeSetUp and TearDown after every Test in different files


  • QA Engineer

    You could likely achieve this through Inheritance, by having all of your test classes inherit from a single class (this would presumably be Reporting).

    To achieve this, something like:

    public class TestDashboardModule : Reporting {
        [Test]
        public void TestThird()
        {
            // test code
        }
        [Test]
        public void TestFourth()
        {
            // test code
        }
    }
    

    One thing worth nothing, it will always call the Teardown in the test class first and then the teardown in the base class. Meaning if you for example kill off selenium in your base class and then use it in your reporting class, it will cause an issue.

    More information can be found here.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2