【发布时间】:2010-11-16 23:59:18
【问题描述】:
现在我有两种类型的测试,但是当我说“mvn test”时,它只执行 TestNG 测试而不是 Junit。我想一个接一个地执行。有什么想法吗?
【问题讨论】:
标签: unit-testing maven-2 testng junit4 maven-surefire-plugin
现在我有两种类型的测试,但是当我说“mvn test”时,它只执行 TestNG 测试而不是 Junit。我想一个接一个地执行。有什么想法吗?
【问题讨论】:
标签: unit-testing maven-2 testng junit4 maven-surefire-plugin
我找到了一个解决方案,可以在不更改构建工具配置的情况下使用 TestNG 运行两种测试类型。
我使用 Gradle 进行了测试,但也应该使用 Maven。
请注意,这将在 TestNG 中运行 JUnit 测试,但不会反过来。
诀窍是在测试类中使用两个框架的注释并使用 TestNG 断言来实现 JUnit 兼容性。
import static org.testng.AssertJUnit.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@org.testng.annotations.Test
public final class ApplicationTest {
@org.testng.annotations.BeforeClass
@BeforeClass
public static void setup () {}
@org.testng.annotations.AfterClass
@AfterClass
public static void cleanup () {}
@Test public void json () throws IOException {
assertTrue (true);
}
}
使用此 hack,您可以轻松地使用 TestNG 运行现有的 JUnit 测试,帮助您在时间允许时迁移它们。
希望对你有帮助!
【讨论】:
您还可以将多个提供程序指定为依赖项,它们都将运行并生成一个通用报告。这对于外部提供者来说可能特别方便,因为很少有用于组合包含的提供者的用例。
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.18.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.18.1</version>
</dependency>
</dependencies>
</plugin>
更多信息:Mixing TestNG and JUnit tests in one Maven module – 2013 edition
maven-surefire-plugin examples 中的当前链接。 搜索“运行 TestNG 和 JUnit 测试”。
您需要配置 testng 提供程序以忽略 junit 测试,如下所示:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<properties>
<property>
<name>junit</name>
<value>false</value>
</property>
</properties>
</configuration>
[...providers as dependecies, see above...]
</plugin>
【讨论】:
对于 Junit 这解决了我的问题
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.19.1</version>
</dependency>
</dependencies>
【讨论】:
对于 JUnit ---
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.19.1</version>
</dependency>
</dependencies>
在需要时类似地使用 TestNG 的依赖项
【讨论】:
对此还有另一种出路。您也可以要求 TestNG 运行 Junit 测试用例。 下面是运行所有测试用例的示例 testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestAll">
<test name="junitTestCases" junit="true">
<packages>
<package name="com.test.*" />
</packages>
</test>
<test name="testNGTestCases" >
<packages>
<package name="com.test.*" />
</packages>
</test>
</suite>
【讨论】:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- ********* Skip Test for Success BUILD ******** -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!-- *********************************************** -->
</plugins>
</build>
<profiles>
<!-- ********** Profiles for run test cases ************ -->
<!-- Profile for run JUnit test dependent tests -->
<profile>
<id>junit-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<skip>false</skip>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.10</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
<!-- Profile for run TestNG dependent tests -->
<profile>
<id>testNG-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- ***************************************************** -->
</profiles>
不仅仅是运行:mvn test -Pjunit-tests(用于基于 junit 的运行测试)或 mvn test -PtestNG-tests(用于基于 TestNG 测试)。
【讨论】:
如果您只指定 testng 提供程序,它将只运行一次 junit 测试和 testng 测试。
所以对测试的命名没有限制。
插件版本:
Surefire-plugin 2.16(junit47 和 testng 提供程序的版本都设置为 2.16)
testng 依赖 6.8.7
junit 依赖 4.7
【讨论】:
junit:junit:jar:4.12 -> org.testng:testng:jar:7.3.0
基于以前的解决方案。我发现这对我们最有效。我们面临的另一个问题是 TestNG 试图运行旧的 JUnit 测试。我们通过不同地命名所有 TestNG 测试来避免这种情况(例如 *TestNG.java)。下面是两次执行surefire-plugin的配置。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
<excludes>
<exclude>**/*TestNG.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>test-testng</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<junitArtifactName>none:none</junitArtifactName>
<testNGArtifactName>org.testng:testng</testNGArtifactName>
<includes>
<include>**/*TestNG.java</include>
</includes>
<excludes>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
【讨论】:
感谢此链接 (http://jira.codehaus.org/browse/SUREFIRE-377),这是我之前问题的解决方案(执行 3 次而不是 2 次)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
</configuration>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<junitArtifactName>none:none</junitArtifactName>
<testNGArtifactName>org.testng:testng</testNGArtifactName>
</configuration>
</execution>
</executions>
</plugin>
【讨论】:
我发现解决方案是强制万无一失的插件使用 JUnit。我通过覆盖特定项目中的surefire插件来做到这一点,如下所示。依赖强制surefire使用JUnit。
<build>
<plugins>
<!-- force sure fire to use junit instead of testng -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.10</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
【讨论】:
我有一个better solution。
这个想法是创建两个maven-surefire-plugin 的执行,一个用于JUnit,一个用于TestNG。您可以通过指定不存在的junitArtifactName 或testNGArtifactName 来禁用每次执行的TestNG 或JUnit:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
</configuration>
</execution>
<execution>
<id>test-testng</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<junitArtifactName>none:none</junitArtifactName>
</configuration>
</execution>
</executions>
</plugin>
【讨论】:
有一个open issue for this,所以没有优雅的方法可以做到这一点。
选择一个框架并坚持下去会简单得多。
编辑:我之前的答案不起作用,因为您无法在执行中指定依赖项。 我尝试了几种方法,但我能做到的最好的方法是为 TestNG 依赖项创建一个配置文件,以便您可以在 TestNG 和 JUnit 测试之间切换,似乎没有办法同时运行 TestNG 和 Junit 4 测试.
还有一点需要注意:您可以launch your JUnit tests from TestNG,但我认为这只适用于 JUnit 3 测试。
【讨论】: