Is there a way to tell surefire to skip tests in a certain package?

前端 未结 3 585
慢半拍i
慢半拍i 2020-11-29 07:11

Something like the following.

I would like a way to skip my dao tests in surefire. Trying to avoid overhead of defining Suites.

With CI I\'d like to have on

相关标签:
3条回答
  • 2020-11-29 07:37

    Sure, no problem:

    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <version>2.6</version>
       <configuration>
          <excludes>
             <!-- classes that include the name Dao -->
             <exclude>**/*Dao*.java</exclude>
             <!-- classes in a package whose last segment is named dao -->
             <exclude>**/dao/*.java</exclude>
          </excludes>
       </configuration>
    </plugin>
    

    Reference:

    • Maven Surefire Plugin > Inclusions and Exclusions of Tests

    (The excludes can not be configured via command line, so if you want to turn this behavior on conditionally, you will have to define a profile and activate that on the command line)

    0 讨论(0)
  • 2020-11-29 07:49

    Let me extend Sean's answer. This is what you set in pom.xml:

    <properties>
      <exclude.tests>nothing-to-exclude</exclude.tests>
    </properties>
    <profiles>
      <profile>
        <id>fast</id>
        <properties>
          <exclude.tests>**/*Dao*.java</exclude.tests>
        </properties>
      </profile>
    </profiles>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <excludes>
         <exclude>${exclude.tests}</exclude>
        </excludes>
      </configuration>
    </plugin>
    

    Then in CI you start them like this:

    mvn -Pfast test
    

    That's it.

    0 讨论(0)
  • 2020-11-29 07:53

    It is possible to exclude tests using the commandline; using ! to exclude.

    Note: I'm not sure but possibly needs 2.19.1 or later version of surefire to work.

    Examples:

    This will not run TestHCatLoaderEncryption

    mvn install '-Dtest=!TestHCatLoaderEncryption'
    

    Exclude a package:

    mvn install '-Dtest=!org.apache.hadoop.**'
    

    This can be combined with positive filters as well. The following will run 0 tests:

    mvn install '-Dtest=Test*CatLoaderEncryption,!TestHCatLoaderEncryption'
    

    See the Maven Surefire docs.

    0 讨论(0)
提交回复
热议问题