如何使用 maven-surefire-plugin 在同一项目中执行 JUnit 和 TestNG 测试?
-
22-07-2019 - |
题
现在我有两种类型的测试,但是当我说“mvn test”时,它只执行 TestNG 测试而不执行 Junit。我想一个接一个地执行这两个操作。任何想法 ?
解决方案
有对THI 秒的悬而未决的问题,所以没有优雅的方式做此
这将是简单得多为您挑选了一个框架,并坚持下去。
编辑:我以前的答案没有工作,因为你不能指定在执行依赖性。 我已经尝试了一些办法,但我可以管理最好的是创造TestNG的依赖,所以你可以TestNG的和JUnit测试之间切换配置文件,似乎没有要同时运行TestNG的和JUnit 4个测试手段
另外一点要注意:您可以从TestNG的启动您的JUnit测试,但我认为JUnit 3个测试这仅适用。
其他提示
官方方式与 选择供应商.
您还可以指定多个提供程序作为依赖项,它们都将运行并生成通用报告. 。这对于外部提供者来说可能特别方便,因为组合所包含的提供者的用例很少。
<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>
有关此的更多信息: 在一个 Maven 模块中混合 TestNG 和 JUnit 测试 – 2013 版
当前的链接位于 maven-surefire-插件示例。搜索 ”运行 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>
我有一个 更好的解决方案.
这个想法是创建两个执行 maven-surefire-plugin
, ,一种用于 JUnit,一种用于 TestNG。您可以通过指定不存在的值来禁用每次执行时的 TestNG 或 JUnit 之一 junitArtifactName
或者 testNGArtifactName
:
<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>
有这个另一出路。你可以问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>
由于该链路( 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 ---
<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的依赖性需要时
我发现,解决办法是迫使神火插件使用JUnit。我通过在具体项目覆盖保命插件如下这样做。依赖力万无一失到使用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>
根据先前的解决方案。我发现这个工作对我们最好的。我们正面临着一个更问题是TestNG的努力来运行老的JUnit测试。我们通过命名所有TestNG进行不同(例如* TestNG.java)避免这一点。下面是万无一失的,插件的两次执行该配置。
<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>
我发现了一种解决方案,而不改变您的构建工具配置中运行两个测试类型使用TestNG。
我与摇篮测试,但应与Maven工作了。
请注意,这将运行JUnit测试里面TestNG的,但不回的其他方式。
诀窍是在测试类使用两个框架注释和使用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);
}
}
使用这个技巧,你可以轻松地运行使用TestNG现有的JUnit测试,帮助您迁移他们的时候时间允许的。
希望它能帮助!
有关的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的供应商,它会同时运行JUnit测试和TestNG测试都只有一次。点击 所以没有限制关于命名测试。
插件版本:结果 神火-插件2.16(junit47和TestNG提供商既版本号为2.16)结果 TestNG的依赖6.8.7结果 junit的依赖性4.7
<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测试-Pjunit检验(基于junit的运行试验)(基于用于TestNG的测试)或MVN测试-PtestNG检验