Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

c# - In Specflow can I run one test as a step of another?

TL;DR; How can I create a specflow test that calls another test as its first step?

Given I already have one specflow test
And I want to run another test that goes deeper than the first test  
Then I create a second test that runs the first test as its first step
And I add additional steps to test the deeper functionality

Sorry, little bit of specflow humor there.

eg I have a test that creates a sale already:

Given I want to create a sales order
And I open the sales order page
And I click the add new order button
Then a new sales order is created

And I want to have another test that tests adding a sales line

And another test that tests completing the sale

And another test that cancels the sale

And .. so on

All of those tests would start with the same first four steps as the simple test, which breaks the DRY principle. So how can I do it so that the first step of the 2nd test just runs the first test? eg something like:

Given I have run the create sales order test  // right here it just runs the first test
And I add a sales order line
Then the order total is updated

If every test starts off with the same first four lines, and later on I realize that I need to change the simple create sale test, then I will also need to go and find and fix everywhere else that repeats those four lines.

EDIT: Note that this should also be able to work across features. eg The simple test above is defined in the sales feature. But I would also have a credits feature, and that would require creating a sale each time in order to be able to credit it:

Given I want to credit a sale
And I run the create sales order test
And I complete the the sale
And I click the credit button
Then the sale is credited
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As noted already you can use a background for this (and that's probably the best option in most situations), but you can also create a step which calls the other steps.

[Binding]
public class MySteps: Steps //Inheriting this base class is vital or the methods used below won't be available
{
    [Given("I have created an order")]
    public void CreateOrder()
    {
         Given("I want to create a sales order");
         Given("I open the sales order page");
         Given("I click the add new order button");
         Then("a new sales order is created");
    }
}

which you can then use in your scenario:

Scenario: I add another sale
    Given I have created an order
    When I add a sales order line
    Then the order total is updated

This has the advantage that this composite step can be used anywhere in the scenario and not just as a starting point. This step can then be reused across multiple features if you need


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...