在 Specflow 场景大纲中自动生成示例范围
Auto generate ranges of Examples in Specflow Scenario Outline
在 Specflow 中,虽然我知道 Scenario Outline / Examples
功能,但我想知道是否有更优雅的方法来生成要在测试用例中使用的范围和组合?
例如,在 vanilla NUnit 中,我可以使用 TestCaseSource
or Theory
构建一个生成器,我可以将大量测试用例输入到测试中。
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 中更优雅地处理这个问题?
Cucumber 不是为进行此类测试而设计的。当你在做 cuking 时,每个场景的运行时间都会比编写良好的单元测试慢一个或多个数量级。因此生成大量场景会使您的测试套件无法使用。
Cucumber 就是使用场景创建一些基本的交互来驱动某些功能的开发。不是测试工具!
要对功能的某些方面进行详尽测试,请继续使用单元测试。
您可以一步从两个输入动态生成数字范围。
例如:
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);
}
在 Specflow 中,虽然我知道 Scenario Outline / Examples
功能,但我想知道是否有更优雅的方法来生成要在测试用例中使用的范围和组合?
例如,在 vanilla NUnit 中,我可以使用 TestCaseSource
or Theory
构建一个生成器,我可以将大量测试用例输入到测试中。
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 中更优雅地处理这个问题?
Cucumber 不是为进行此类测试而设计的。当你在做 cuking 时,每个场景的运行时间都会比编写良好的单元测试慢一个或多个数量级。因此生成大量场景会使您的测试套件无法使用。
Cucumber 就是使用场景创建一些基本的交互来驱动某些功能的开发。不是测试工具!
要对功能的某些方面进行详尽测试,请继续使用单元测试。
您可以一步从两个输入动态生成数字范围。
例如:
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);
}