范围报告Nunit C#覆盖了最后一个类的结果,以执行多个类

时间:2019-11-15 16:32:02

标签: c# nunit nunit-console

我在Nunit c#中为我的测试集设置了几种不同的类设置,并且为html报告实现了extedn report 4.0。我遇到的问题是使用Nunit GUI运行器执行测试用例时。它执行我所有的测试用例,但在报告中将仅显示上一个执行的类的测试用例结果。

请问有人可以帮我怎样为我所有的班级生成一份扩展报告。

这是我为范围报告编写的代码。

namespace Test
{
    using System;
    using System.Collections.Generic;
    using System.Drawing.Imaging;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using AventStack.ExtentReports;
    using AventStack.ExtentReports.Reporter;
    using NUnit.Framework;
    using NUnit.Framework.Interfaces;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;


    public class Report
    {
        public IWebDriver driver;
        protected ExtentReports _extent;
        public ExtentTest _test;


        ///For report directory creation and HTML report template creation
        ///For driver instantiation

#pragma warning disable SA1604 // Element documentation must have summary
        public void BeforeClass()
#pragma warning restore SA1604 // Element documentation must have summary
        {
            try
            {
                ExtentHtmlReporter htmlReporter;
                string path = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", string.Empty);
                DirectoryInfo di = Directory.CreateDirectory(path + "\\Test_Execution_Reports\\" + DateTime.Now.ToString("yyyyMMM")+"\\"+ DateTime.Now.ToString("dd") + "\\" + DateTime.Now.ToString("dd_HHmm"));
                string reportpath = path + "Test_Execution_Reports\\" + DateTime.Now.ToString("yyyyMMM") + "\\" + DateTime.Now.ToString("dd") + "\\" + DateTime.Now.ToString("dd_HHmm") + "\\AutomationReport.html";

                htmlReporter = new ExtentHtmlReporter(reportpath);
                htmlReporter.LoadConfig(path + "extent-config.xml");

                htmlReporter.Config.DocumentTitle = "Regression Test Automation";
                htmlReporter.Config.ReportName = "Regression Test Automation report";

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-AT");

                _extent = new ExtentReports();
                _extent.AddSystemInfo("Browser", TestDriver.Settings.Browser);
                _extent.AddSystemInfo("Environment", TestDriver.Settings.Environment);
                _extent.AddSystemInfo("User Name", Environment.UserName);
                _extent.AddSystemInfo("Domain", Environment.UserDomainName);
                _extent.AddSystemInfo("Machine Name", Environment.MachineName);
                _extent.AnalysisStrategy = AnalysisStrategy.Test;
                _extent.AttachReporter(htmlReporter);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Getting the name of current running test to extent report
        /// </summary>
       // [SetUp]
        public void BeforeTest()
        {
            try
            {
                _test = _extent.CreateTest(TestContext.CurrentContext.Test.MethodName, TestContext.CurrentContext.Test.Name);
                _test.Log(Status.Pass,"Test steps start for test case " + TestContext.CurrentContext.Test.Name);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void AfterTest(IWebDriver driver)
        {
            try
            {
                var status = TestContext.CurrentContext.Result.Outcome.Status;
                var stacktrace = string.Empty + TestContext.CurrentContext.Result.StackTrace + string.Empty;
                var errorMessage = TestContext.CurrentContext.Result.Message;
                Status logstatus;
                switch (status)
                {
                    case TestStatus.Failed:
                        logstatus = Status.Fail;
                        string path = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", string.Empty);
                        string finalpth = path + "Defect_Screenshots\\" + DateTime.Now.ToString("yyyyMMMdd") + "\\testImage.png";
                        _test.Log(logstatus, "Test steps NOT Completed for Test case " + TestContext.CurrentContext.Test.Name + " ");
                        _test.Log(logstatus, "Test ended with " + logstatus + " – " + errorMessage);
                        _test.Log(logstatus, "Snapshot below for Test Case :  " + TestContext.CurrentContext.Test.Name + " " + _test.AddScreenCaptureFromPath(finalpth));
                        break;
                    case TestStatus.Skipped:
                        logstatus = Status.Skip;
                        _test.Log(logstatus, "Test ended with " + logstatus);
                        break;
                    default:
                        logstatus = Status.Pass;
                        _test.Log(Status.Pass, "Test steps finished for test case " + TestContext.CurrentContext.Test.Name);
                        _test.Log(logstatus, "Test ended with " + logstatus);
                        break;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// To flush extent report
        /// </summary>
        public void AfterClass()
        {
            try
            {
                _extent.Flush();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}

0 个答案:

没有答案