C# extent 报告在运行测试时未生成 html 报告文件

问题描述 投票:0回答:1

我正在 Visual Studio C# 中试用 ExtentReports。当我运行测试用例时,未生成 html 报告文件。我不确定我的代码有什么问题。 我认为我的报告文件夹的路径是正确的。 在解决方案资源管理器中,我创建了一个名为“Reports”的文件夹,我希望在此处创建报告文件。

我的代码片段是:

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();
        }

        }

    }

构建解决方案时没有错误。测试运行良好,但未生成 testreport.html。 我已经使用 NuGet 安装了 extentReports 并安装了 Nunit。

谢谢,里亚兹

c# selenium-webdriver reporting
1个回答
0
投票
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();
        }
    }
}

您只需要将 ExtentConfig.xml 文件放入特定文件夹,TestReport.html 在特定路径上自动生成。从给定链接复制 xml extentReports.xml

© www.soinside.com 2019 - 2024. All rights reserved.