Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specflow - Using the same step def for any keyword

How do I use a step definition in a feature file where the starting keyword can be any of Given/When/Then

example:

Feature file

Given I do something
When I do something else
Then blah blah
And I do something else

How do I write just the 1 step definition to deal with the 2 x "I do something else". As far as I can tell right now I have to write 2 x identical step definitions to deal with this

Currently I have to do this in the Step Def file

[When(@"I do something else"]
public blah()
{
do something
}

[Then(@"I do something else"]
public blah2()
{
do something
}
like image 646
Valkorum Avatar asked Nov 22 '25 20:11

Valkorum


1 Answers

You can put multiple attributes on the same method.
So in your case it would look like this:

[When(@"I do something else"]
[Then(@"I do something else"]
public void blah()
{
// do something
}

If you want that a step is for all 3 parts (Given/When/Then), you can use the StepDefinitionAttribute.

In that case it looks like this:

[StepDefinition(@"I do something else"]
public void blah()
{
// do something
}
like image 182
Andreas Willich Avatar answered Nov 24 '25 09:11

Andreas Willich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!