SpecFlow - Step(Given)在不同的类中使用相同的正则表达式而不是独立执行

Sid*_*eFX 9 cucumber specflow gherkin

我有两个类(A类和B类)都标有[Binding].目前我正在使用每个功能的类.类A和B都有一个如下所示的步骤:

   [Given(@"an employee (.*) (.*) is a (.*) at (.*)")]
   public void GivenAnEmployeeIsAAt(string firstName, string lastName, string role, string businessUnitName)
Run Code Online (Sandbox Code Playgroud)

当我运行A类中定义的功能的场景,并且测试运行器执行上面指出的步骤时,将执行B类中的匹配步骤.

"步骤"也是全球性的吗?我认为只有"钩子"方法是全局的,即BeforeScenario,AfterScenario.我不希望这种行为为"给定","然后"和"何时".有没有什么办法解决这一问题?我尝试将这两个类放在不同的命名空间中,这也不起作用.

另外,如果我把它们放在不同的类中,我希望每个"Given"是独立的,我可能会滥用SpecFlow吗?

Mar*_*erg 13

是步骤(默认情况下)全局.因此,如果定义两个具有与同一步骤匹配的RegExps的属性,则会遇到麻烦.即使他们在不同的班级.

处于单独的类或其他位置(甚至是其他程序集)与SpecFlow如何对它进行分组没有任何关系 - 它只是Given的一个大列表,When's和Then它试图匹配Step.

但是有一个名为Scoped Steps的功能可以解决这个问题.请在此处查看:https://github.com/techtalk/SpecFlow/blob/master/Tests/FeatureTests/ScopedSteps/ScopedSteps.feature

这个想法是你在Step Defintion方法上放置了另一个属性(StepScope),然后它将遵循该范围.像这样例如:

[Given(@"I have a step definition that is scoped to tag (?:.*)")]
[StepScope(Tag = "mytag")] 
public void GivenIHaveAStepDefinitionThatIsScopedWithMyTag()
{
    stepTracker.StepExecuted("Given I have a step definition that is scoped to tag 'mytag'");
}
Run Code Online (Sandbox Code Playgroud)

...或将整个步骤定义类的范围限定为单个特征:

[Binding]
[StepScope(Feature = "turning on the light should make it bright")]
public class TurningOnTheLightSteps
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

此步骤定义使用StepScope作为标记.您可以通过以下方式确定步骤

  • 标签
  • 场景标题
  • 专题标题

好问题!直到现在我还没有完全明白这是什么;)