TestNG 如何使用Annotations将单元测试和集成测试在TestNG + Maven中分离

TestNG 如何使用Annotations将单元测试和集成测试在TestNG + Maven中分离

TestNG是一个测试框架,可以使用Maven作为构建工具。它有助于在pom.xml文件中维护依赖和版本。

用户可以从testng.xml或pom.xml中运行测试。要从pom.xml运行测试,用户需要在pom.xml中指定testng.xml的路径,并且需要使用maven−surefire−plugin来执行。除了testng.xml,maven还提供了通过maven或命令行运行特定组的功能。在通过命令行运行时,用户可以在运行时指定组名,而无需更改testng.xml或pom.xml中的任何内容。

这个功能可以用来分离单元测试和集成测试。用户可以将与单元测试相关的所有测试组合成一个组,将集成测试作为另一个组,同时一些测试既属于单元测试又属于集成测试。

在本教程中,我们将看到如何分离单元测试和集成测试。

步骤

  • 步骤1:创建一个TestNG类 – NewTestngClass

  • 步骤2:在类中编写2个带有组名的@Test方法。

  • 步骤3:现在根据以下示例创建testNG.xml

  • 步骤4:将testng.xml的路径添加到pom.xml中,如下所示。

<configuration>
       <suiteXmlFiles>
             <suiteXmlFile>Path of testng.xml</suiteXmlFile>
        </suiteXmlFiles>
  </configuration>

在配置部分中,提到了testng的路径。有两件事要记住,第一是这个配置应该与maven-surefire-plugin一起保留,第二是不要忘记添加testng的依赖。

详细的pom.xml可以在程序部分找到。

  • 步骤5:我们不会在pom.xml或testng.xml中添加任何分组名称。

  • 步骤6:现在,使用命令行运行它。

示例

以下代码展示了如何从一个大的测试套件中运行仅有一个测试方法:

src/NewTestngClass.java

import org.testng.annotations.Test;

public class NewTestngClass {

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

testng.xml

这是一个用于组织和运行TestNG测试用例的配置文件。 当需要执行有限的测试而不是完整套件时,它非常方便。

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1" parallel = "none">
    <test name = "test1" preserve-order = "true">
        <classes>
        <class name=”NewTestngClass”/>
       </classes>
    </test>
</suite>

pom.xml

这是一个用于组织依赖项、插件并运行TestNG测试用例的Maven配置文件。

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

<?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>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <suiteXmlFiles>
                            <suiteXmlFile>src/main/java/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </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>
    </dependencies>
</project>

执行命令

mvn test -Dgroups=unit

输出

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.sample:TestNGProjectct >---------------------
[INFO] Building TestNGProjectct 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TestNGProjectct ---
[INFO] Deleting C:\Users\anandas\IdeaProjects\TestNGProjectct\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNGProjectct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestNGProjectct ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 8 source files to C:\Users\anandas\IdeaProjects\TestNGProjectct\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestNGProjectct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ TestNGProjectct ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to C:\Users\anandas\IdeaProjects\TestNGProjectct\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ TestNGProjectct ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
...
... TestNG 7.3.0 by Cédric Beust (cedric@beust.com)
...

in test case 1 of NewTestngClass
in test case 3 of NewTestngClass
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.639 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------
[INFO] Total time:  3.840 s
[INFO] Finished at: 2022-02-11T19:41:23+05:30
[INFO] --------------------------------------------------------

Process finished with exit code 0

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程

TestNG 精选笔记