1

可能重复:
如何在 Maven 中按类别运行 junit 测试

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

我有一个带有注释的测试类

@Category(IntegrationTests.class)
public class TestClass { ... }

而IntegrationTests 只是一个接口。

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

非常感谢。

4

2 回答 2

4

单元测试和集成测试之间的区别很简单,命名约定

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

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

**/IT*.java
**/*IT.java
**/*ITCase.java
于 2012-04-20T13:45:12.553 回答
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 Plugin 将自动包含所有具有以下通配符模式的测试类:

"**/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".
于 2012-04-20T13:08:32.733 回答