How to run JUnit tests by category in Maven?

前端 未结 8 1843
说谎
说谎 2020-11-28 21:59

Using JUnit 4.8 and the new @Category annotations, is there a way to choose a subset of categories to run with Maven\'s Surefire plugin?

For example I h

相关标签:
8条回答
  • 2020-11-28 22:34

    You can use

    mvn test -Dgroups="com.myapp.FastTests, com.myapp.SlowTests"
    

    But ensure that you configure properly the maven surefire plugin

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.11</version>
      <dependencies>
        <dependency>
          <groupId>org.apache.maven.surefire</groupId>
          <artifactId>surefire-junit47</artifactId>
          <version>2.12.2</version>
        </dependency>
      </dependencies>
    </plugin>
    

    See docs in: https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

    0 讨论(0)
  • 2020-11-28 22:35

    Maven has since been updated and can use categories.

    An example from the Surefire documentation:

    <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.11</version>
          <configuration>
            <groups>com.mycompany.SlowTests</groups>
          </configuration>
    </plugin>
    

    This will run any class with the annotation @Category(com.mycompany.SlowTests.class)

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