gpt4 book ai didi

java - Maven 不会从父项目或子项目运行测试

转载 作者:行者123 更新时间:2023-11-28 19:52:09 24 4
gpt4 key购买 nike

我有一个多模块项目。每个项目都有自己的单元测试,与父项目相同。当我使用 mvn test 时,没有运行任何测试,并且目标文件夹不包含任何测试类。父项目甚至不创建目标文件夹

结构如下:

|-module1-> pom.xml
|-module2-> pom.xml
|-module3-> pom.xml
|-src/main
|-src/test/java/MyTest.java
|-pom.xml

请参阅下面的 poms(我省略了标准 pom 样板文件)

父 pom.xml

<groupId>com.tests</groupId>
<artifactId>unit-tests</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>RELEASE</version>
</dependency>
<dependencies>

子 pom 有:

<parent>
<groupId>com.tests</groupId>
<artifactId>unit-tests</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

每当我运行 mvn clean test 时,都不会运行任何测试,并且在控制台中我会得到如下信息:

[INFO] ------------------------------------------------------------------------
[INFO] Building unit-tests 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------

顺便说一句,父项目没有任何源代码,只有测试

最佳答案

您正在使用包装 pom。使用这种打包方式,您只能运行少量绑定(bind)到阶段的目标(例如 installdeploy)。为了在父级中运行测试,您必须明确说明您的意图,如下所示:

$mvn clean compiler:testCompile surefire:test

[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.tests.unit_tests.TestModules
Test1!
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.016 s <<< FAILURE! - in com.tests.unit_tests.TestModules
[ERROR] test1(com.tests.unit_tests.TestModules) Time elapsed: 0.003 s <<< FAILURE!
java.lang.AssertionError
at com.tests.unit_tests.TestModules.test1(TestModules.java:11)

[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] TestModules.test1:11
[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] unit-tests ......................................... FAILURE [ 1.262 s]
[INFO] module1 ............................................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.473 s
[INFO] Finished at: 2017-12-02T11:04:50-02:00
[INFO] Final Memory: 16M/207M
[INFO] ------------------------------------------------------------------------

这样你就告诉 maven 你想编译(生成你的目标)并在聚合器(有父 pom)中运行你的测试。否则 maven 将只运行模块中的测试。

$mvn test

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] unit-tests
[INFO] module1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building unit-tests 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building module1 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ module1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ module1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ module1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ module1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ module1 ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] unit-tests ......................................... SUCCESS [ 0.002 s]
[INFO] module1 ............................................ SUCCESS [ 0.424 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.504 s
[INFO] Finished at: 2017-12-02T11:06:24-02:00
[INFO] Final Memory: 9M/241M
[INFO] ------------------------------------------------------------------------

我知道这很难读,但你应该检查一下: http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Packaging

For example, a project that is purely metadata (packaging value is pom) only binds goals to the install and deploy phases (for a complete list of goal-to-build-phase bindings of some of the packaging types, refer to the Lifecycle Reference).

编辑

正如 eis 指出的那样。尽管有可能做到这一点,但 maven 让它很难做到,因为默认情况下您不应该这样做。您的单元测试应该在他们正在测试的模块中。分散它们不是一个好习惯。

The answer is: you can do it, yes. But you shouldn't! There are somethings in life that you can do, but shouldn't...

如果我们谈论的是集成测试,那将是另一回事。那是另一回事。

干杯!

关于java - Maven 不会从父项目或子项目运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47607842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com