TestNg 可以在同一个 pom 中进行集成测试和 junit5 进行单元测试吗?

问题描述 投票:0回答:1

所以我有一个pom文件,其中有junit5,还有testng,我想实现以下目标: 运行 mvn verify 时 -> 首先,单元测试将使用 junit 平台运行,然后将触发集成测试,但 testng 将是集成测试的平台提供者(我确实有 xml 文件)。

当运行 mvn package -> 我希望 Surefire 插件仅触发 junit 平台并运行单元测试。

目前,我确实看到运行 mvn package 时它使用 junit 平台提供程序运行单元测试,这很好。

此外,在运行 mvn verify 时,它首先使用 junit 平台提供程序触发单元测试并运行 junit 测试,这很棒,但随后它找不到 testng 平台提供程序,并且不运行 xml 内编写的任何集成测试文件。

波姆:

  <junit.jupiter.version>5.8.2</junit.jupiter.version>
  <testcontainers.version>1.17.2</testcontainers.version>
  <junit.platform.version>1.8.2</junit.platform.version>

 
  <dependencies>
        <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter</artifactId>
             <version>${junit.jupiter.version}</version>
         </dependency>
         <dependency>
             <groupId>org.testcontainers</groupId>
             <artifactId>testcontainers</artifactId>
             <version>${testcontainers.version}</version>
         </dependency>
         <dependency>
             <groupId>org.testcontainers</groupId>
             <artifactId>junit-jupiter</artifactId>
             <version>${testcontainers.version}</version>
         </dependency>
         <dependency>
             <groupId>org.junit.platform</groupId>
             <artifactId>junit-platform-suite-api</artifactId>
             <version>${junit.platform.version}</version>
         </dependency>
         <dependency>
             <groupId>org.junit.platform</groupId>
             <artifactId>junit-platform-suite-engine</artifactId>
             <version>${junit.platform.version}</version>
         </dependency>
         <dependency>
             <groupId>org.junit.vintage</groupId>
             <artifactId>junit-vintage-engine</artifactId>
             <version>${junit.jupiter.version}</version>
         </dependency>
          <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter-api</artifactId>
             <version>${junit.jupiter.version}</version>
         </dependency>
          <dependency>
             <groupId>org.testng</groupId>
             <artifactId>testng</artifactId>
             <version>7.9.0</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.junit.support</groupId>
             <artifactId>testng-engine</artifactId>
             <version>1.0.1</version>
             <scope>test</scope>
         </dependency>
  </dependencies>
  <plugins>
          <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>2.22.2</version>
                 <configuration>

                 </configuration>
                 <dependencies>
                     <dependency>
                         <groupId>org.apache.maven.surefire</groupId>
                         <artifactId>surefire-junit-platform</artifactId>
                         <version>3.2.1</version>
                     </dependency>
                     <dependency>
                         <groupId>org.apache.maven.surefire</groupId>
                         <artifactId>surefire-testng</artifactId>
                         <version>3.2.1</version>
                     </dependency>
                 </dependencies>
          </plugin>

          <plugin>
             <!-- The Failsafe plugin will look for **/IT\*.java, **/\*IT.java, and **/\*ITCase.java -->
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-failsafe-plugin</artifactId>
             <version>3.0.0-M7</version>
             <executions>
                 <execution>
                     <goals>
                         <goal>integration-test</goal>
                         <goal>verify</goal>
                     </goals>
                 </execution>
             </executions>
             <configuration>
                 <suiteXmlFiles>
                     <file>src/test/resources/testng-first- 
                      suite.xml</file>
                     <file>src/test/resources/testng-second- 
                       suite.xml</file>
                 </suiteXmlFiles>
             </configuration>
          </plugin>
    </plugins>
java maven junit5
1个回答
0
投票

下面的

<dependencies>
部分和
<plugins>
部分应该阐明如何执行此操作。

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <surefire.version>3.2.5</surefire.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.9.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.10.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${surefire.version}</version>
            <configuration>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit-platform</artifactId>
                    <version>${surefire.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${surefire.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/suite.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-testng</artifactId>
                    <version>${surefire.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

现在您可以使用

mvn clean verify
运行 JUnit 和 TestNG 测试。

© www.soinside.com 2019 - 2024. All rights reserved.