如何最好地为 Specflow 引入一个 Base class?这个不行

How is best to introduce a Base class for Specflow? This one isn't working

有人知道如何创建基类以便步骤定义可以继承吗?需要在 [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
{
}

谢谢

由于您只是使用钩子,因此不需要基础 class。相反,创建一个专门用于扩展报告逻辑的 class。无需在您的其他步骤定义中继承此 class:

[Binding]
public class ExtentReports
{
    [Before]
    public void BeforeScenario(ScenarioInfo scenario)
    {
        // Extent report logic
    }

    [After]
    public void AfterScenario(ScenarioInfo scenario)
    {
        // Extent report logic
    }
}

如果您需要多步骤定义中的 Extent 报告逻辑 classes,请考虑创建另一个 class 并使用 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 class):

[Binding]
public class YourSteps
{
    ExtentReportUtils extentUtils;

    public YourSteps(ExtentReportUtils extentUtils)
    {
        this.extentUtils = extentUtils;
    }

    [Given(@"...")]
    public void GivenX()
    {
        extentUtils.Foo(...);
    }
}