【问题标题】:How is best to introduce a Base class for Specflow? This one isn't working如何最好地为 Specflow 引入基类?这个不行
【发布时间】:2021-08-11 15:22:25
【问题描述】:
有人知道如何创建一个基类以便可以继承步骤定义吗?需要在 [setup] 中集成范围报告。这个我试过了,但是没有输出
public class BaseClass(){
[Before]
public class void BeforeTests(){
console.writeline("This runs before the tests");
//Extent reports setup code here
}
[After]
public class void AfterTests(){
//Extent reports flush code here
console.writeline("This runs after the tests");
}
}
[Bindings]
public class StepDefinitionFile: BaseClass
{
}
谢谢
【问题讨论】:
标签:
c#
specflow
extentreports
【解决方案1】:
由于您只是使用钩子,因此不需要基类。相反,创建一个专门用于扩展报表逻辑的类。无需在其他步骤定义中从此类继承:
[Binding]
public class ExtentReports
{
[Before]
public void BeforeScenario(ScenarioInfo scenario)
{
// Extent report logic
}
[After]
public void AfterScenario(ScenarioInfo scenario)
{
// Extent report logic
}
}
如果在多个步骤定义类中需要Extent报告逻辑,可以考虑再创建一个类,使用context injection获取对象:
public class ExtentReportUtils
{
// common Extent Report logic and methods here
}
[Binding]
public class SpecflowHooks
{
IObjectContainer container;
public SpecflowHooks(IObjectContainer container)
{
this.container = container;
}
[Before]
public void Before()
{
var utils = new ExtentReportUtils();
container.RegisterInstanceAs(utils);
}
}
并在步骤定义中使用它(甚至是上面的ExtentReports 类):
[Binding]
public class YourSteps
{
ExtentReportUtils extentUtils;
public YourSteps(ExtentReportUtils extentUtils)
{
this.extentUtils = extentUtils;
}
[Given(@"...")]
public void GivenX()
{
extentUtils.Foo(...);
}
}