如何使用Maven surefire排除给定类别的所有JUnit4测试?

时间:2013-01-03 02:12:07

标签: junit maven-2 junit4 maven-surefire-plugin

我打算用@Category注释来注释我的一些JUnit4测试。然后我想在我的Maven版本中运行那些类别的测试。

我从Maven documentation看到可以指定一类测试运行。我想知道如何指定一类测试来运行。

谢谢!

1 个答案:

答案 0 :(得分:19)

您可以按照以下方式执行此操作:

您的pom.xml应包含以下设置。

<configuration>
   <excludes>
       <exclude>**/TestCircle.java</exclude>
        <exclude>**/TestSquare.java</exclude>
   </excludes>
</configuration>

如果您想要正则表达式支持,请使用

<excludes>
    <exclude>%regex[.*[Cat|Dog].*Test.*]</exclude>
</excludes>

http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

如果要使用类别注释,则需要执行以下操作:

@Category(com.myproject.annotations.Exclude)
@Test
public testFoo() {
   ....
}

在你的maven配置中,你可以有这样的东西

<configuration>
  <excludedGroups>com.myproject.annotations.Exclude</excludedGroups>
</configuration>