Maven:如何解决 Surefire 的 -Dtest 覆盖包含和排除问题?

发布于 2024-12-12 01:00:49 字数 2739 浏览 0 评论 0原文

我在一个配置文件中有两个肯定的执行 - 它们需要不同的配置。 当我运行 -Dtest=... 时,匹配的测试运行两次 - 每次执行一次。

如何让测试只运行一次? 或者更好的是,我怎样才能确保surefire遵循包含和排除? (一次执行将运行 0 次测试;我会使用 -DfailIfNoTest=false。)

    <profile>
        <id>clustering.integration.tests.profile</id>
        <activation>
            <property>
                <name>clustering.integration.tests</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <executions combine.children="append">

                        <!-- Disable default-test execution. -->
                        <execution>
                            <id>default-test</id>
                            <goals><goal>test</goal></goals>
                            <phase>none</phase>
                        </execution>

                        <!-- Single node clustering tests. -->
                        <execution>
                            <id>tests-clustering-single-node.surefire</id>
                            <phase>test</phase>
                            <goals><goal>test</goal></goals>
                            <configuration>
                                <includes>
                                    <include>org/jboss/as/test/clustering/single/**/*TestCase.java</include>
                                </includes>
                            </configuration>
                        </execution>

                        <!-- Multi node clustering tests. -->
                        <execution>
                            <id>tests-clustering-multi-node.surefire</id>
                            <phase>test</phase>
                            <goals><goal>test</goal></goals>
                            <configuration>
                                <includes>
                                    <include>org/jboss/as/test/clustering/cluster/**/*TestCase.java</include>
                                </includes>
                            </configuration>
                        </execution>

                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

I have two surefire executions in one profile - they need different config.
When I run -Dtest=..., the matching test is running twice - once for each execution.

How can I make the test run only once?
Or better, how can I make surefire follow includes and excludes?
(One execution would run 0 tests; I'd use -DfailIfNoTest=false.)

    <profile>
        <id>clustering.integration.tests.profile</id>
        <activation>
            <property>
                <name>clustering.integration.tests</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <executions combine.children="append">

                        <!-- Disable default-test execution. -->
                        <execution>
                            <id>default-test</id>
                            <goals><goal>test</goal></goals>
                            <phase>none</phase>
                        </execution>

                        <!-- Single node clustering tests. -->
                        <execution>
                            <id>tests-clustering-single-node.surefire</id>
                            <phase>test</phase>
                            <goals><goal>test</goal></goals>
                            <configuration>
                                <includes>
                                    <include>org/jboss/as/test/clustering/single/**/*TestCase.java</include>
                                </includes>
                            </configuration>
                        </execution>

                        <!-- Multi node clustering tests. -->
                        <execution>
                            <id>tests-clustering-multi-node.surefire</id>
                            <phase>test</phase>
                            <goals><goal>test</goal></goals>
                            <configuration>
                                <includes>
                                    <include>org/jboss/as/test/clustering/cluster/**/*TestCase.java</include>
                                </includes>
                            </configuration>
                        </execution>

                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花随流水 2024-12-19 01:00:49

这听起来像是对 maven-surefire-plugin 的滥用,因为你似乎有某种集成测试应该由 maven-failsafe-plugin 代替。通过使用它们,您将自动获得不同的单元测试和集成测试配置。 maven-surefire-plugin 旨在运行单元测试,而 maven-failsafe-plugin 旨在运行集成测试。此外,您的配置看起来需要不同类型的集成测试,换句话说,这意味着有多个集成测试模块。

 +-- pom.xml
 +-- module-1
 +-- module-2
 +-- integration-test-single-node
 +-- integration-test-multi-node
 ...

对于集成测试运行来说,这将是最好的选择。

This sounds like a misuse of the maven-surefire-plugin, cause you seemed to have some kind of integration-tests which should be done by the maven-failsafe-plugin instead. By using them you automatically have different configurations for unit- and integration tests. The maven-surefire-plugin is intended to run unit tests where as the maven-failsafe-plugin is intended to run integration tests. Furthermore you configuration looks like you need different kind of integration-tests which means in other word to have several integration-tests modules.

 +-- pom.xml
 +-- module-1
 +-- module-2
 +-- integration-test-single-node
 +-- integration-test-multi-node
 ...

And this will be the best thing to have different configurations for integration test runs.

不…忘初心 2024-12-19 01:00:49

maven-surefire-plugin 版本 2.12 修复了此问题。
http://jira.codehaus.org/browse/SUREFIRE-806

(请参阅 v2.12 中添加的更改:
引用 John Casey 对上面 JIRA 链接的评论:

此外,在存在多个测试执行块的情况下,为了避免在错误的块中运行指定的测试,现在尊重现有的包含/排除...指定的测试现在充当这些包含/的精炼过滤器排除。这意味着,在存在多个测试执行块的情况下,您无法再运行通常仅使用 -Dtest=... 无法运行的测试。

如果只有一个测试执行块,指定的测试应像以前一样覆盖包含内容。

Version 2.12 of the maven-surefire-plugin has a fix for this problem.
(See http://jira.codehaus.org/browse/SUREFIRE-806)

Describing the changes added to v2.12:
Quoted from John Casey's comment on the JIRA link above:

Also, in cases where there are multiple test execution blocks, to avoid running a specified test in the wrong block, the existing includes/excludes are now honored...the specified tests now act as a refining filter on these includes/excludes. This means that in cases where there are multiple test-execution blocks, you cannot run a test that wouldn't ordinarily be run by just using -Dtest=... any more.

In cases where there is only a single test-execution block, the specified tests should override the includes as before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文