'C# extent reports when running a test the html report file is not being generated

I am trying out ExtentReports in Visual Studio C#. When i run a test case the html report file is not being generated. I am not sure what is wrong in my code. I think the path to my reports folder is correct. In Solution Explorer I created a folder called "Reports" and I would like the report file to be created here.

My code snippet is:

using NUnit.Framework;
using RelevantCodes.ExtentReports;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtentReportsDemo
{
    [TestFixture]
    public class BasicReport
    {
        public ExtentReports extent;
        public ExtentTest test;

        [OneTimeSetUp]
        public void StartReport()
        {
            string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
            string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
            string projectPath = new Uri(actualPath).LocalPath; // project path of your solution

            string reportPath = projectPath + "Reports\\testreport.html";

            // true if you want to append data to the report.  Replace existing report with new report.  False to create new report each time
            extent = new ExtentReports(reportPath, false);
            extent.AddSystemInfo("Host Name", "localhost")
                .AddSystemInfo("Environment", "QA")
                .AddSystemInfo("User Name", "testUser");

            extent.LoadConfig(projectPath + "extent-config.xml");

        }

        [Test]
        public void DemoReportPass()
        {
            test = extent.StartTest("DemoReportPass");
            Assert.IsTrue(true);
            test.Log(LogStatus.Pass, "Assert Pass as consition is true");

        }

        [Test]
        public void DemoReportFail()
        {
            test = extent.StartTest("DemoReportPass");
            Assert.IsTrue(false);
            test.Log(LogStatus.Fail, "Assert Pass as condition is false");

        }

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

            if (status == NUnit.Framework.Interfaces.TestStatus.Failed)
            {
                test.Log(LogStatus.Fail, stackTrace + errorMessage);
            }
            extent.EndTest(test);

        }

        [OneTimeTearDown]
        public void EndReport()
        {
            extent.Flush();
            extent.Close();
        }

        }

    }

There are no errors when I build the solution. The test runs fine but the testreport.html is not being generated. I have used NuGet to install extentReports and installed Nunit.

Thanks, Riaz



Solution 1:[1]

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RelevantCodes.ExtentReports;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using NUnit.Framework;
using NUnit.Framework.Interfaces;

namespace testingExtentReports
{
    [TestClass]
    public class UnitTest1
    {
        public ExtentReports extent;
        public ExtentTest test;
        IWebDriver driver;
        [SetUp]
        public void StartReport()
        {
            string reportPath = @"D:\\TestReport.html";
            extent = new ExtentReports(reportPath, true);
            extent.AddSystemInfo("Host Name", "Your Host Name")
                  .AddSystemInfo("Environment", "YourQAEnvironment")
                  .AddSystemInfo("Username", "Your User Name");
            string xmlPath = @"D:\\ExtentConfig.xml";
            extent.LoadConfig(xmlPath);
        }

        [Test]
        public void OpenTest1()
        {

            test = extent.StartTest("OpenTest1", "test Started");
            test.Log(LogStatus.Pass, "Website is open properly");
            test.Log(LogStatus.Pass, "Successfully Login into agency Portal");
            test.Log(LogStatus.Info, "Click on UI button link");
            test.Log(LogStatus.Fail, "Error Occurred while creating document");
            test.Log(LogStatus.Pass, "Task Released Succssfully");
            test.Log(LogStatus.Warning, "Workflow saved with warning");
            test.Log(LogStatus.Error, "Error occurred while releasing task.");
            test.Log(LogStatus.Unknown, "Dont know what is happning.");
            test.Log(LogStatus.Fatal, "Unhandled exception occured.");
            extent.EndTest(test);
            extent.Flush();
            extent.Close();
        }
    }
}

You just need to Put the ExtentConfig.xml file to your specific folder, TestReport.html automatically generated on a specific path.Copy xml from given link extentReports.xml

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dhru 'soni