【问题标题】:Auto generate ranges of Examples in Specflow Scenario Outline在 Specflow Scenario Outline 中自动生成示例范围
【发布时间】:2018-01-30 11:12:08
【问题描述】:

在 Specflow 中,虽然我知道 Scenario Outline / Examples 功能,但我想知道是否有更优雅的方法来生成要在测试用例中使用的范围和组合?

例如,在 vanilla NUnit 中,我可以使用 TestCaseSourceTheory 构建生成器,我可以将大量测试用例输入到测试中。

private static readonly IEnumerable<int> Numbers = Enumerable.Range(1, 50);

[TestCaseSource(nameof(Numbers))]
public void TestFoo(int number)
{
    // Test goes here.
}

目前,在我的测试中,我需要在 Examples 中手动创建所有排列,这可能难以阅读,并且可能容易出错。

Scenario Outline: Count things
    Given I'm playing a game of counting
    When I count to <number>
    Then the highest number I should have counted to should be <number>

    Examples:
        | number|
        | 1    | 
        | 2    | 
        | 3    | 
        ...
        | 50    | 

我真正想做的是

Examples:
    | number| : Range 1 to 20

更好的是,创建两组的笛卡尔积,即:

Examples:
    | number1| : Range 1 to 20
    | number2| : Range 5 to 10

// i.e. 20 x 5 = 100 combinations of the tuple (number1, number2)

我是否可以在 Specflow 中更优雅地处理这个问题?

【问题讨论】:

  • 您尝试过同时使用CombinatorialAttributeValueSourceAttribute 吗?
  • 道歉 - 我不应该标记 NUnit - 但你是对的,我正在尝试从 SpecFlow(即 Cucumber 语言)中寻找是否有等效的方式来利用自动化数据底层测试框架的驱动功能(如 NUnit 的 Theory、Combinatorial、TestCaseSource(带有生成的数据集)。我已经更新了标签。

标签: cucumber specflow


【解决方案1】:

您可以将两个输入作为一个步骤动态生成数字范围。

例如:

Scenario Outline: Count things
    Given I'm playing a game of counting
    When I count from <First Number> to <Last Number>
    Then the highest number I should have counted to should be <Last Number>

    Examples:
        | Description        | First Number| Last Number |
        | Count from 1 to 20 | 1           | 20          |
        | Count from 5 to 10 | 5           | 10          |

然后When 步骤可以定义为:

[When("I count from (\d+) to (\d+)")]
public void ICountFromFirstNumberToLastNumber(int firstNumber, int lastNumber)
{
    IEnumerable<int> numbers = Enumerable.Range(firstNumber, lastNumber);
    this.countResult = this.Count(numbers);
}

【讨论】:

    【解决方案2】:

    Cucumber 不是为进行此类测试而设计的。当您进行 cuking 时,每个场景的运行时间都会比编写良好的单元测试慢一个或多个数量级。因此,生成大量场景会使您的测试套件无法使用。

    Cucumber 就是使用场景创建一些基本交互来推动某些功能的开发。它不是测试工具!

    要彻底测试功能的某些方面,请继续使用单元测试。

    【讨论】:

      猜你喜欢
      • 2016-08-11
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      相关资源
      最近更新 更多