在Specflow我可以运行一个测试作为迈出了一步? [英] In Specflow can I run one test as a step of another?

查看:633
本文介绍了在Specflow我可以运行一个测试作为迈出了一步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR;如何创建一个specflow测试调用另一个测试它的第一步是什么?

 由于我已经有一个specflow测试
我想运行另一个测试远不止第一次测试更深
然后,我创建运行的第一个测试作为其第一步,第二次测试
我添加额外的步骤来测试深层次功能

对不起,specflow幽默那里点点。

比如我有一个已经创建了一个销售的测试:

 鉴于我想创建销售订单
我打开销售订单页面
我点击添加新订单按钮
然后创建一个新的销售订单

和我想要有另一个测试,测试增加一个销售行

和其他测试,测试完成交易

和另一个测试取消出售

和..等等

所有这些测试将开始用相同的前四个步骤的简单测试,它打破了DRY原则。 所以,我该怎么办它,这样第二次测试的第一步,只是运行的第一个测试?比如像:

 鉴于我已运行创建销售订单测试//这里它只是运行第一个测试
而我添加了一个销售订单行
然后,订单总额已更新

如果每个测试开始与同前四行,后来我意识到我需要改变简单的创建销售的测试,那么我也需要去发现和修复其他地方是重复的四行。

编辑:注意,这也应该能够在整个功能工作。例如简单的测试在上面的销售功能的定义。但我也将有一个信贷功能,这将需要为了每次创建一个出售给能够相信吗:

 鉴于我想赊销
我运行创建销售订单测试
我完成销售
我点击充值按钮
然后出售记


解决方案

如前所述,您可以使用此背景(这可能是在大多数情况下最好的选择),但你也可以创建调用其他的一步步骤。

  [装订]
公共类MySteps://步骤继承这个基类是至关重要的或低于所使用的方法将不可用
{
    [文(我已经创建了一个令)
    公共无效CreateOrder()
    {
         鉴于(我想创建销售订单);
         鉴于(我打开销售订单页);
         鉴于(我点击添加新订单按钮);
         然后(新的销售订单被创建);
    }
}

然后你就可以在你的场景中使用:

 场景:我补充另外一个销售
    鉴于我已经创建了一个订单
    当我添加一个销售订单行
    然后,订单总额已更新

这具有这种复合步骤可在方案中的任何地方,不使用只是作为起点的优点。这一步可在多个功能,如果你需要重复使用

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

解决方案

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

这篇关于在Specflow我可以运行一个测试作为迈出了一步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆