Sat*_*ish 2 c# nunit specflow context-injection
我有一个 hooks.cs 绑定文件,其中包含 BeforeTestRun、BeforeFeature 和 BeforeScenario。我需要当前功能和场景的标题以用于日志和报告目的。由于我并行运行测试,ScenarioContext 抛出异常为:
ScenarioContext.Current 静态访问器不能用于多线程执行...
有什么办法可以在多线程执行中获得当前的功能标题和场景标题吗?
是的,您可以通过构造函数注入获取当前的 FeatureContext 和 ScenarioContext。
public class MyBindingClass
{
private ScenarioContext scenarioContext;
public MyBindingClass(ScenarioContext scenarioContext)
{
this.scenarioContext = scenarioContext;
}
[When("I say hello to ScenarioContext")]
public void WhenISayHello()
{
// access scenarioContext here
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅https://specflow.org/documentation/ScenarioContext/ - 在底部注入 ScenarioContext。
| 归档时间: |
|
| 查看次数: |
370 次 |
| 最近记录: |