0

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

New contributor
Ravi Pal is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 1
    Hard to answer this without more information... couldn't you just write a helper class that does the report generation regardless of the test class? – Brian 2 days ago
  • @BrianPlease check the question again, I have updated. – Ravi Pal 2 days ago
  • Create and overload for your reporting class that accepts an instance of your test runner classes...? – Brian 2 days ago

Your Answer

Ravi Pal is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.