将字符串列表作为 Cucumber 参数传递
Passing list of strings as Cucumber parameter
我正在将我的黄瓜版本从旧的 info.cukes
更新到最新的 io.cucumber
(v6.8.1),我的一些测试现在失败了 java.lang.IllegalArgumentException at BuiltInParameterTransformer.java:114
如何在新黄瓜中将字符串列表作为参数传递?
我读了 this answer 但它对我不起作用。您可以在下面找到重现我的问题的示例:
Scenario Outline: Testing lists of strings
When Great test <first> <second> <third>
Examples:
| first | second | third |
| "simple" | "listOfParams" | "another simple" |
| "simple" | "list,OF,params" | "another simple" |
@When("^Great test \"(.*?)\" \"(.*?)\" \"(.*?)\"$")
public void great_test(String string, List<String> string2, String string3) {
System.out.println(string);
System.out.println(string2); // I want to get here ["listOfParams"] and ["list", "OF", "params"]
System.out.println(string3);
}
您可以为字符串列表定义参数类型:
@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 就不会猜测了。
// This will complain
@ParameterType(value = ".*", preferForRegexMatch = true)
public List<String> listOfStrings(String arg){
return Arrays.asList(arg.split(",\s?"));
}
我正在将我的黄瓜版本从旧的 info.cukes
更新到最新的 io.cucumber
(v6.8.1),我的一些测试现在失败了 java.lang.IllegalArgumentException at BuiltInParameterTransformer.java:114
如何在新黄瓜中将字符串列表作为参数传递?
我读了 this answer 但它对我不起作用。您可以在下面找到重现我的问题的示例:
Scenario Outline: Testing lists of strings
When Great test <first> <second> <third>
Examples:
| first | second | third |
| "simple" | "listOfParams" | "another simple" |
| "simple" | "list,OF,params" | "another simple" |
@When("^Great test \"(.*?)\" \"(.*?)\" \"(.*?)\"$")
public void great_test(String string, List<String> string2, String string3) {
System.out.println(string);
System.out.println(string2); // I want to get here ["listOfParams"] and ["list", "OF", "params"]
System.out.println(string3);
}
您可以为字符串列表定义参数类型:
@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 就不会猜测了。
// This will complain
@ParameterType(value = ".*", preferForRegexMatch = true)
public List<String> listOfStrings(String arg){
return Arrays.asList(arg.split(",\s?"));
}