Java Maven 故障安全不执行测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16387679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven fail-safe not executing tests
提问by danny-v
I've combed StackOverflow and many other sites, have found many other related posts and have followed all said suggestions, but in the end, failsafe is skipping my tests.
我梳理了 StackOverflow 和许多其他网站,发现了许多其他相关帖子,并遵循了所有所说的建议,但最终,故障安全跳过了我的测试。
My JUnit test is located here:
myModule/src/main/test/java/ClientAccessIT.java
我的 JUnit 测试位于:
myModule/src/main/test/java/ClientAccessIT.java
I am skipping surefirebecause there are no unit tests in this module:
我跳过了surefire,因为这个模块中没有单元测试:
<!-- POM snippet -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
And I'm trying to run integration tests with failsafe:
我正在尝试使用故障安全运行集成测试:
<!-- POM snippet -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>run-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
However, when I run mvn verify
I see this:
但是,当我运行时,mvn verify
我看到:
[INFO] --- maven-failsafe-plugin:2.14.1:integration-test (run-tests) @ rest-services-test ---
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
I spent the last 4 1/2 hours scouring, any help would be appreciated. The only other thing that may be worth mentioning is that I have Cargosetting up and tearing down a Tomcat container. Does anybody see the glaring problem?
我花了最后 4 1/2 小时进行搜索,任何帮助将不胜感激。唯一值得一提的另一件事是我让Cargo设置和拆除了一个 Tomcat 容器。有人看到明显的问题吗?
采纳答案by Keppil
You need to rename your test class.
您需要重命名您的测试类。
You can find the names the plugin looks for by default in the documentation, as pointed out by @acdcjunior:
正如@acdcjunior 所指出的,您可以在文档中找到插件默认查找的名称:
By default, the Failsafe Plugin will automatically include all test classes with the following wildcard patterns:
- "
**/IT*.java
" - includes all of its subdirectories and all java filenames that start with "IT".- "
**/*IT.java
" - includes all of its subdirectories and all java filenames that end with "IT".- "
**/*ITCase.java
" - includes all of its subdirectories and all java filenames that end with "ITCase".
默认情况下,Failsafe 插件将自动包含所有具有以下通配符模式的测试类:
- "
**/IT*.java
" - 包括其所有子目录和所有以“IT”开头的 java 文件名。- "
**/*IT.java
" - 包括它的所有子目录和所有以“IT”结尾的 java 文件名。- "
**/*ITCase.java
" - 包括它的所有子目录和所有以“ITCase”结尾的 java 文件名。
回答by Raymond
I had a similar problem. If there aren't any test classes compiled to target/test-classes then check your pom file and ensure that the packaging isn't 'pom'.
我有一个类似的问题。如果没有任何测试类编译为目标/测试类,请检查您的 pom 文件并确保包装不是“pom”。
回答by conapart3
I also had a similar problem but needed the packaging element to be "pom". Make sure your test source files are being compiled and added to the .../target/test-classes folder using the maven-compiler-plugin:
我也有类似的问题,但需要包装元素是“pom”。确保您的测试源文件正在编译并使用 maven-compiler-plugin 添加到 .../target/test-classes 文件夹中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<executions>
<execution>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
回答by barryku
For multi-module projects, the child project needs to have the plugin declared as followed,
对于多模块项目,子项目需要声明插件如下,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
version inherits from parent pom. Without the above defined, the plugin in parent pom alone will not work.
版本继承自父 pom。如果没有上面的定义,单独在父 pom 中的插件将无法工作。
回答by pro-tester
Your tests are not in the default test sources directory src/test/java. See:
您的测试不在默认测试源目录 src/test/java 中。看:
https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
myModule/src/main/test/java/ClientAccessIT.java
myModule/src/main/test/java/ClientAccessIT.java
should be:
应该:
myModule/src/test/java/ClientAccessIT.java
myModule/src/test/java/ClientAccessIT.java
You could also update your pom file (if you really wanted tests to live in main) to include:
您还可以更新您的 pom 文件(如果您真的希望测试存在于 main 中)以包括:
<build>
<testSources>
<testSource>
<directory>src/main/test</directory>
</testSource>
</testSources>
</build>
回答by Danny Bullis
I was having the same issue and tried a few of the suggested options here. I am developing a Spring Boot application using JUnit 5. Unfortunately, none of my integration tests (located in src/test/java
and suffixed with *TestIT.java
) were getting picked up, regardless of the advertising of the pluginto do so. I tried downgrading to 2.18.1, 2.19.1and tried both removing the <executions>
and <configuration>
sections to attempt to use even the plugin's default functionality, but no luck.
我遇到了同样的问题,并在这里尝试了一些建议的选项。我正在使用 JUnit 5 开发 Spring Boot 应用程序。不幸的是,无论插件的广告如何,我的集成测试(位于src/test/java
并以*TestIT.java
为后缀)都没有被接受。我尝试降级到 2.18.1、2.19.1并尝试删除和部分以尝试使用插件的默认功能,但没有运气。<executions>
<configuration>
I finally changed the version to the latest (as of the time of writing) to 2.22.0, and viola. Here is the configuration I added to the <build><plugins>
section of my pom. And even better, I don't need to add executions that define the integration-test
and verify
goals that you see most people include; failsafe executes these by default. I've included my surefire plugin configuration as well, since I am using it to execute unit tests during the test
phase of the maven lifecycle.
我终于将版本更改为最新的(截至撰写本文时)到 2.22.0 和中提琴。这是我添加到<build><plugins>
我的 pom.xml 部分的配置。更好的是,我不需要添加定义您看到的大多数人包括的目标integration-test
和verify
目标的执行;默认情况下,故障安全会执行这些。我也包含了我的 surefire 插件配置,因为我test
在 maven 生命周期的阶段使用它来执行单元测试。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version><!--$NO-MVN-MAN-VER$-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.0</version><!--$NO-MVN-MAN-VER$ -->
</plugin>
And just for good measure, to tell surefire and failsafe to use JUnit 5, I am including this in my <dependencies>
section:
为了更好地衡量,为了告诉surefire和failsafe使用JUnit 5,我在我的<dependencies>
部分中包含了这个:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
Eclipse displays a warning (yellow squiggly underline) under the version tag, hence the inclusion of the <!--$NO-MVN-MAN-VER$ -->
error. I'm sure there's a better way to deal with this, but it works for me in this case.
Eclipse 在版本标记下显示警告(黄色波浪线下划线),因此包含<!--$NO-MVN-MAN-VER$ -->
错误。我确信有更好的方法来处理这个问题,但在这种情况下它对我有用。
To speed up testing this configuration, and to avoid having to go through allof the earlier lifecycle phases before test
, integration-test
orverify
phases, I used the following commands to quickly validate:
要加快测试此配置,避免不必经过所有之前的早期生命周期阶段的test
,integration-test
或verify
阶段,我用下面的命令来快速验证:
mvn failsafe:integration-test
(runs only integration tests)mvn surefire:test
(runs only unit tests)- (I obviously run the entire lifecycle when it counts)
mvn failsafe:integration-test
(仅运行集成测试)mvn surefire:test
(仅运行单元测试)- (我显然在重要时运行了整个生命周期)
Hope this helps someone. Cheers!
希望这可以帮助某人。干杯!
回答by horizon7
I had somewhat similar issue. My Integration tests were not being picked up. My project is a multi module spring boot project. And my mistake was that I added maven-failsafe-plugin in the parent POM. After fiddling around with many options I placed the failsafe-plugin in the module pom where all my integration tests were present which solved my problem.
我有一些类似的问题。我的集成测试没有被接受。我的项目是一个多模块 spring boot 项目。我的错误是我在父 POM 中添加了 maven-failsafe-plugin。在摆弄了许多选项之后,我将故障安全插件放在模块 pom 中,其中存在我所有的集成测试,这解决了我的问题。
回答by Gonen I
For me it was a missing executions section
对我来说,这是一个缺失的处决部分
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
The failsafe with TestNG documentation at https://maven.apache.org/surefire/maven-failsafe-plugin/examples/testng.htmlhad shown a pom without it, and it didn't work.
https://maven.apache.org/surefire/maven-failsafe-plugin/examples/testng.html 上的带有 TestNG 文档的故障保护 显示了一个没有它的 pom,但它不起作用。
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
[...]
</plugins>
Adding the missing executions section shown above solved the problem.
添加上面显示的缺失执行部分解决了问题。
回答by cliffberg
For me, surefire:verify
did not run any tests.
I had to use surefire:integration-test
.
对我来说,surefire:verify
没有运行任何测试。我不得不使用surefire:integration-test
.