如何在编写单元测试用例时将整数数组从 Cucumber 功能文件传递到步骤定义



我正在学习黄瓜进行单元测试,并试图为基数排序代码编写单元测试。但是我无法弄清楚如何提供整数数组作为特征文件中基数排序代码的输入。

我尝试提供以下输入:

Scenario: Sorting integer array using radix sort within the specified range
Given The nonnull integer array 10,25,0,1
When radix sort is performed over the range from 0 to 7
Then validate if the array is sorted

对于上述方案,Cucumber 需要以下代码正文:

@Given("The nonnull integer array {double}")
public void the_nonnull_integer_array(Double double1) {
// Write code here that turns the phrase above into concrete actions
throw new io.cucumber.java.PendingException();
}

如果我尝试输入为

Scenario: Sorting integer array using radix sort within the specified range
Given The nonnull integer array [10,25,0,1]
When radix sort is performed over the range from 0 to 7
Then validate if the array is sorted

那么 Cucumber 需要下面提到的代码正文:

@Given("The nonnull integer array [{double}]")
public void the_nonnull_integer_array(Double double1) {
// Write code here that turns the phrase above into concrete actions
throw new io.cucumber.java.PendingException();
}

我还尝试在引号内提供数组

Scenario: Sorting integer array using radix sort within the specified range
Given The nonnull integer array "[10,25,0,1]"
When radix sort is performed over the range from 0 to 7
Then validate if the array is sorted

但是黄瓜期望字符串作为输入

@Given("The nonnull integer array {string}")
public void the_nonnull_integer_array(String string) {
// Write code here that turns the phrase above into concrete actions
throw new io.cucumber.java.PendingException();
}

我尝试了各种其他方法,但没有成功。谁能提出更好的方法来处理这种测试场景?

有很多方法可以做到这一点!

Feature: Lists of integers
Scenario: Passing lists of integers
* With individual arguments 1, 2, 3
* With a custom parameter type [1, 2, 3]
* With a horizontal datatable
| 1 | 2 | 3 |
* With a vertical datatable
| 1 |
| 2 |
| 3 |
* With a horizontal list
| 1 | 2 | 3 |
* With a vertical list
| 1 |
| 2 |
| 3 |
package com.example;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.ParameterType;
import io.cucumber.java.Transpose;
import io.cucumber.java.en.Given;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class StepDefinitions {
@Given("With individual arguments {int}, {int}, {int}")
public void with_individual_arguments(Integer int1, Integer int2, Integer int3) {
assertEquals(asList(int1, int2, int3), asList(1, 2, 3));
}
@ParameterType("\[([0-9, ]*)\]")
public List<Integer> listOfIntegers(String integers) {
return Arrays.stream(integers.split(", ?"))
.map(Integer::parseInt)
.collect(Collectors.toList());
}
@Given("With a custom parameter type {listOfIntegers}")
public void with_a_custom_parameter_type(List<Integer> list) {
assertEquals(list, asList(1, 2, 3));
}
@Given("With a horizontal datatable")
public void with_a_horizontal_datatable(@Transpose DataTable table) {
assertEquals(table.column(0), asList("1", "2", "3"));
}
@Given("With a vertical datatable")
public void with_a_vertical_datatable(DataTable table) {
assertEquals(table.column(0), asList("1", "2", "3"));
}
@Given("With a horizontal list")
public void with_a_horizontal_list(@Transpose List<Integer> list) {
assertEquals(list, asList(1, 2, 3));
}
@Given("With a vertical list")
public void with_a_vertical_list(List<Integer> list) {
assertEquals(list, asList(1, 2, 3));
}
}

这对您不起作用的原因:

Given The nonnull integer array 10,25,0,1

黄瓜试图提供帮助。因为它看到10,25所以它认为你可能想要在步骤定义中使用双精度参数并建议这样做。如果在每个数字后添加一个空格,Cucumber 将建议使用整数的步骤定义。

值得一读:

  • https://cucumber.io/docs/cucumber/cucumber-expressions/
  • https://github.com/cucumber/cucumber-jvm/tree/master/java

最新更新