如何使用maven-surefire-plugin在同一个项目中执行JUnit和TestNG测试?

rav*_*kam 39 testng maven-2 unit-testing junit4 maven-surefire-plugin

现在我有两种类型的测试但是当我说"mvn test"它只执行TestNG测试而不是Junit.我想一个接一个地执行.任何的想法 ?

Mar*_*szS 46

选择供应商的官方方式.

您还可以将多个提供程序指定为依赖项,并且它们都将运行并生成公共报告.这可能对外部提供商特别方便,因为用于组合所包含的提供者的用例很少.

<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>
Run Code Online (Sandbox Code Playgroud)

有关此内容的更多信息:在一个Maven模块 - 2013版中混合TestNG和JUnit测试

maven-surefire-plugin示例中的当前链接.搜索" 运行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>
Run Code Online (Sandbox Code Playgroud)

  • 这应该会上升.唯一的问题是,如果您有一个多模块项目,则不继承作为插件依赖项的提供程序.插件是继承的,它们的执行和配置是,但不是它们的依赖.除此之外,解决问题的好方法. (2认同)
  • 是的,这应该是公认的答案 (2认同)

lex*_*ore 13

我有一个更好的解决方案.

我们的想法是创建两个执行maven-surefire-plugin,一个用于JUnit,一个用于TestNG.您可以通过指定不存在junitArtifactName或每次执行来禁用TestNG或JUnit之一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>
Run Code Online (Sandbox Code Playgroud)


Ric*_*ler 11

这是一个悬而未决的问题,所以没有优雅的方法来做到这一点.

选择一个框架并坚持使用它会简单得多.

编辑:我之前的答案不起作用,因为您无法在执行中指定依赖项.我尝试了一些方法,但我能管理的最好的方法是为TestNG依赖创建一个配置文件,这样你就可以在TestNG和JUnit测试之间切换,似乎没有办法同时运行TestNG和Junit 4测试.

另外需要注意的一点是:您可以从TestNG启动JUnit测试,但我认为这仅适用于JUnit 3测试.


小智 6

还有另一种出路.您也可以要求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>
Run Code Online (Sandbox Code Playgroud)


Chr*_*iak 5

感谢这个链接(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>
Run Code Online (Sandbox Code Playgroud)