Does it make sense to have specflow functions depending on other functions?

I want to write an acceptance test line by line

given the first test has run
when I do this new test
then this new test passes

      

This is because the first test will leave the data in a valid state for the next test

Can I do this in Specflow?

+1


source to share


2 answers


yes you can do it in specflow, but with a little caveat. this approach will not run one script and then run another, it will run the first script by itself, then the dependent script will run the first script and then the second script. Their order cannot be deterministic. We avoid this problem by using the @ignore

first script after writing the dependent script. Since the second script always runs the first script, it doesn't matter that its @ignore

d anyway , since any failure in the first script will cause the second script to fail.

What we have done to achieve this is as follows:

Scenario: Some base scenario
    Given some condition
    When some action
    Then some result

      

and then in another function:

Scenario: Some dependant scenario
    Given some base scenario has happened
    And some other condition
    When some other action
    Then some other result

      

The key is in the definition of the step Given some base scenario has happened



if you define this step like this:

[Given("some base scenario has happened")]
public void SomeBaseScenarioHasHappened()
{
    Given("some condition");
    When("some action");
    Then("some result");
}

      

you need to make sure your class containing the step definitions is sourced from the base class Steps

in specflow in order to access the methods Given

, When

and Then

:

[Binding]
public class MyStepClass: Steps

      

you can find more details on this in the specflow wiki

+1


source


As Sam says you can do this with SpecFlow, however I would question if this is good for your tests.

If you look at Sam's link on the specflow wiki , you can see that their example consists of Given

multiple s.ThisGiven

makes sense to me as it Given

gets the test into a very specific state so that assertions can work against it. It also has no side effects, as it theoretically Given

cannot fail.



However, when we come to create Given

from When

, it indicates the possibility that the test is in the wrong state before we can assert that everything is correct. I'll agree that if you just compose Given

with the steps of another Scenario

it will lessen since the original one Scenario

will fail, and so you can resolve that first, but you still have to find this failing test.Imagine building your tests over a few years and end up with a few thousand, composed in this way. One simple change to the baseline scenario and thousands of tests fail, making it much harder to find the real problem.

+2


source







All Articles