Jenkins中Cucumber测试重复执行及指定Runner文件运行咨询
Absolutely! You can definitely run only specific Cucumber runner files in Jenkins instead of having all tests execute twice. Here's how to configure it properly:
1. 核心原理:利用Maven Surefire插件指定测试类
Cucumber runner classes are essentially JUnit test classes (annotated with @RunWith(Cucumber.class)), so we can use Maven's Surefire plugin to target exactly the runners we want to execute.
2. 在Jenkins的「Goal and options」中配置命令
In your Jenkins job configuration, navigate to the Build section (for Maven projects, it's the "Goals and options" field). Use the following syntax to specify your target runner(s):
运行单个特定Runner
Use the test goal with the -Dtest parameter, providing the full qualified class name (package + class name) of your runner:
clean test -Dtest=com.yourpackage.runner.YourTargetRunner
Example: If your runner is in com.ecommerce.runner and named CheckoutFlowRunner, the command would be:
clean test -Dtest=com.ecommerce.runner.CheckoutFlowRunner
运行多个特定Runners
Separate multiple runner class names with commas (still use full qualified names if they're in different packages):
clean test -Dtest=com.ecommerce.runner.LoginRunner,com.ecommerce.runner.CheckoutFlowRunner
If all your runners are in the same package, you can even use wildcard patterns (though explicit names are safer to avoid unintended runs):
clean test -Dtest=com.ecommerce.runner.*Runner
3. 解决「全部测试执行两次」的问题
The double execution issue usually stems from one of these scenarios:
- Your Jenkins job has duplicate build steps that trigger the test phase
- Your
pom.xmlhas conflicting configurations (e.g., both Surefire and Failsafe plugins configured to run tests, or the same plugin executing twice) - You're using a default Maven command like
clean installwhich might trigger tests in multiple phases
To fix this:
- Check your Jenkins job's build steps to ensure only one Maven command is running tests
- Review your
pom.xmlto confirm only the Surefire plugin is configured to execute your Cucumber runners, with no duplicate executions
注意事项
- Make sure your runner classes are properly annotated with
@RunWith(Cucumber.class)so the Surefire plugin recognizes them as test classes - Class names are case-sensitive, so double-check the spelling and capitalization
- If you omit the package name, ensure the plugin's
testClassesDirectoryorincludesconfiguration inpom.xmlcovers the correct package
内容的提问来源于stack exchange,提问作者K.Krishnakanth

