Automated tests 如何从SpecFlow中的AfterScenario钩子访问步骤中创建的类的实例?

Automated tests 如何从SpecFlow中的AfterScenario钩子访问步骤中创建的类的实例?,automated-tests,ui-automation,specflow,Automated Tests,Ui Automation,Specflow,我有一个名为“RollbackManager”的类,它用于从测试中添加一些操作,并在测试后执行所有操作 我在测试中使用SpecFlow,因此为了在测试后执行某些东西,我使用了[AfterScenario]钩子 问题是:在并行运行测试时,我无法将回滚管理器设置为静态 问题是:如何从钩子访问在SpecFlow步骤定义中创建的RollBackManager类的实例 我当前的项目结构: 具有回滚管理器的基类: public class StepBase : Steps { public Roll

我有一个名为“
RollbackManager
”的类,它用于从测试中添加一些操作,并在测试后执行所有操作

我在测试中使用SpecFlow,因此为了在测试后执行某些东西,我使用了
[AfterScenario]
钩子

问题是:在并行运行测试时,我无法将
回滚管理器
设置为静态

问题是:如何从钩子访问在SpecFlow步骤定义中创建的RollBackManager类的实例

我当前的项目结构:

具有回滚管理器的基类:

public class StepBase : Steps
{
    public RollbackManager RollbackManager { get; } = new RollbackManager();

    protected new ScenarioContext ScenarioContext { get; set; }

    public StepBase(ScenarioContext scenarioContext)
    {
        RollbackManager = new RollbackManager();
    }
}
步骤定义类的示例:

[Binding]
public sealed class ThenExport : StepBase
{
    public ThenExport(ScenarioContext scenarioContext) : base(scenarioContext)
    {
    }

    [Then(@"export status should contain entities: ""(.*)""")]
    public void ThenExportStatusShouldContain(List<String> commaSeparatedList)
    { 
        RollbackManager.RegisterRollback(() => Console.WriteLine());
    }
}

首先,不要使用基步类并在其中放入挂钩。您的钩子将执行多次

现在谈谈你真正的问题:

要在步骤类之间共享状态,可以使用SpecFlow的上下文注入。
binding类的每个实例都将获得TestState类的相同实例

它的工作原理如下:

public class TestState
{
    public RollbackManager RollbackManager { get; } = new RollbackManager();
}

[Binding]
public sealed class ThenExport 
{
    private TestState _testState;

    public ThenExport(TestState testState)
    {
        _testState = testState;
    }

    [Then(@"export status should contain entities: ""(.*)""")]
    public void ThenExportStatusShouldContain(List<String> commaSeparatedList)
    { 
        _testState.RollbackManager.RegisterRollback(() => Console.WriteLine());
    }
}

[Binding]
public sealed class Hooks
{
    private TestState _testState;

    public ThenExport(TestState testState)
    {
        _testState = testState;
    }

    [AfterScenario]
    public void AfterScenario()
    {
        _testState.RollBackManager.DoYourStuff();
    }
}
公共类TestState
{
public RollbackManager RollbackManager{get;}=new RollbackManager();
}
[有约束力]
公共密封类出口
{
私有测试状态_TestState;
公共导出(TestState TestState)
{
_testState=testState;
}
[然后(@“导出状态应包含实体:“”(.*))]
public void ThenExportStatus应包含(列表commaSeparatedList)
{ 
_testState.RollbackManager.RegisterRollback(()=>Console.WriteLine());
}
}
[有约束力]
公共密封类挂钩
{
私有测试状态_TestState;
公共导出(TestState TestState)
{
_testState=testState;
}
[赛后]
赛后公众无效()
{
_testState.RollBackManager.DoYourStuff();
}
}
您可以在此处找到其他文档:

非常感谢,似乎有帮助!我误解了上下文注入的用法。谢谢你的例子。
public class TestState
{
    public RollbackManager RollbackManager { get; } = new RollbackManager();
}

[Binding]
public sealed class ThenExport 
{
    private TestState _testState;

    public ThenExport(TestState testState)
    {
        _testState = testState;
    }

    [Then(@"export status should contain entities: ""(.*)""")]
    public void ThenExportStatusShouldContain(List<String> commaSeparatedList)
    { 
        _testState.RollbackManager.RegisterRollback(() => Console.WriteLine());
    }
}

[Binding]
public sealed class Hooks
{
    private TestState _testState;

    public ThenExport(TestState testState)
    {
        _testState = testState;
    }

    [AfterScenario]
    public void AfterScenario()
    {
        _testState.RollBackManager.DoYourStuff();
    }
}