SpecFlow — Step (Given) with the same regex in different classes not executing independently

StackOverflow https://stackoverflow.com/questions/5463212

  •  13-11-2019
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

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 ;)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top