使用 Maven Surefire 运行包含在依赖项 jar 中的 JUnit 测试 [英] Run JUnit Tests contained in dependency jar using Maven Surefire

查看:33
本文介绍了使用 Maven Surefire 运行包含在依赖项 jar 中的 JUnit 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 maven 存储库中有一个 jar,其中包含 junit 测试,它应该在不同的项目中运行,因为它能够检查项目并测试它的某些功能.不幸的是,surefire 不会选择包含在 jar 中的测试,因为此功能请求显示.

I have a jar in my maven repository that contains junit tests, which should be run in different projects, because it is able to inspect the project and test for certain features of it. Unforunately surefire doesn't pick up tests that are contained in a jar, as this Feature Request shows.

在功能请求中,他们建议解压缩 jar,然后由 surefire 执行.

In the feature request they propose to unpack the jar to be then executed by surefire.

我使用 maven-dependency-plugin 成功解压了 jar,但是包含的测试并没有被执行.这是我配置 maven-dependency-plugin 来解压我的 jar 的方式:

I successfully unpacked the jar using the maven-dependency-plugin, but the contained tests are not executed anyway. This is how I configured the maven-dependency-plugin to unpack my jar:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>process-test-classes</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>de.mwx.test</groupId>
                        <artifactId>selenium-test-base</artifactId>
                        <version>0.1</version>
                        <overWrite>true</overWrite>
                          <outputDirectory>
                              ${project.build.directory}/classes
                          </outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

如有任何帮助,我们将不胜感激.

Any help would be appriciated.

推荐答案

有一种方法可以从另一个 jar 在 Maven 中运行测试.从 maven-surefire-plugin 2.15 版开始,您可以告诉 maven 扫描您的测试 jar 以进行测试并运行它们.您不需要提取测试 jar.只需向您的测试 jar 添加一个依赖项,然后:

There is a way of running a test in maven from another jar. from maven-surefire-plugin version 2.15 you can tell maven to scan your test jars for tests and run them. You don't need to extract the tests jar. Just add a dependency to your test jar and:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <dependenciesToScan>
                <dependency>test.jar.group:test.jar.artifact.id</dependency>
            </dependenciesToScan>
        </configuration>
    </plugin>

https://gist.github.com/aslakknutsen/4520226 获取这些内容和 https://issues.apache.org/jira/browse/SUREFIRE-569

Took this stuff from https://gist.github.com/aslakknutsen/4520226 And https://issues.apache.org/jira/browse/SUREFIRE-569

正如预期的那样,这适用于 JUnit 和 Testng.可能适用于surefire可以运行的任何东西.

As expected, this works for JUnit and Testng. Will probably work for anything that surefire can run.

这篇关于使用 Maven Surefire 运行包含在依赖项 jar 中的 JUnit 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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