用于跳过单元测试执行的maven profile参数

时间:2012-11-06 21:04:59

标签: unit-testing maven command pom.xml

我的应用程序中有一些单元测试。我需要跳过他们的实践。 在pom.xml中,我已经定义了test是否具有特定的类 - 然后跳过它。

<profile>
  <id>skip-unit-tests</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.11</version>
        <configuration>
          <excludes>
            <exclude>**/*.class</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

所以,如果我做package -Pskip-unit-tests一切正常,但如果我做clean compile -Pskip-unit-tests test那么maven会执行它们。上一个命令有什么问题?

3 个答案:

答案 0 :(得分:2)

选项skipTests完成工作:

mvn clean install -DskipTests

这解决了所有测试依赖项,编译测试,但阻止了它们的执行。 如果您还要跳过他们的编译(不是一个好主意!),请使用:

mvn clean install -Dmaven.test.skip

顺便说一句。没有选项可以避免解决测试依赖性问题。以上选项在surefire插件中实现,并在解析deps后执行。

有关在maven-surefire-plugin的文档中跳过测试的更多信息。

答案 1 :(得分:0)

根据maven生命周期(参见here),测试在编译之后才运行,但是在包之前运行。所以我怀疑你的配置实际上并没有按照你的想法进行。

答案 2 :(得分:0)

我测试了你的个人资料,并没有任何问题。

没有个人资料的正常运行:

iMac:Q13257987 maba$ mvn package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Q13257987-1.0-SNAPSHOT 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ Q13257987 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ Q13257987 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ Q13257987 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/maba/Development/stackoverflow/java/Q13257987/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ Q13257987 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ Q13257987 ---
[INFO] Surefire report directory: /Users/maba/Development/stackoverflow/java/Q13257987/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.stackoverflow.OrderTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.04 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.3.1:jar (default-jar) @ Q13257987 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.655s
[INFO] Finished at: Tue Nov 06 22:51:52 CET 2012
[INFO] Final Memory: 4M/81M
[INFO] ------------------------------------------------------------------------

结果:运行了一次测试


使用个人资料运行包:

iMac:Q13257987 maba$ mvn package -Pskip-unit-tests
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Q13257987-1.0-SNAPSHOT 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ Q13257987 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ Q13257987 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ Q13257987 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/maba/Development/stackoverflow/java/Q13257987/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ Q13257987 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.11:test (default-test) @ Q13257987 ---
[INFO] Surefire report directory: /Users/maba/Development/stackoverflow/java/Q13257987/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.3.1:jar (default-jar) @ Q13257987 ---
[INFO] Building jar: /Users/maba/Development/stackoverflow/java/Q13257987/target/Q13257987-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.609s
[INFO] Finished at: Tue Nov 06 22:49:24 CET 2012
[INFO] Final Memory: 4M/81M
[INFO] ------------------------------------------------------------------------

结果:未运行测试


使用profile:

运行clean compile test
iMac:Q13257987 maba$ mvn clean compile -Pskip-unit-tests test
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Q13257987-1.0-SNAPSHOT 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ Q13257987 ---
[INFO] Deleting /Users/maba/Development/stackoverflow/java/Q13257987/target
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ Q13257987 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ Q13257987 ---
[INFO] Compiling 1 source file to /Users/maba/Development/stackoverflow/java/Q13257987/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ Q13257987 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ Q13257987 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ Q13257987 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/maba/Development/stackoverflow/java/Q13257987/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ Q13257987 ---
[INFO] Compiling 1 source file to /Users/maba/Development/stackoverflow/java/Q13257987/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.11:test (default-test) @ Q13257987 ---
[INFO] Surefire report directory: /Users/maba/Development/stackoverflow/java/Q13257987/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.676s
[INFO] Finished at: Tue Nov 06 22:54:44 CET 2012
[INFO] Final Memory: 8M/81M
[INFO] ------------------------------------------------------------------------

结果:未运行测试


所以总结你的个人资料设置。

那就是说,我认为你应该使用mvn package -DskipTests代替这是跳过所有测试的最简单方法。

另一方面,如果您想跳过一些测试,可以使用您的个人资料设置,但我建议您使用JUnit Categories。可以在maven-surefire-plugin配置中直接选择类别。