anshooarora / extentreports-csharp

Community version of Extent API for .NET has moved to https://github.com/extent-framework/
http://extentreports.com/
Other
47 stars 43 forks source link

ExtentReport not writing to html file from Test only from OneTimeSetUp #110

Closed qamessenger closed 6 years ago

qamessenger commented 6 years ago

Versions: ExtentReports 3.1.1 Nunit 3.10.1

I have setup the example on how to create the report using my test code. When I run the tests I only get the test results from the OneTimeSetup of my test. The tests do not log anything to the html file. What is wrong. See code and screen shot.

If I remove the _test = _extent.CreateTest("ClassSetup"); entry under OneTimeSetup. I does not create the html file at all.

using System; using System.Diagnostics; using AventStack.ExtentReports; using NUnit.Framework; using NUnit.Framework.Interfaces; using System.IO;

namespace AcceptanceTests { [TestFixture] public class TestReporting { public ExtentReports _extent; public ExtentTest _test; public IExtentReporter _htmlReporter;

    [OneTimeSetUp]
    public void ClassSetup()
    {
        string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
        string actualPath = path.Substring(0, path.LastIndexOf("bin"));
        string projectPath = new Uri(actualPath).LocalPath;
        string reportPath = Path.Combine(projectPath, @"Reports\MyOwnReport.html");

        _extent = new ExtentReports();
        _extent.AddSystemInfo("Host Name", "Local");
        _extent.AddSystemInfo("Environment", "QA");
        _extent.AddSystemInfo("User Name", "Greg");
        _htmlReporter = new AventStack.ExtentReports.Reporter.ExtentHtmlReporter(reportPath);
        _htmlReporter.LoadConfig(projectPath + "extent-config.xml");
        _extent.AttachReporter(_htmlReporter);
        _test = _extent.CreateTest("ClassSetup");
    }

    /// <summary>
    /// Run after each test
    /// </summary>
    [TearDown]
    public void TearDown()
    {
        var status = TestContext.CurrentContext.Result.Outcome.Status;
        var stackTrace = "<pre>" + TestContext.CurrentContext.Result.StackTrace + "</pre>";
        var errorMessage = TestContext.CurrentContext.Result.Message;

        if (status == TestStatus.Failed)
        {
            _test.Log(Status.Fail, stackTrace + errorMessage);
        }
        _extent.RemoveTest(_test);
    }

    /// <summary>
    /// Once per assembly, before any test are run
    /// </summary>
    [OneTimeTearDown]
    public void ClassTeardown()
    {
        _extent.Flush();
        //_extent = null;
        //_htmlReporter = null;
    }

    [Test]
    public void TestPass()
    {
        _test = _extent.CreateTest("LoginTest");
        Assert.AreEqual(true, true);
        _test.Pass("LoginTest PASS");
        _test.Log(Status.Info, "This gives log info if needed");

    }
    [Test]
    public void TestFail()
    {
        _test = _extent.CreateTest("LogoutTest");
        Assert.AreEqual(true, false);
        _test.Log(Status.Fail, "LogoutTest");
        _test.Log(Status.Info, "This gives log info if needed");

    }
}

}

results

qamessenger commented 6 years ago

I found the problem. If you call _extent.RemoveTest(_test); method in TearDown the html file is not created and nothing is logged.