TestNG 如何使用Maven-Surefire-Plugin在同一项目中执行JUnit和TestNG测试

TestNG 如何使用Maven-Surefire-Plugin在同一项目中执行JUnit和TestNG测试

Maven是一个项目管理和理解工具,提供了一个完整的构建生命周期框架。用户可以自动化项目的构建基础设施,因为Maven使用了标准的目录布局和默认的构建生命周期。

总结一下,Maven简化和标准化了项目构建过程。它处理编译、分发、文档、团队协作和其他任务。Maven提高了可重用性,并处理了大部分与构建相关的任务。

TestNG和JUnit是测试框架,可以使用Maven作为构建工具。它帮助在pom.xml文件中集中管理依赖和它们的版本。

用户可以使用surefire插件在maven中运行JUnit和TestNG测试用例。但是,它提供了灵活性,可以使用相同的构建分别运行它们。

在本教程中,我们将看到如何通过surefire在同一项目中运行JUnit和TestNG测试用例。

步骤

  • 第一步:创建一个Junit类 – JunitTest

  • 第二步:创建一个TestNG类 – newTest

  • 第三步:在这两个类中编写@Test方法。

  • 第四步:现在在pom.xml中添加testng和junit依赖。

  • 第五步:在pom.xml中添加两个执行部分。一个用于testng,另一个用于junit,如程序代码部分所示。

  • 第六步:现在,运行所需的执行来运行junit或testng测试用例。

命令的语法应该如下:

mvn groupId:artifactId:versionId:goal@excution-id

示例

以下代码演示如何在Maven中运行JUnit和TestNG测试用例:

src/test/java/JunitTest.java

import org.junit.Test;

public class JunitTest {
    @Test
    public void testcase1() {
        System.out.println("in test case 1 of junit");
    }
}

src/ test/java/newTest.java

import org.testng.annotations.Test;

public class newTest {

    @Test(groups = { "unit", "integration" })
    public void testCase1() {
        System.out.println("in test case 1 of newTest");
    }
    @Test(groups = { "integration" })
    public void testCase2() {
        System.out.println("in test case 2 of newTest");
    }
    @Test(groups = { "unit" })
    public void testCase3() {
        System.out.println("in test case 3 of newTest");
    }
}

pom.xml

这是一个Maven配置文件,用于组织依赖项,插件,并运行junit和testng测试案例。

当需要执行有限的测试而不是整个测试套件时,它非常方便。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>TestNGProjectct</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
<!-- configured to work with JUnit and TestNg in same project separatelly -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
<!-- skip the default execution, we have separate for TestNG and for JUnit -->
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>TestNGRun</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                            <includes>
                                <include>%regex[.*newTest.*]</include>
                            </includes>
                           <!-- used to skip JUnit provider -->
                            <junitArtifactName>null:null</junitArtifactName>
<!-- to continue on next execution in case of failures here -->
                            <testFailureIgnore>true</testFailureIgnore>
                        </configuration>
                    </execution>
                    <execution>
                        <id>JUnitRun</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>

                            <skip>false</skip>
                       <!-- used to skip TestNg provider -->
                            <testNGArtifactName>null:null</testNGArtifactName>
                            <excludes>
                                <exclude>%regex[.*newTest.*]</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    </plugins>
    </build>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>

    </properties>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

运行TestNG测试用例的命令

mvn org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M5::test@TestNGRun

输出

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------< com.sample:TestNGProjectct >-------------
[INFO] Building TestNGProjectct 1.0-SNAPSHOT
[INFO] ------------------------[ jar ]-------------------------
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (TestNGRun) @ TestNGProjectct ---
[INFO] 
[INFO] -----------------------------------------------------
[INFO]  T E S T S
[INFO] -----------------------------------------------------
[INFO] Running newTest
in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
in test case 3 of NewTestngClass
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.547 s - in newTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] -------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -------------------------------------------------------
[INFO] Total time:  1.854 s
[INFO] Finished at: 2022-02-15T16:48:13+05:30
[INFO] --------------------------------------------------------

Process finished with exit code 0

运行JUnit测试用例的命令

mvn org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M5::test@JUnitRun

输出

[INFO] 
[INFO] ---------------------< com.sample:TestNGProjectct >---------------------
[INFO] Building TestNGProjectct 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (JUnitRun) @ TestNGProjectct ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running JunitTest
in test case 1 of junit
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 s - in JunitTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------
[INFO] Total time:  1.430 s
[INFO] Finished at: 2022-02-15T17:01:57+05:30
[INFO] --------------------------------------------------------

Process finished with exit code 0

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程