我发现不可能制作 Surefire <exclude>
标签来工作。我已经尝试过多次,并查看了 stackoverflow 和其他网站上的大量问题,以及 Surefire 的文档,所以我只能在这里提问。我的情况是我希望能够通过 mvn package 命令控制运行哪些测试。我有大量的测试,我经常遇到这样的情况:我需要运行所有测试,然后逐步从需要运行的列表中删除一些测试(因为它们被验证是正确的)。
例如,假设我有 1 到 20 个单元测试。第一次执行 mvn package 时,我想运行所有 20 个单元测试。然后,我将获取测试日志,查看执行的任何测试是否没有任何问题,然后我想从下一次执行中删除这些测试。第二次执行时,我会想要运行测试 1 到 12、13 到 15、17 到 20。单独运行这些测试或在每个 mvn CLI 命令中排除它们是很麻烦的。
我的首选解决方案是在 pom.xml 上保留排除测试的列表,至少在我经历此测试阶段时暂时保留。现在,我的 pom.xml 如下所示:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<packaging>jar</packaging>
<version>...</version>
<name>...</name>
<url>...</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<repositories>
<repository>
...
</repository>
</repositories>
<dependencies>
...
</dependencies>
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugin</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<enableAssertions>true</enableAssertions>
<excludes>
<exclude>**/CleanUpCodeMaatWrapperTest.java</exclude>
<exclude>**/AcdcWrapperTest.java</exclude>
<exclude>**/CSourceToDepsBuilderWrapperTest.java</exclude>
<exclude>**/JavaSourceToDepsBuilderWrapperTest.java</exclude>
<exclude>**/PipeExtractorWrapperTest.java</exclude>
<exclude>**/BatchCloneFinderWrapperTest.java</exclude>
<exclude>**/InferencerTest.java</exclude>
<exclude>**/TopicModelGeneratorTest.java</exclude>
</excludes>
</configuration>
</plugin>
...
</plugins>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<excludes>
<exclude>**/*</exclude>
</excludes>
</testResource>
</testResources>
</build>
</project>
我已将其尽可能完整,以便不会遗漏任何重要信息。如果需要 pom.xml 中的更多信息来解决这个问题,我将很乐意提供。目前,这个pom.xml,带有命令mvn package
,导致执行我的 src/test
中的所有测试目录,包括上面列出的目录。指定完整路径,例如 src/test/AcdcWrapperTest.java
,给了我相同的结果。目前,每次执行所有测试可能需要一个小时以上,通过排除其中可能有 3 或 4 个我目前知道是正确的测试(通过执行它们数十次),我可以将测试时间缩短一半的。我不想永久删除这些测试,或者必须重命名它们以便 Surefire 跳过它们,因为这也很麻烦。任何帮助将不胜感激,因为几个月来我一直在努力解决这个问题。
最佳答案
没有org.apache.maven.plugin这样的groupId
,但有org.apache.maven.plugins,请参阅官方docs .
更改此配置即可工作:
...
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
...
关于java - Maven Surefire 适当使用Exclude,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61709940/