关于 Maven 和运行单元测试的小问题。
我有一个非常基本的 Java SpringBoot 项目,有一些单元测试。
我非常简单地运行基本的 mvn clean install
。
出于某种原因,我不明白,我看到我所有的单元测试都运行了两次。
这里有一次:
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ project ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running MyTest
这里有一次:
[INFO] --- maven-surefire-plugin:2.22.2:test (integration-test) @ project ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running MyTest
这是我的专家:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<reportsDirectory>target/reports/junit</reportsDirectory>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>test</goal>
</goals>
<phase>integration-test</phase>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
请问我做错了什么,因为我不希望单元测试运行两次。
谢谢
答案 0 :(得分:2)
Surefire 和Failsafe 插件非常相似,主要区别在于它们运行的阶段。通常,Surefire 在test
阶段运行,Failsafe 在integration-test
阶段运行。 (默认配置让他们寻找略有不同的测试名称,例如 *Test
与 *IT
,以了解何时运行每个名称。)
您已明确告知 Surefire(单元测试插件)在集成测试期间运行。如果您不想要这个,只需删除整个 executions
节。我还强烈建议不要在测试失败时告诉 Maven 不要失败,并且更改报告目录很可能会混淆像 CI 这样在默认位置查找的工具。最好?完全取出插件定义并使用默认值。