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

  • 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 Dec 2 '19 at 16:16
  • @BrianPlease check the question again, I have updated. – Ravi Pal Dec 3 '19 at 5:14
  • Create and overload for your reporting class that accepts an instance of your test runner classes...? – Brian Dec 3 '19 at 5:18
0

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.

|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.