Unable to generate Extent reports (Version 3.1.3)in selenium with c#

痞子三分冷 提交于 2019-12-09 23:07:40

问题


I am new to C# and I am trying to use ExtentReports for generating reports ( Version used: 3.13). Any help with this would be much appreciated thank you.

I am getting below error: Message: System.InvalidOperationException : No reporters were started. Atleast 1 reporter must be started to create tests.

Here is my Code:`

using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutomationReports
    {
    class ReportsGenerationClass
        {
        protected ExtentReports _extent;
        protected ExtentTest _test;

        [OneTimeSetUp]
        protected void Setup()
            {
            var dir = TestContext.CurrentContext.TestDirectory + "\\";
            var fileName = this.GetType().ToString() + ".html";
            var htmlReporter = new ExtentHtmlReporter(dir + fileName);

            _extent = new ExtentReports();
            _extent.AttachReporter(htmlReporter);
            }

        [OneTimeTearDown]
        protected void TearDown()
            {
            _extent.Flush();
            }

        [SetUp]
        public void BeforeTest()
            {
            _test = _extent.CreateTest(TestContext.CurrentContext.Test.Name);
            }

        [TearDown]
        public void AfterTest()
            {
            var status = TestContext.CurrentContext.Result.Outcome.Status;
            var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace)
                    ? ""
                    : string.Format("{0}", TestContext.CurrentContext.Result.StackTrace);
            Status logstatus;

            switch (status)
                {
                case TestStatus.Failed:
                    logstatus = Status.Fail;
                    break;
                case TestStatus.Inconclusive:
                    logstatus = Status.Warning;
                    break;
                case TestStatus.Skipped:
                    logstatus = Status.Skip;
                    break;
                default:
                    logstatus = Status.Pass;
                    break;
                }

            _test.Log(logstatus, "Test ended with " + logstatus + stacktrace);
            _extent.Flush();
            }


        [Test]
        public void PassingTest()
            {

            ExtentReports extent = new ExtentReports();
            _test = extent.CreateTest("PassingTest");
            Driver.Navigate().GoToUrl("http://www.google.com");

            try
                {
                Assert.IsTrue(true);
                _test.Pass("Assertion passed");
                _test.Log(Status.Pass, "Pass");
                }
            catch (AssertionException)
                {
                _test.Fail("Assertion failed");
                _test.Log(Status.Fail, "Fail");
                throw;
                }
            }
        }
    }

回答1:


In PassingTest method, remove the lines:

ExtentReports extent = new ExtentReports();
_test = extent.CreateTest("PassingTest");

And it should work.

You already initialize the ExtentReports object and the _test field correctly in the [OneTimeSetUp] and [SetUp] methods, but you were overwriting it redundantly and incorrectly inside the test method.



来源:https://stackoverflow.com/questions/49700689/unable-to-generate-extent-reports-version-3-1-3in-selenium-with-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!