SpecFlow——不同类中具有相同正则表达式的步骤(给定)不独立执行

发布于 2024-10-27 13:50:16 字数 523 浏览 1 评论 0原文

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

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

当我运行 A 类中定义的功能的场景时,并且测试运行程序执行上面指示的步骤,则会执行 B 类中的匹配步骤。

“Steps”也是全球性的吗?我认为只有“钩子”方法是全局的,即BeforeScenario、AfterScenario。我不希望“给定”、“然后”和“何时”出现这种行为。有什么办法可以解决这个问题吗?我尝试将这两个类放在不同的名称空间中,但这也不起作用。

另外,如果我将它们放在单独的类中,我是否可能会滥用 SpecFlow,希望每个“Given”都是独立的?

I have two classes (class A and B) both marked with [Binding]. Currently I'm using a class per feature. Classes A and B both have a step that looks like this:

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

When I run the scenario for the features defined in class A, and the test runner executes the step indicated above, the matching step in class B gets executed instead.

Are "Steps" global as well? I thought only the "hook" methods are global, i.e. BeforeScenario, AfterScenario. I do not want this behavior for "Given", "Then", and "When". Is there any way to fix this? I tried putting the two classes in different namespaces and this didn't work either.

Also, am I potentially misusing SpecFlow by wanting each "Given" to be independent if I put them in separate classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-11-03 13:50:17

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

位于单独的类中或其他位置(甚至其他程序集)与 SpecFlow 的分组方式没有任何关系 - 它只是一个很大的给定、何时和然后的列表,它尝试匹配步骤。

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

这个想法是,您在步骤定义方法上放置另一个属性 (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'");
}

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

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

此步骤定义使用 StepScope 作为标记。您可以通过以下方式确定您的步骤范围:

  • 标签
  • 场景标题
  • 功能标题

好问题!直到现在我才完全明白那是做什么的;)

Yes Steps are (per default) global. So you will run into trouble if you define two attributes that have RegExps that matches the same Step. Even if they are in separate classes.

Being in separate classes, or other placement (other assembly even) doesn't have anything to do with how SpecFlow groups it - it's just a big list of Given's, When's and Then's that it try to match the Step against.

But there's a feature called Scoped Steps that solves this problem for you. Check it out here: https://github.com/techtalk/SpecFlow/blob/master/Tests/FeatureTests/ScopedSteps/ScopedSteps.feature

The idea is that you put another attribute (StepScope) on your Step Defintion method and then it will honor that scoping. Like this for example:

[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'");
}

... or to scope an entire step definition class to a single feature:

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

This step definition is using a StepScope for a tag. You can scope your steps by:

  • Tag
  • Scenario title
  • Feature title

Great question! I hadn't fully understood what that was for until now ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文