防止单元测试,但允许在Maven中进行集成测试 [英] Prevent unit tests but allow integration tests in Maven

查看:177
本文介绍了防止单元测试,但允许在Maven中进行集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven构建,其中使用SureFire插件运行一些单元测试,并使用FailSafe插件运行一些集成测试.我想只运行FailSafe插件的测试.

在pom中添加不同的配置文件或其他内容对我来说不是一个好的解决方案,因为它是多模块构建,并且我不想编辑每个模块的pom.

skip.testsmaven.test.skipskipTests停止所有 测试,并且

我发现,仅跳过surefire测试的最简单方法是按以下方式配置surefire(但不设置故障安全性):

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.14</version>
    <configuration>
        <!-- skips surefire tests without skipping failsafe tests.
                 Property value seems to magically default to false -->
        <skipTests>${skip.surefire.tests}</skipTests>
    </configuration>
</plugin>

这允许您运行mvn verify -Dskip.surefire.tests,并且只跳过surefire(不是故障安全)测试;它还将运行所有其他必要阶段,包括集成前和集成后,并且还将运行verify目标,如果集成测试失败,该目标实际上会使您的Maven构建失败.

请注意,这重新定义了用于指定应跳过测试的属性,因此,如果提供规范的-DskipTests=true,则surefire将忽略它,但故障保护将尊重它,这可能是意外的,特别是如果您已经有内部版本/用户已经指定了该标志.一个简单的解决方法似乎是将skip.surefire.tests的默认值设置为pom的<properties>部分中的skipTests值:

<properties>
    <skip.surefire.tests>${skipTests}</skip.surefire.tests>
</properties>

如果需要,可以为故障安全提供一个名为skip.failsafe.tests的类似参数,但是我没有必要这样做-因为单元测试通常在较早的阶段运行,并且如果我想运行单元测试而不是集成测试,我将运行test阶段而不是verify阶段.您的经历可能会有所不同!

这些skip.(surefire|failsafe).tests属性可能应该集成到surefire/failsafe代码本身中,但是我不确定它会违反它们是完全相同的插件,只是有一点点区别"的精神.

I've a Maven build in which I use the SureFire plugin to run some unit tests, and the FailSafe plugin to run some integration tests. I would like a way to run just the FailSafe plugin's tests.

It's not a good solution for me to add different profiles or anything in the pom, because it's a multimodule build and I don't want to have to edit every module's pom.

There are skip.tests and maven.test.skip and skipTests which stop all tests, and skipITs, which stops only the failsafe plugin.

So, is there a command-line flag for Maven like skipITs, but instead with the functionality of "onlyITs"?

解决方案

I found the simplest way to skip only surefire tests is to configure surefire (but not failsafe) as follows:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.14</version>
    <configuration>
        <!-- skips surefire tests without skipping failsafe tests.
                 Property value seems to magically default to false -->
        <skipTests>${skip.surefire.tests}</skipTests>
    </configuration>
</plugin>

This allows you to run mvn verify -Dskip.surefire.tests and only surefire, not failsafe, tests will be skipped; it will also run all other necessary phases including pre-integration and post-integration, and will also run the verify goal which is required to actually fail your maven build if your integration tests fail.

Note that this redefines the property used to specify that tests should be skipped, so if you supply the canonical -DskipTests=true, surefire will ignore it but failsafe will respect it, which may be unexpected, especially if you have existing builds/users specifying that flag already. A simple workaround seems to be to default skip.surefire.tests to the value of skipTests in your <properties> section of the pom:

<properties>
    <skip.surefire.tests>${skipTests}</skip.surefire.tests>
</properties>

If you need to, you could provide an analagous parameter called skip.failsafe.tests for failsafe, however I haven't found it necessary - because unit tests usually run in an earlier phase, and if I want to run unit tests but not integration tests, I would run the test phase instead of the verify phase. Your experiences may vary!

These skip.(surefire|failsafe).tests properties should probably be integrated into surefire/failsafe code itself, but I'm not sure how much it would violate the "they're exactly the same plugin except for 1 tiny difference" ethos.

这篇关于防止单元测试,但允许在Maven中进行集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆