【问题标题】:excluding Junit tests in maven [closed]不包括 Maven 中的 Junit 测试 [关闭]
【发布时间】:2017-01-14 03:39:47
【问题描述】:

我正在遵循以下说明: http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

我在我的pom.xml 中使用了排除,但它似乎不起作用。

这里是sn-p:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.19.1</version>
   <configuration>
      <argLine>${surefireArgLine}</argLine>
      <excludes>
         <exclude>${project.basedir}Test1.java</exclude>
      </excludes>
   </configuration>
</plugin>

【问题讨论】:

  • 当我用 mvn clean install 调用 mvn - 我必须给它一个特定的参数来包含“排除”吗?
  • 您是要排除所有测试还是仅排除文件名与某些正则表达式匹配的测试?

标签: java maven junit pom.xml maven-surefire-plugin


【解决方案1】:

如果您的测试类文件名为 Test1.java,Surefire 将自动将其作为测试类包含在内,因为它与通配符 **/Test*.java 匹配(包含的默认值之一,请参阅问题中的链接)。如果你想排除一个特定的测试,你需要改变你的&lt;exclude&gt;标签,因为它有两个问题。

(1) basedir 之间没有分隔符(例如${project.basedir}/Test1.java

(2) maven 中的类通常在一个目录结构中(如果有一个包而不是默认的......也应该有一个)。例如,${project.basedir}/src/test/java/com/foo/Test1.java

您的排除也应该使用通配符:

&lt;exclude&gt;**/Test1.java&lt;/exclude&gt;

如果您只想跳过所有测试,可以使用 skipTests 参数:

mvn clean install -DskipTests

【讨论】:

    【解决方案2】:

    ${project.basedir} 指的是module/project 的根文件夹。因此,如果Test1 位于根文件夹下,那么一旦您将排除语句更正为&lt;exclude&gt;${project.basedir}/Test1.java&lt;/exclude&gt;,它肯定会被surefire default-test 目标排除。

    如果根文件夹不是Test1所在的位置,则将Test1的位置映射到其他Maven POM变量here或使用通配符,如下:

    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <argLine>${surefireArgLine}</argLine>
                        <excludes>
                            <exclude>**/Test1.java</exclude>
                        </excludes>
                    </configuration>
                </plugin>
             </plugins>
    </build>
    

    或者更简洁的方法是 create the property for the tests to exclude 并将其用作 maven surefire 插件配置的一部分,如下所示:

    <properties>
            <exclude.tests>**/Test1.java</exclude.tests>
    </properties>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <argLine>${surefireArgLine}</argLine>
                        <excludes>
                            <exclude>${exclude.tests}</exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 2013-07-31
      • 1970-01-01
      • 2017-03-28
      • 2016-04-01
      相关资源
      最近更新 更多