maven ::在多模块项目中仅运行单个测试

时间:2020-12-20 00:00:26

Is there any way to provide some command-line argument in order to skip all tests but one on some module? So I will not need to change pom.xml every time I will need to run another test?

有没有办法提供一些命令行参数,以跳过所有测试,但在某些模块上有一个?所以每次我需要运行另一个测试时我都不需要更改pom.xml吗?

For example, I want to create build configuration on TeamCity, and provide command-line arguments to run only single test in some module. Next time I will need to change it and run another test, and so on.

例如,我想在TeamCity上创建构建配置,并提供命令行参数以在某个模块中仅运行单个测试。下次我需要更改它并运行另一个测试,依此类推。

Perhaps it is not how CI is intended to be used, but still.

也许不是如何使用CI,但仍然如此。

3 个解决方案

#1


59  

I assume you've read the docs about running a single test under surefire? What they don't tell you is how to do that in a sub-module:

我假设您已经阅读了关于在确定的情况下运行单个测试的文档?他们没有告诉你的是如何在子模块中做到这一点:

mvn test -Dtest=testname -pl subproject

Where subproject is the project containing that test. From the mvn man page:

子项目是包含该测试的项目。从mvn手册页:

-pl,--projects arg Comma-delimited list of specified reactor projects to build instead of all projects. A project can be specified by [groupId]:artifactId or by its relative path.

-pl, - projects arg以逗号分隔的指定反应器项目列表,而不是所有项目。项目可以由[groupId]:artifactId或其相对路径指定。

#2


12  

In case the module to be tested depends on other projects, solution works by changing commands as:

如果要测试的模块依赖于其他项目,则解决方案通过将命令更改为:

mvn test -DfailIfNoTests=false -Dtest=testname -pl subproject

Reference: http://www.automatethebox.com/2015/12/some-helpful-commands-to-build-multi.html

参考:http://www.automatethebox.com/2015/12/some-helpful-commands-to-build-multi.html

#3


9  

Other answers I see are not fully complete, for projects that depend on other sub-modules to be built. One option is to run mvn install to have the required jars to be installed into ~/.m2/..., but that option is not very "clean".

对于依赖于要构建的其他子模块的项目,我看到的其他答案并不完全。一个选项是运行mvn install以将所需的jar安装到〜/ .m2 / ...中,但该选项不是很“干净”。

Following command will build the sub-modules, and run only the test class that is specified. This is to be run at parent module level. Also, no need to specify sub-module name.

以下命令将构建子模块,并仅运行指定的测试类。这将在父模块级别运行。此外,无需指定子模块名称。

mvn test -DfailIfNoTests=false -Dtest={test_class_name} -am

As an aside, this can also be mvn clean test -Dfa...... It is my personal habit to always run clean when running tests.

顺便说一句,这也可以是mvn clean test -Dfa ......在运行测试时总是运行干净是我个人的习惯。

References..
-am will make all the other sub-modules.
-DfailIfNoTests=false does not fail the entire process since we are not intending to run tests in other modules.
-pl option is not needed since -am is already building everything

引用.. -am将生成所有其他子模块。 -DfailIfNoTests = false并不会使整个过程失败,因为我们不打算在其他模块中运行测试。不需要-pl选项,因为-am已经构建了所有内容

#1


59  

I assume you've read the docs about running a single test under surefire? What they don't tell you is how to do that in a sub-module:

我假设您已经阅读了关于在确定的情况下运行单个测试的文档?他们没有告诉你的是如何在子模块中做到这一点:

mvn test -Dtest=testname -pl subproject

Where subproject is the project containing that test. From the mvn man page:

子项目是包含该测试的项目。从mvn手册页:

-pl,--projects arg Comma-delimited list of specified reactor projects to build instead of all projects. A project can be specified by [groupId]:artifactId or by its relative path.

-pl, - projects arg以逗号分隔的指定反应器项目列表,而不是所有项目。项目可以由[groupId]:artifactId或其相对路径指定。

#2


12  

In case the module to be tested depends on other projects, solution works by changing commands as:

如果要测试的模块依赖于其他项目,则解决方案通过将命令更改为:

mvn test -DfailIfNoTests=false -Dtest=testname -pl subproject

Reference: http://www.automatethebox.com/2015/12/some-helpful-commands-to-build-multi.html

参考:http://www.automatethebox.com/2015/12/some-helpful-commands-to-build-multi.html

#3


9  

Other answers I see are not fully complete, for projects that depend on other sub-modules to be built. One option is to run mvn install to have the required jars to be installed into ~/.m2/..., but that option is not very "clean".

对于依赖于要构建的其他子模块的项目,我看到的其他答案并不完全。一个选项是运行mvn install以将所需的jar安装到〜/ .m2 / ...中,但该选项不是很“干净”。

Following command will build the sub-modules, and run only the test class that is specified. This is to be run at parent module level. Also, no need to specify sub-module name.

以下命令将构建子模块,并仅运行指定的测试类。这将在父模块级别运行。此外,无需指定子模块名称。

mvn test -DfailIfNoTests=false -Dtest={test_class_name} -am

As an aside, this can also be mvn clean test -Dfa...... It is my personal habit to always run clean when running tests.

顺便说一句,这也可以是mvn clean test -Dfa ......在运行测试时总是运行干净是我个人的习惯。

References..
-am will make all the other sub-modules.
-DfailIfNoTests=false does not fail the entire process since we are not intending to run tests in other modules.
-pl option is not needed since -am is already building everything

引用.. -am将生成所有其他子模块。 -DfailIfNoTests = false并不会使整个过程失败,因为我们不打算在其他模块中运行测试。不需要-pl选项,因为-am已经构建了所有内容