gpt4 book ai didi

c# - 与 Specflow 并行运行 BDD 测试时,如何在 C# 中创建范围报告

转载 作者:行者123 更新时间:2023-12-05 03:59:35 24 4
gpt4 key购买 nike

当我单独运行 C# Specflow 测试时,我的范围报告会正确生成。

但是,当我并行运行测试时,范围报告是错误的。步骤被写入错误的场景,来自同一功能的场景未分组。

有没有人在使用 C# 和 Specflow 并行运行测试时成功地生成了 ExtentReport?

下面是我目前使用的代码。

感谢您的帮助。

using AutomationFramework.Base;
using TechTalk.SpecFlow;
using AutomationFramework.Helpers;
using AutomationFramework.Config;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using AventStack.ExtentReports.Gherkin.Model;

namespace EmployeeTest.Hooks
{

[Binding]
public class HookInitialize : TestInitializeHook
{

private readonly ParallelConfig _parallelConfig;
private readonly FeatureContext _featureContext;
private readonly ScenarioContext _scenarioContext;

public HookInitialize(ParallelConfig parallelConfig, FeatureContext featureContext, ScenarioContext scenarioContext) : base(parallelConfig)
{
_parallelConfig = parallelConfig;
_featureContext = featureContext;
_scenarioContext = scenarioContext;
}

private static ExtentTest featureName;
private static ExtentTest scenario;
private static ExtentReports extent;


[BeforeTestRun]
public static void TestInitalize()
{
//Initialize the Report
var htmlReporter = new ExtentHtmlReporter(@"C:\Logs\ExtentReport.html");
htmlReporter.Config.Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Dark;
htmlReporter.Config.ReportName = "Automation Test Report";
extent = new ExtentReports();
extent.AttachReporter(htmlReporter);

}


[AfterStep]
public void InsertReportingSteps()
{
var stepType = _scenarioContext.StepContext.StepInfo.StepDefinitionType.ToString();


if (_scenarioContext.TestError == null)
{
if (stepType == "Given")
scenario.CreateNode<Given>(_scenarioContext.StepContext.StepInfo.Text);
else if (stepType == "When")
scenario.CreateNode<When>(_scenarioContext.StepContext.StepInfo.Text);
else if (stepType == "Then")
scenario.CreateNode<Then>(_scenarioContext.StepContext.StepInfo.Text);
else if (stepType == "And")
scenario.CreateNode<And>(_scenarioContext.StepContext.StepInfo.Text);
}
else if (_scenarioContext.TestError != null)
{
if (stepType == "Given")
{
scenario.CreateNode<Given>(_scenarioContext.StepContext.StepInfo.Text).Fail(_scenarioContext.TestError.InnerException);
ScreenShotHelpers.CaptureScreen(_parallelConfig.Driver, _featureContext.FeatureInfo.Title);
}
else if (stepType == "When")
{
scenario.CreateNode<When>(_scenarioContext.StepContext.StepInfo.Text).Fail(_scenarioContext.TestError.InnerException);
ScreenShotHelpers.CaptureScreen(_parallelConfig.Driver, _featureContext.FeatureInfo.Title);
}
else if (stepType == "Then")
{
scenario.CreateNode<Then>(_scenarioContext.StepContext.StepInfo.Text).Fail(_scenarioContext.TestError.Message);
ScreenShotHelpers.CaptureScreen(_parallelConfig.Driver, _featureContext.FeatureInfo.Title);
}
}



}

[BeforeScenario]
public void Initialize()
{
InitializeSettings();
Settings.ApplicationCon = Settings.ApplicationCon.DBConnect(Settings.AppConnectionString);

//Create feature name
featureName = extent.CreateTest<Feature>(_featureContext.FeatureInfo.Title);

//Get scenario name
scenario = featureName.CreateNode<Scenario>(_scenarioContext.ScenarioInfo.Title);

}


[AfterScenario]
public void TestStop()
{
_parallelConfig.Driver.Quit();

}

[AfterTestRun]
public static void TearDownReport()
{
//Write the report to the report directory
extent.Flush();


}

}

最佳答案

我正在尝试同样的事情。这是快速修复

将 [ThreadStatic] 属性用于场景变量。它为您提供线程特定变量。

 [ThreadStatic]
private static ExtentTest scenario;

关于c# - 与 Specflow 并行运行 BDD 测试时,如何在 C# 中创建范围报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57060415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com