简体   繁体   English

如何在 Maven 中运行单个集成测试?

[英]How to run single integration test in maven?

I am trying run single integration test.我正在尝试运行单个集成测试。 I have aa lot of *IT class and I want to run only one test.我有很多 *IT 课程,我只想运行一个测试。 I try this :我试试这个:

mvn -Dit.test=XControllerIT verify

Am I doing wrong?我做错了吗? Is there another alternative to this?还有另一种选择吗? Maven is being used.正在使用 Maven。

There are two main options depending on your project setup:根据您的项目设置,有两个主要选项:

  • Integration Tests are run with a dedicated Failsafe plugin集成测试使用专用的故障安全插件运行
  • Integration Tests are run with a regular surefire plugin集成测试使用常规的surefire插件运行

If you have a failsafe plugin (and you actually should, its a recommended approach), then use the following snippet:如果你有一个故障安全插件(你实际上应该,这是一个推荐的方法),然后使用以下代码片段:

mvn -Dit.test=MySampleIntegrationTest failsafe:integration-test

If you're on surefire, then run:如果您确定无疑,请运行:

mvn -Dtest=MySampleUnitTest surefire:test

In both cases there is a direct plugin goal execution, bypassing the lifecycle like in your initial example (with mvn verify )在这两种情况下,都有一个直接的插件目标执行,绕过了初始示例中的生命周期(使用mvn verify

In maven it is possible to run the lifecycle, see Default Lifecycle Documentation for more information在 maven 中可以运行生命周期,请参阅默认生命周期文档以获取更多信息

Basically, the lifecycle is comprised of phases with plugins bound to each phase So when you run the mvn verify all the phases before verify will also run.基本上,生命周期由阶段组成,每个阶段都绑定了插件,因此当您运行mvn 时,verify之前的所有阶段也将运行。

As a consequence, the code will be compiled (compile phase with a maven compile plugin automatically attached to it will do the job), tests will run (surefire plugin), and so on.因此,代码将被编译(编译阶段会自动附加一个 maven 编译插件来完成这项工作),测试将运行(surefire 插件),等等。

If you don't have a compiled source code and code of tests, you can't use the presented approach because you have to compile the code first.如果您没有已编译的源代码和测试代码,则不能使用所提供的方法,因为您必须先编译代码。

However, if you already have everything compiled, it makes sense to run only the one test without recompilation of the code, and in this case, depending on the plugin you can use the suggested solution.但是,如果您已经编译了所有内容,那么只运行一个测试而不重新编译代码是有意义的,在这种情况下,您可以根据插件使用建议的解决方案。

Especially it can make sense for local debugging or for CI in some cases multi-step build setup (can be seen in fairly complicated projects)特别是它对于本地调试或在某些情况下的 CI 多步构建设置(可以在相当复杂的项目中看到)有意义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM