阻止单元测试但允许在 Maven 中进行集成测试 [英] Prevent unit tests but allow integration tests in Maven
问题描述
我有一个 Maven 构建,在其中我使用 SureFire 插件运行一些单元测试,并使用 FailSafe 插件运行一些集成测试.我想要一种只运行 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.
在 pom 中添加不同的配置文件或任何东西对我来说不是一个好的解决方案,因为它是一个多模块构建,我不想编辑每个模块的 pom.
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.
有 skip.tests
和 maven.test.skip
和 skipTests
停止所有测试,和skipITs
,仅停止故障安全插件.
There are skip.tests
and maven.test.skip
and skipTests
which stop all tests, and skipITs
, which stops only the failsafe plugin.
那么,是否有像 skipITs
这样的 Maven 命令行标志,但具有onlyITs"的功能?
So, is there a command-line flag for Maven like skipITs
, but instead with the functionality of "onlyITs"?
推荐答案
我发现只跳过surefire测试的最简单方法是配置surefire(但不是failsafe),如下所示:
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>
这允许你运行 mvn verify -Dskip.surefire.tests
并且只有surefire,而不是failsafe,测试将被跳过;它还将运行所有其他必要的阶段,包括预集成和后集成,并且还将运行 verify
目标,如果您的集成实际上使您的 maven 构建失败测试失败.
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.
请注意,这重新定义了用于指定应跳过测试的属性,因此如果您提供规范的-DskipTests=true
,则surefire 将忽略它但failsafe 会尊重它,这可能是意外的,特别是如果您已经拥有指定该标志的现有构建/用户.一个简单的解决方法似乎是将 skip.surefire.tests
默认设置为 pom 的
部分中的 skipTests
值:
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>
如果需要,您可以为故障安全提供一个名为 skip.failsafe.tests
的类似参数,但我认为没有必要 - 因为单元测试通常在较早的阶段运行,并且如果我想运行单元测试而不是集成测试,我会运行 test
阶段而不是 verify
阶段.您的体验可能会有所不同!
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!
这些skip.(surefire|failsafe).tests
属性可能应该集成到surefire/failsafe 代码本身中,但我不确定它会在多大程度上违反它们正是相同的插件,除了 1 个微小的差异"精神.
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屋!