首页 > 解决方案 > Java Maven Cucumber 如何从命令行正确地重新运行测试?

问题描述

嗨,我正在使用我目前正在使用 Cucumber 6.8.1 版,我正在尝试弄清楚如何解决重新运行失败的测试。我已经破解了我自己的解决方案,但感觉不正确。为了

maven clean install

为了检测测试运行者,我将 Test 附加到他们的名字中:

@RunWith(Cucumber.class)
@CucumberOptions(plugin = { "de.monochromata.cucumber.report.PrettyReports:target/cucumber", "rerun:target/rerun.txt" })
public class CucumberRunnerTest {

}

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "@target/rerun.txt",
        plugin = {"pretty", "html:target/site/cucumber-pretty",
                "json:target/cucumber.json"}
)
public class RerunningTest {

}

现在这可行,但它似乎有点hacky。因为像这样的命令

    mvn clean install -Dcucumber.glue="cucumber" \                            
   -Dcucumber.plugin="de.monochromata.cucumber.report.PrettyReports:target/cucumber" \
   -Dcucumber.plugin="rerun:target/rerun.txt"

应该工作,但不工作。构建成功,但没有运行测试。就像 Dcucumber 被 Maven 忽略了一样。但是,在将“测试”附加到跑步者之后,然后运行上面的命令会使用两个跑步者,因为它们被视为感觉错误的测试。

所以我的问题是如何让 Dcucumber 正常工作?

标签: javamavencucumber

解决方案


如果您不命名您的测试类*Test,那么 Surefire 将无法识别它。命名约定明智,尝试使用RunCucumberTest。它不是太丑陋。

https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

测试的包含和排除

默认情况下,Surefire 插件会自动包含所有具有以下通配符模式的测试类:

   "**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test".
   "**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test".
   "**/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests".
   "**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".

如果测试类不遵循任何这些命名约定,则配置 Surefire 插件并指定要包含的测试。


推荐阅读