传递字符串列表作为Cucumber参数

huangapple go评论99阅读模式
英文:

Passing list of strings as Cucumber parameter

问题

我正在将旧的info.cukes版本更新到最新的io.cucumber(v6.8.1),但我的一些测试现在出现了java.lang.IllegalArgumentException at BuiltInParameterTransformer.java:114错误。

在新的cucumber中,我如何将字符串列表作为参数传递?

我阅读了这个答案,但对我没用。您可以在下面找到一个示例以重现我的问题:

  Scenario Outline: 测试字符串列表
    When 进行伟大的测试 <first> <second> <third>
    Examples:
      | first    | second                 | third            |
      | "简单" | "参数列表"         | "另一个简单" |
      | "简单" | "列表,OF,参数"       | "另一个简单" |
    @When("^进行伟大的测试 \"(.*?)\" \"(.*?)\" \"(.*?)\"$")
    public void 进行伟大的测试(String string, List<String> string2, String string3) {
        System.out.println(string);
        System.out.println(string2); // 我想在这里获得 ["参数列表"] 和 ["列表", "OF", "参数"]
        System.out.println(string3);
    }
英文:

I'm updating my cucumber version from old info.cukes to latest io.cucumber (v6.8.1) and some of my tests are failing now with java.lang.IllegalArgumentException at BuiltInParameterTransformer.java:114

How can I pass list of strings as parameter in new cucumber?

I read this answer but it doesn't work for me. You can find below an example to reproduce my problem:

  Scenario Outline: Testing lists of strings
    When Great test &lt;first&gt; &lt;second&gt; &lt;third&gt;
    Examples:
      | first    | second                 | third            |
      | &quot;simple&quot; | &quot;listOfParams&quot;         | &quot;another simple&quot; |
      | &quot;simple&quot; | &quot;list,OF,params&quot;       | &quot;another simple&quot; |
    @When(&quot;^Great test \&quot;(.*?)\&quot; \&quot;(.*?)\&quot; \&quot;(.*?)\&quot;$&quot;)
    public void great_test(String string, List&lt;String&gt; string2, String string3) {
        System.out.println(string);
        System.out.println(string2); // I want to get here [&quot;listOfParams&quot;] and [&quot;list&quot;, &quot;OF&quot;, &quot;params&quot;]
        System.out.println(string3);
    }

答案1

得分: 7

// 定义字符串列表的参数类型
@ParameterType("(?:[^,]*)(?:,\\s?[^,]*)*")
public List<String> listOfStrings(String arg){
    return Arrays.asList(arg.split(",\\s?"));
}

// 使用Cucumber表达式的步骤定义
@Given("a list of strings {listOfStrings}")
public void a_list_of_strings_cucumber_expression(List<String> list) {
    System.out.println(list);
}

// 使用正则表达式的步骤定义
@Given("^a list of strings \"((?:[^,]*)(?:,\\s?[^,]*)*)\"$")
public void a_list_of_strings_regex(List<String> list) {
    System.out.println(list);
}

// 注意不能使用 .*,因为这是匿名参数类型的保留字
// 当使用匿名参数类型时,Cucumber将根据参数的类型猜测你想要做什么
// 这对于Boolean、String、Numbers等类型有效,因为它们有一种明确的表示方法
// 然而,有许多方法可以表示字符串列表,所以Cucumber无法猜测
// 以下代码会报错
@ParameterType(value = ".*", preferForRegexMatch = true)
public List<String> listOfStrings(String arg){
    return Arrays.asList(arg.split(",\\s?"));
}
英文:

You can define a parameter type for your list of strings:

    @ParameterType(&quot;(?:[^,]*)(?:,\\s?[^,]*)*&quot;)
    public List&lt;String&gt; listOfStrings(String arg){
        return Arrays.asList(arg.split(&quot;,\\s?&quot;));
    }

Then you can either use this parameter type in a step definition with a Cucumber Expression:

    @Given(&quot;a list of strings \&quot;{listOfStrings}\&quot;&quot;)
    public void a_list_of_strings_cucumber_expression(List&lt;String&gt; list) {
        System.out.println(list);
    }

Or you can use the exact regular expression wrapped in a capture group:

    @Given(&quot;^a list of strings \&quot;((?:[^,]*)(?:,\\s?[^,]*)*)\&quot;$&quot;)
    public void a_list_of_strings_regex(List&lt;String&gt; list) {
        System.out.println(list);
    }

Note that you can't use .* because this is reserved for the anonymous parameter type. When using the anonymous parameter type Cucumber will guess what you want to do based on the type of the argument.

And this works for Boolean, String, Numbers, ect because they have a single way to writ them down. There are however many ways to write down a list of strings so Cucumber won't guess.

    // This will complain
    @ParameterType(value = &quot;.*&quot;, preferForRegexMatch = true)
    public List&lt;String&gt; listOfStrings(String arg){
        return Arrays.asList(arg.split(&quot;,\\s?&quot;));
    }

huangapple
  • 本文由 发表于 2020年10月15日 14:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64366164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定