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

发布于 2024-07-29 17:07:28 字数 87 浏览 9 评论 0原文

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

Right now I have both type of tests but when I say "mvn test" it only executes TestNG tests and not Junit. I want to execute both one after another. Any Idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

天涯沦落人 2024-08-05 17:07:28

选择提供商的官方方式。

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

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

Official way with selecting providers.

You can also specify multiple providers as dependencies, and they will all be run and produce a common report. This may be especially handy with external providers, since there are few use-cases for combining the included 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>

More info about this: Mixing TestNG and JUnit tests in one Maven module – 2013 edition

Current Link for this in the maven-surefire-plugin examples.
Search for "Running TestNG and JUnit Tests".

You will want to configure the testng provider to ignore the junit tests like so:

<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>
毁我热情 2024-08-05 17:07:28

我有一个 更好的解决方案

这个想法是创建 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>

I have a better solution.

The idea is to create two executions of the maven-surefire-plugin, one for JUnit, one for TestNG. You can disable one of TestNG or JUnit per execution by specifying nonexisting junitArtifactName or 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>
春风十里 2024-08-05 17:07:28

有一个此问题尚未解决,因此没有优雅的方法可以做到这一点。

对你来说,选择一个框架并坚持使用会简单得多。

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

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

There is an open issue for this, so there's no elegant way to do this.

It would be far simpler for you to pick a framework and stick with it.

Edit: My previous answer doesn't work because you can't specify dependencies in the execution.
I've tried a few approaches, but the best I can manage is to create a profile for the TestNG dependency so you can toggle between TestNG and JUnit testing, there doesn't seem to be a means to run both TestNG and Junit 4 tests.

One other point to note: You can launch your JUnit tests from TestNG, but I think this only works for JUnit 3 tests.

就像说晚安 2024-08-05 17:07:28

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

There is another wayout for this. You could ask TestNG to run Junit test cases as well.
Below is the sample testng.xml to run all test cases

<!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>
绻影浮沉 2024-08-05 17:07:28

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

Thanks to this link (http://jira.codehaus.org/browse/SUREFIRE-377), here is a solution to my previous problem (having 3 executions instead of 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>
当梦初醒 2024-08-05 17:07:28

我找到了一个解决方案,可以使用 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);
    }
}

使用此技巧,您可以使用 TestNG 轻松运行现有的 JUnit 测试,帮助您在时间允许时迁移它们。

希望能帮助到你!

I found out a solution to run both test types with TestNG without changing your build tool configuration.

I tested with Gradle but should work with Maven too.

Note that this will run JUnit tests inside TestNG, but not the other way back.

The trick is to use both frameworks' annotations in the test classes and use TestNG asserts for JUnit compatibility.

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);
    }
}

Using this hack, you can easily run existing JUnit tests with TestNG, helping you migrate them when time allows.

Hope it helps!

私藏温柔 2024-08-05 17:07:28

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

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

Similarly use the dependency for TestNG when required

破晓 2024-08-05 17:07:28

我发现解决方案是强制 Surefire 插件使用 JUnit。 我通过覆盖特定项目中的 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>

I found that the solution was to force the sure-fire plugin to use JUnit. I did this by overriding surefire plugin in the specific project as follows. The dependency forces surefire to use 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>
萌能量女王 2024-08-05 17:07:28

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

Based on previous solutions. I found this worked best for us. One more issue we were facing was TestNG trying to run old JUnit tests. We avoided this by naming all TestNG tests differently (e.g. *TestNG.java). Below is the configuration with two executions of 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>
苦行僧 2024-08-05 17:07:28

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

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

if you just specify testng provider, it will run both junit tests and testng tests all just once.
so there is no restriction on naming the tests.

plugin versions:
surefire-plugin 2.16 (junit47 and testng providers both version set to 2.16)
testng dependency 6.8.7
junit dependency 4.7

痴者 2024-08-05 17:07:28
<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 测试)。

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

Than just run: mvn test -Pjunit-tests (for run test based on junit) or mvn test -PtestNG-tests (for TestNG test based).

风铃鹿 2024-08-05 17:07:28

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

for Junit this solved my problem

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文