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

Dar*_*hia 9 junit unit-testing cucumber cucumber-junit cucumber-java

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

我尝试提供以下输入:

  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

Run Code Online (Sandbox Code Playgroud)

对于上述场景,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();
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试输入为

    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
Run Code Online (Sandbox Code Playgroud)

那么 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();
}

Run Code Online (Sandbox Code Playgroud)

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

  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

Run Code Online (Sandbox Code Playgroud)

但是 cucumber 需要 String 作为输入

@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();
}
Run Code Online (Sandbox Code Playgroud)

我尝试了各种其他方法但没有成功。谁能建议处理此类测试场景的更好方法?

mpk*_*nje 12

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

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 |
Run Code Online (Sandbox Code Playgroud)
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 |
Run Code Online (Sandbox Code Playgroud)

这对你不起作用的原因是:

    Given The nonnull integer array 10,25,0,1
Run Code Online (Sandbox Code Playgroud)

黄瓜正在努力提供帮助。因为它认为10,25您可能需要在步骤定义中使用双参数并建议这样做。如果您在每个数字后添加空格,Cucumber 将建议使用整数的步骤定义。

值得一读: