JK.*_*JK. 15 c# automated-tests unit-testing dry specflow
TL; DR; 如何创建一个将另一个测试调用为第一步的specflow测试?
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
Run Code Online (Sandbox Code Playgroud)
对不起,那里有一点点幽默幽默.
例如,我已经有一个测试可以创建销售:
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
Run Code Online (Sandbox Code Playgroud)
我希望有另一个测试来测试添加销售线
另一个测试完成销售的测试
另一项取消销售的测试
等等
所有这些测试都将从与简单测试相同的前四个步骤开始,这将打破DRY原则. 那么我怎么能这样做,以便第二次测试的第一步只运行第一次测试?例如:
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
Run Code Online (Sandbox Code Playgroud)
如果每个测试都以相同的前四行开始,后来我意识到我需要更改简单的创建销售测试,那么我还需要去寻找并修复重复这四行的其他地方.
编辑:请注意,这也应该能够跨功能.例如,上面的简单测试在销售功能中定义.但我也有一个学分功能,这需要每次创建一个销售,以便能够信用它:
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
Run Code Online (Sandbox Code Playgroud)
Sam*_*der 17
如前所述,您可以使用背景(在大多数情况下这可能是最佳选择),但您也可以创建一个调用其他步骤的步骤.
[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");
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在场景中使用它:
Scenario: I add another sale
Given I have created an order
When I add a sales order line
Then the order total is updated
Run Code Online (Sandbox Code Playgroud)
这具有以下优点:该复合步骤可以在场景中的任何地方使用,而不仅仅作为起点.如果需要,可以在多个功能中重复使用此步骤
使用背景:
Background:
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
Scenario: I add another sale
When I add a sales order line
Then the order total is updated
Scenario: I add cancel a sale
When I cancel a sale
Then the order total is updated to 0
etc.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10046 次 |
最近记录: |