【发布时间】:2020-07-29 17:18:10
【问题描述】:
我正在用 Java 编写并使用带有 Eclipse 的 cucumber 来搜索特定的单词。要求如下:
// 模式区分。
// 应该匹配以:test_splitter arg
开头的字符串 // 哪里应该匹配下面的正例
// 而不是负面的例子。注意:任何不在
中的字符串 // 可以接受或拒绝正面或负面的例子。
正面词是 (spill, Sponge, tap, rebuild) 负面词是 (si, egregious, Foul, Test, top, ta)
通过在Stepdefs.java文件中写正则表达式,这就是我写的
@When("^test_splitter (?=.*?\bspill\b)(?=.*?\bSponge\b)(?=.*?\btap\b)(?=.*?\brebuilld\b)((?!si|egregious|Foul|Test|ta).)*$")
public void test_splitter(String match) throws Throwable {
System.out.println("test_splitter true for: " + match);
}
现在,当我在 Test.feature 文件中为此方法编写测试(用 Gherkin 语言)时,测试失败,应该全部通过)
Scenario: test regular expressions that should pass
When test_splitter spill
When test_splitter Sponge
When test_splitter tap
When test_splitter rebuild
When test_splitter2 spill
When test_splitter2 Sponge
When test_splitter2 tap
When test_splitter2 rebuild
我的正则表达式有什么问题?如何以更好的方式编写正则表达式?
【问题讨论】:
-
我不确定你的正则表达式的目的是什么。它看起来真的很乱,但据我所知,您正在使用多个前瞻,这意味着它们应该同时匹配。因此,如果您说有
spill但没有Sponges,您的行将不匹配。 -
感谢您指出这一点。我对正则表达式相当陌生,但仍在苦苦挣扎。我现在已经像这样编辑了
"^test_splitter2 (?=.*?\bspill|Sponge|tap|rebuild\b)((?!si|egregious|Foul|Test|ta).)*$",但它在 Gradle 构建中失败了。
标签: java unit-testing testing cucumber gherkin