Maven: 通过命令行忽略指定Unit test (maven-surefire-plugin exclude specific unit test by command line)

maven-surefire-plugin 是在 maven 生命周期中,test 阶段的内置插件,用来专门做 exclude 等控制用的。

相信大家都很清楚 maven-surefire-plugin 在 pom.xml 里面配置的用法,通过配置来 exclude、include 指定的 unit test(支持通配符 * )。简单展示:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <configuration>
          <excludes>
            <exclude>**/TestCircle.java</exclude>
            <exclude>**/TestSquare.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

但是现在出现了一个问题:在我的 jenkins 进行自动部署过程中,分开了 uat deploy 和 prd deploy。 在 uat deploy 时候,需要通过外部传参的方式进行 unit test 的控制。翻看了一下 maven official document。发现 maven-surefire-plugin 也是可以通过 command line 的方式来控制的。
now, let me show you the code:

# Exclude one test class, by using the explanation mark (!)
mvn test -Dtest=!LegacyTest
# Exclude one test method
mvn verify -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn verify -Dtest=!LegacyTest#testFoo+testBar
# Exclude a package with a wildcard (*)
mvn test -Dtest=!com.mycompany.app.Legacy*

(reference: https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-maven/)

当然也可以直接在 install phase 进行 exclude:

eg.
mvn install -Dtest=Class,!Class#function

更多的介绍可以直接看 official document:
[https://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Charlven

你的鼓励将是我最大的动力哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值