在JUnit中进行组测试并指定在Maven中运行的测试

时间:2012-04-20 13:01:34

标签: java maven junit

  

可能重复:
  How to run junit tests by category in maven

我有一个关于在JUnit中对测试进行分组的问题。

我有一个用

注释的测试类
@Category(IntegrationTests.class)
public class TestClass { ... }

IntegrationTests只是一个界面。

无论如何,我可以在maven命令行中指定只运行此类测试吗?

非常感谢。

2 个答案:

答案 0 :(得分:4)

单元测试和集成测试之间的区别很简单naming convention

**/Test*.java
**/*Test.java
**/*TestCase.java

将被视为单元测试。基于maven-failsafe-plugin的集成测试将通过不同的命名约定来识别:

**/IT*.java
**/*IT.java
**/*ITCase.java

答案 1 :(得分:2)

为什么不依赖现有的惯例?

mvn clean test将通过surefire运行单元测试。 mvn clean verify将通过故障安全

运行集成测试

您可以使用命名约定或注释来强制选择。

thisIsAUnitTest.java will be executed by surefire (mvn test)
thisClassIsAnIT.java will be executed by failsafe (mvn verify)

怎么样?!

单位测试的SureFire

默认情况下,Surefire插件会自动包含具有以下通配符模式的所有测试类:

"**/Test*.java" - includes all of its subdirectories and all java filenames that start with "Test".
"**/*Test.java" - includes all of its subdirectories and all java filenames that end with "Test".
"**/*TestCase.java" - includes all of its subdirectories and all java filenames that end with "TestCase".

整合测试的失败保护

默认情况下,Failsafe插件会自动包含具有以下通配符模式的所有测试类:

"**/IT*.java" - includes all of its subdirectories and all java filenames that start with "IT".
"**/*IT.java" - includes all of its subdirectories and all java filenames that end with "IT".
"**/*ITCase.java" - includes all of its subdirectories and all java filenames that end with "ITCase".