A.M*_*oud 5 java regex cucumber-junit cucumber-java
我正在用java编写并使用带有eclipse的cucumber来搜索具有以下要求的类似IP的字符串
应该接受由句点分隔的四位数序列,其中数字序列定义如下:任何单个数字,任何两位数字字符(如果第一个字符是非零),一个一个后跟一个零,一个或两个后跟任何数字
通过在 Stepdefs.java 文件中编写适当的正则表达式,这就是我写的
@When("^test_ip_address ((?:(\\d)|(1[0-2]\\d)|([1-9]\\d))\\.){3}(?:(\\d)|([1-9]\\d)|(1[0-2]\\d))$")
public void test_ip_address(String arg1) throws Throwable {
System.out.println("test_ip_address true for: " + arg1);
}
Run Code Online (Sandbox Code Playgroud)
现在,当我在 Test.feature 文件中为此方法编写测试(用 Gherkin 语言)时,第一个测试总是失败,测试(应该全部通过)
When test_ip_address 1.2.3.4
When test_ip_address 123.34.76.109
When test_ip_address 123.34.76.109
When test_ip_address 105.22.33.44
Run Code Online (Sandbox Code Playgroud)
这不是价值问题,就像当我重新排序这些测试时,它总是第一个失败,即使我在另一个测试中使用了完全相同的值,它通过了!这是我得到的错误
cucumber.runtime.CucumberException: Arity mismatch: Step Definition 'skeleton.Stepdefs.test_ip_address(String) in file:(file path..)' with pattern [^test_ip_address ((?:(\d)|(1[0-2]\d)|([1-9]\d))\.){3}(?:(\d)|([1-9]\d)|(1[0-2]\d))$] is declared with 1 parameters. However, the gherkin step has 7 arguments [3., 3, null, null, 4, null, null]
Run Code Online (Sandbox Code Playgroud)
我搜索了错误,当测试中的参数数量与方法中的参数数量不同时,即使我使用 (?:) 将字符串作为一个参数传递,我也不知道在哪里这7个论点来自!也不是错误的原因
cucumber.runtime.CucumberException: Arity mismatch: Step Definition 'skeleton.Stepdefs.test_ip_address(String) in file:(file path..)' with pattern [^test_ip_address ((?:(\d)|(1[0-2]\d)|([1-9]\d))\.){3}(?:(\d)|([1-9]\d)|(1[0-2]\d))$] is declared with 1 parameters. However, the gherkin step has 7 arguments [3., 3, null, null, 4, null, null]
Run Code Online (Sandbox Code Playgroud)
这个错误是由于正则表达式有 7 个组。捕获7个参数。和方法
public void test_ip_address(String arg1) throws Throwable
Run Code Online (Sandbox Code Playgroud)
仅声明 1 个参数 ( arg1)。
要仅捕获一个参数,请使用非捕获组以避免将不必要的组捕获为方法参数。
表达式应如下所示:
@When("^test_ip_address ((?:(?:(?:\\d)|(?:1[0-2]\\d)|(?:[1-9]\\d))\\.){3}(?:(?:\\d)|(?:[1-9]\\d)|(?:1[0-2]\\d)))$")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
847 次 |
| 最近记录: |