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

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

2 Answers 2

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.

0

For generating reports in all tests without rewriting the code in C# using Extent Reports, you needs to follow below process -

Create a Base Test Class:

You should to create a base test class where you place your reporting code, so that all other test classes can inherit from it. This will allow the OneTimeSetUp and TearDown methods to be called for every test in different files.

Inheritance in Test Classes: Your test classes like TestLoginModule.cs and TestDashboardModule.cs should to inherit from the base test class where reporting code is written.

BaseTest Class (reporting.cs)

public class Reporting
{
    [OneTimeSetUp]
    public void BeforeClass()
    {
        // code for setup
    }

    [TearDown]
    public void AfterTest()
    {
        // code for tear down
    }
}

In TestLoginModule.cs

public class TestLoginModule : Reporting
{
    [Test]
    public void TestFirst()
    {
        // test code
    }
    
    [Test]
    public void TestSecond()
    {
        // test code
    }
}

TestDashboardModule.cs

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

Note: the Reporting class's OneTimeSetUp and TearDown method will be called for every test in different files, without needing to write again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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