无法使用 c# 在 selenium 中生成范围报告(版本 3.1.3)
Unable to generate Extent reports (Version 3.1.3)in selenium with c#
我是 C# 的新手,我正在尝试使用 ExtentReports 生成报告(使用的版本:3.13)。如有任何帮助,我们将不胜感激。
我遇到以下错误:
消息:System.InvalidOperationException:没有启动记者。必须至少启动 1 个记者才能创建测试。
这是我的代码:`
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;
}
}
}
}
在PassingTest
方法中,删除行:
ExtentReports extent = new ExtentReports();
_test = extent.CreateTest("PassingTest");
它应该可以工作。
您已经在 [OneTimeSetUp]
和 [SetUp]
方法中正确初始化了 ExtentReports
对象和 _test
字段,但是您在测试中冗余且错误地覆盖了它方法。
我是 C# 的新手,我正在尝试使用 ExtentReports 生成报告(使用的版本:3.13)。如有任何帮助,我们将不胜感激。
我遇到以下错误: 消息:System.InvalidOperationException:没有启动记者。必须至少启动 1 个记者才能创建测试。
这是我的代码:`
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;
}
}
}
}
在PassingTest
方法中,删除行:
ExtentReports extent = new ExtentReports();
_test = extent.CreateTest("PassingTest");
它应该可以工作。
您已经在 [OneTimeSetUp]
和 [SetUp]
方法中正确初始化了 ExtentReports
对象和 _test
字段,但是您在测试中冗余且错误地覆盖了它方法。