如何访问在 SpecFlow 中的 AfterScenario 挂钩的步骤中创建的 class 的实例?
How to access an instance of a class created in a step from AfterScenario hook in SpecFlow?
我有一个名为“RollbackManager
”的class,它用于添加一些来自测试的动作并执行测试后的所有动作。
我使用 SpecFlow 进行测试,因此为了在测试后执行某些操作,我使用 [AfterScenario]
挂钩。
问题是: 我不能使 RollbackManager
成为静态的,因为我 运行 并行测试!
问题是:如何从挂钩访问在 SpecFlow 步骤定义中创建的 RollBackManager class 实例?
我当前的项目结构:
Base class 与 RollbackManager:
public class StepBase : Steps
{
public RollbackManager RollbackManager { get; } = new RollbackManager();
protected new ScenarioContext ScenarioContext { get; set; }
public StepBase(ScenarioContext scenarioContext)
{
RollbackManager = new RollbackManager();
}
}
步骤定义示例 class:
[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());
}
}
我的 class 有钩子:
[Binding]
public sealed class SpecFlowTestsBase
{
[AfterScenario]
public void AfterScenario()
{
// here I need my rollbacks craeted in steps
}
}
首先,不要使用基本步骤 class 并将钩子放入其中。然后你的钩子将被执行多次。
现在回答你真正的问题:
要在步骤 class 之间共享状态,您可以使用 SpecFlow 的上下文注入。
绑定 class 的每个实例都将获得相同的 TestState 实例 class.
它是这样工作的:
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();
}
}
您可以在此处找到其他文档:
https://specflow.org/documentation/Sharing-Data-between-Bindings/
https://specflow.org/documentation/Context-Injection/
我有一个名为“RollbackManager
”的class,它用于添加一些来自测试的动作并执行测试后的所有动作。
我使用 SpecFlow 进行测试,因此为了在测试后执行某些操作,我使用 [AfterScenario]
挂钩。
问题是: 我不能使 RollbackManager
成为静态的,因为我 运行 并行测试!
问题是:如何从挂钩访问在 SpecFlow 步骤定义中创建的 RollBackManager class 实例?
我当前的项目结构:
Base class 与 RollbackManager:
public class StepBase : Steps
{
public RollbackManager RollbackManager { get; } = new RollbackManager();
protected new ScenarioContext ScenarioContext { get; set; }
public StepBase(ScenarioContext scenarioContext)
{
RollbackManager = new RollbackManager();
}
}
步骤定义示例 class:
[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());
}
}
我的 class 有钩子:
[Binding]
public sealed class SpecFlowTestsBase
{
[AfterScenario]
public void AfterScenario()
{
// here I need my rollbacks craeted in steps
}
}
首先,不要使用基本步骤 class 并将钩子放入其中。然后你的钩子将被执行多次。
现在回答你真正的问题:
要在步骤 class 之间共享状态,您可以使用 SpecFlow 的上下文注入。
绑定 class 的每个实例都将获得相同的 TestState 实例 class.
它是这样工作的:
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();
}
}
您可以在此处找到其他文档:
https://specflow.org/documentation/Sharing-Data-between-Bindings/
https://specflow.org/documentation/Context-Injection/