英文:
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 <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);
}
答案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("(?:[^,]*)(?:,\\s?[^,]*)*")
public List<String> listOfStrings(String arg){
return Arrays.asList(arg.split(",\\s?"));
}
Then you can either use this parameter type in a step definition with a Cucumber Expression:
@Given("a list of strings \"{listOfStrings}\"")
public void a_list_of_strings_cucumber_expression(List<String> list) {
System.out.println(list);
}
Or you can use the exact regular expression wrapped in a capture group:
@Given("^a list of strings \"((?:[^,]*)(?:,\\s?[^,]*)*)\"$")
public void a_list_of_strings_regex(List<String> 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 = ".*", preferForRegexMatch = true)
public List<String> listOfStrings(String arg){
return Arrays.asList(arg.split(",\\s?"));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论