使用 Maven,如何运行特定测试?
我的项目中有数千个单元测试,我想选择其中一个或几个从命令行运行。执行此操作的命令是什么?
I have thousands of unit tests in my project, and I'd like to choose one or a couple of them to run from the command line. What's the command to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过将
-Dtest=
标志传递给 Maven 来运行类中的所有测试:从 Surefire 2.8 开始,您还可以运行单独的测试,例如方法
testA
在您的单元测试中,使用相同的标志:通过名称模式或名称列表运行多个测试的更多示例可以在 Maven Surefire 文档 >运行单个测试。
You can run all the tests in a class, by passing the
-Dtest=<class>
flag to Maven:Since Surefire 2.8, you can also run an individual test, say a method
testA
within your unit tests, using the same flag:More examples for running multiple tests, by name pattern or name lists, can be found in the Maven Surefire documentation > Running a Single Test.
请阅读 maven Surefire 插件手册。基本上你可以执行以下操作:
只运行以 PerformanceTest 结尾的所有测试类。
Please read this piece of the maven surefire plugin manual. Basically you can do the following:
Which only runs all the test classes ending in PerformanceTest.
对上述答案+1,和...确保您位于您尝试运行的测试所在模块的同一目录中。
我遇到了这个问题,因为我通常在多模块项目中工作并从项目的根目录运行
mvn clean install
...但对于这个用例,您需要cd< /code> 进入您要运行的测试的模块。
+1 to the above answers, and... make sure you're in the same directory of the module where the test you're trying to run lives.
I ran into this issue, because I normally work in multi-module projects and run
mvn clean install
from the root of the project... but for this use case, you need tocd
into the module of the test you're trying to run.