C#范围报告运行测试时未生成html报告文件

时间:2016-12-01 14:47:15

标签: c# selenium-webdriver reporting

我在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。

谢谢Riaz

1 个答案:

答案 0 :(得分: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。从给定链接extentReports.xml复制xml