【问题标题】:How to execute JUnit and TestNG tests in same project using maven-surefire-plugin?如何使用 maven-surefire-plugin 在同一个项目中执行 JUnit 和 TestNG 测试?
【发布时间】:2010-11-16 23:59:18
【问题描述】:

现在我有两种类型的测试,但是当我说“mvn test”时,它只执行 TestNG 测试而不是 Junit。我想一个接一个地执行。有什么想法吗?

【问题讨论】:

    标签: unit-testing maven-2 testng junit4 maven-surefire-plugin


    【解决方案1】:

    我找到了一个解决方案,可以在不更改构建工具配置的情况下使用 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 测试,帮助您在时间允许时迁移它们。

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      官方方式selecting providers

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

      <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>
      

      【讨论】:

      • 这应该会上升。唯一的问题是,如果您有一个多模块项目,则不会继承作为插件依赖项的提供程序。插件是继承的,它们的执行和配置是继承的,但不是它们的依赖关系。除此之外,解决问题的好方法。
      【解决方案3】:

      对于 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>
      

      【讨论】:

        【解决方案4】:

        对于 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 的依赖项

        【讨论】:

          【解决方案5】:

          对此还有另一种出路。您也可以要求 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>

          【讨论】:

          • 这行得通,虽然结果分组有点奇怪。
          • 请注意,TestNG 似乎只是忽略了有问题的测试而没有说什么。例如,我有一个规则(必须是公共字段),它是私有的,然后 testng 简单地忽略了测试,没有说什么。
          【解决方案6】:
          <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 测试)。

          【讨论】:

            【解决方案7】:

            如果您只指定 testng 提供程序,它将只运行一次 junit 测试和 testng 测试。
            所以对测试的命名没有限制。

            插件版本:
            Surefire-plugin 2.16(junit47 和 testng 提供程序的版本都设置为 2.16)
            testng 依赖 6.8.7
            junit 依赖 4.7

            【讨论】:

            • 我刚刚将项目从 JUnit 迁移到测试 NG 并意识到没有编译问题,junit:junit:jar:4.12 -> org.testng:testng:jar:7.3.0
            【解决方案8】:

            基于以前的解决方案。我发现这对我们最有效。我们面临的另一个问题是 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>
            

            【讨论】:

              【解决方案9】:

              感谢此链接 (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>
              

              【讨论】:

              • 但这还不够,因为如果您想使用 -Dtest=XXX 运行单个测试,它可能会失败,具体取决于主要提供者。例如,由于 junit 是我的主要提供者,尝试使用 -Dtest=XXX 运行任何 TestNG 测试都会失败。
              【解决方案10】:

              我发现解决方案是强制万无一失的插件使用 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>
              

              【讨论】:

              • 这个答案有点被上面的 MariuszS 回答(官方方式)过时了。此外,它没有启动 TestNG 测试(至少在 Surefire 2.10 和 2.16 下)。
              【解决方案11】:

              我有一个better solution

              这个想法是创建两个maven-surefire-plugin 的执行,一个用于JUnit,一个用于TestNG。您可以通过指定不存在的junitArtifactNametestNGArtifactName 来禁用每次执行的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>
              

              【讨论】:

              • 这个解决方案的工作原理是它为我启动了 3 次 surefire 插件:正常执行(启动 testng)+ 1 次仅用于 testng 测试,1 次仅用于 junit 测试。
              • 因为运行了 3 次而投反对票。恕我直言,最好的方法是使用“none:none”配置默认执行,请参阅jira.codehaus.org/browse/SUREFIRE-377 和 James Kato 的评论 - 对我很有用
              • @dermoritz 嗯,那是基于我最初的想法。
              • 禁用? 不存在
              【解决方案12】:

              有一个open issue for this,所以没有优雅的方法可以做到这一点。

              选择一个框架并坚持下去会简单得多。

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

              还有一点需要注意:您可以launch your JUnit tests from TestNG,但我认为这只适用于 JUnit 3 测试。

              【讨论】:

              • 当然这行不通,因为你不能在执行层添加依赖。您可以将 testng 依赖项移动到配置文件中以选择 junit 或 testng 执行,但这不是您所追求的。如果我发现任何东西,我会看看选项并更新
              • 谢谢rich,是的,你在cmets 中说的没错。我还尝试将“Junit”的属性添加为“true”,如该问题中所述,但对我没有帮助,所以决定在这里提问。无论如何谢谢:)
              • 是的,但很遗憾没有为我运行,请参考stackoverflow.com/questions/1238017/…
              • 更新:该问题已修复并关闭issues.apache.org/jira/browse/SUREFIRE-377
              猜你喜欢
              • 1970-01-01
              • 2013-07-22
              • 2022-01-14
              • 2020-11-17
              • 1970-01-01
              • 2019-01-23
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多