如何为 Surefire Maven 插件指定 JUnit 自定义运行器?
假设我有一个 junit 自定义类加载器,它从文本文件读取测试数据并在运行时创建和运行测试。跑步者不使用测试类。
现在我想使用 surefire
maven 插件运行它。也就是说,我想将运行程序指定为 pom.xml
中 surefire
插件“执行”的参数。
我可以这样做吗?
Suppose I have a junit
custom class loader, which reads the test data from a text file and create and run tests in runtime. The runner uses no test class.
Now I would like to run it with surefire
maven plugin. That is, I would like to specify the runner as a parameter of the the surefire
plugin "execution" in the pom.xml
.
Can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不。 据我所知,没有办法在 maven-surefire-plugin 中指定
Runner
类。但是,您应该能够创建一个测试类并使用 @RunWith(YourRunner.class) 让它与您的自定义运行器一起运行。我认为这是因为
Runner
的预期用例是基于每个测试,而不是项目级别。您可以拥有一个混合使用各种 Runner 的项目,例如,有些是基于 Spring 的,有些是并发的,有些使用 JUnit3 运行,有些使用 JUnit4 等。No. As far as I know, there is no way of specifying a
Runner
class in the maven-surefire-plugin. However, you should be able to create a single Test-Class and use the@RunWith(YourRunner.class)
to let it run with your custom runner.I think that is because the intended use case for
Runner
s is on a per-test basis, not on a project-level. You can have a project with mixed uses of various Runners, e.g. some are Spring-based, some are concurrent, some run with JUnit3, some with JUnit4 etc.根据您想要实现的目标,您也许能够通过使用自定义
RunListener
来全局影响测试行为。以下是如何使用 Maven Surefire 插件配置它: http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html#Using_custom_listeners_and_reporters(我对类似问题发布了相同的回复,即:全局设置 JUnit 运行器而不是 @RunWith)
Depending on what you want to achieve, you might be able to influence the test behavior globally by using a custom
RunListener
. Here is how to configure it with the Maven Surefire plugin: http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html#Using_custom_listeners_and_reporters(I posted the same response to a similar question, namely: Globally setting a JUnit runner instead of @RunWith)
在我的项目中,我使用插件 exec-maven-plugin 解决了相同的任务
我有自定义的跑步者,它使用特殊参数运行 junit 测试。我使用 maven 命令执行我的运行程序: mvn test -Dparam1=value1 -Dparam2=value2 -Dparam3=value3
在 pom.xml 中:
In my project i resolve same task with plugin exec-maven-plugin
I have custom Runner, which run junit test with special parameters. I execute my runner with maven command: mvn test -Dparam1=value1 -Dparam2=value2 -Dparam3=value3
In pom.xml: