26

我的 maven 存储库中有一个 jar,其中包含 junit 测试,它应该在不同的项目中运行,因为它能够检查项目并测试它的某些功能。不幸的是,surefire 不会提取包含在 jar 中的测试,正如此Feature Request 所示

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

我使用 maven-dependency-plugin 成功解压缩了 jar,但无论如何都不会执行包含的测试。这就是我配置 maven-dependency-plugin 以解压缩我的 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>

任何帮助都将不胜感激。

4

3 回答 3

42

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

<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/4520226https://issues.apache.org/jira/browse/SUREFIRE-569拿了这些东西

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

于 2013-06-12T09:12:27.247 回答
3

(这只是重申上面来自 khmarbaise 的评论中的内容,但由于没有澄清,我认为值得重申):

使用 test-classes 目录而不是 classes 文件夹作为 outputDirectory:

<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}/test-classes
                          </outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>
于 2013-01-28T21:48:27.077 回答
1

如问题中所述,您需要有一个包含在项目中的套件,该套件不位于测试 jar 中。

于 2012-05-08T10:46:25.223 回答