【问题标题】:Group test in JUnit and specify which to run in Maven [duplicate]在 JUnit 中进行分组测试并指定在 Maven 中运行哪个 [重复]
【发布时间】:2012-05-02 01:42:05
【问题描述】:

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

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

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

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

而 IntegrationTests 只是一个接口。

我是否可以在 maven 命令行中指定只运行此类测试?

非常感谢。

【问题讨论】:

标签: java maven junit


【解决方案1】:

为什么不依赖现有的约定?

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".

【讨论】:

【解决方案2】:

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

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

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

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

【讨论】:

  • 注意这是默认的,可以被Junit > 4.8中的自定义规则或注释类别覆盖
猜你喜欢
  • 1970-01-01
  • 2010-09-30
  • 1970-01-01
  • 2016-02-15
  • 2019-12-17
  • 2013-08-23
  • 2012-09-16
  • 1970-01-01
相关资源
最近更新 更多