使用Maven Surefire运行依赖jar中包含的JUnit测试

时间:2012-05-08 10:28:32

标签: maven jar junit surefire

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

在功能请求中,他们建议解压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>

任何帮助都会得到满足。

3 个答案:

答案 0 :(得分:34)

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

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.15</version>
        <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

答案 1 :(得分:2)

(这只是重述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>

答案 2 :(得分:1)

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

相关问题