如何在TestNG + Maven中使用注解分离单元测试和集成测试?
TestNG是一个测试框架,可以使用Maven作为构建工具。它有助于在一个地方(pom.xml)维护依赖项及其版本。
用户可以从testng.xml或pom.xml运行测试。要从pom.xml运行测试,用户需要提及testng.xml的路径,并需要maven-surefire-plugin来执行。除了testng.xml,Maven还提供了通过Maven或命令行运行特定组的功能。在通过命令行运行时,用户可以在运行时提及组名,而无需更改testng.xml或pom.xml中的任何内容。
此功能可用于分离单元测试和集成测试。用户可以将所有与单元测试相关的测试归类为“unit”,将集成测试归类为“integration”,如果某些测试属于两者,则可以将其定义为“unit”和“integration”。
在本教程中,我们将了解如何分离单元测试和集成测试。
解决此问题的方法/算法
步骤 1:创建TestNG类 - NewTestngClass
步骤 2:在类中编写2个@Test方法,并指定组名。
步骤 3:现在创建如下所示的testng.xml
步骤 4:在pom.xml中添加testng.xml的路径,如下所示。
<configuration>
<suiteXmlFiles>
<suiteXmlFile>Path of testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
在配置部分,提到了testng的路径。需要记住两件事,第一件事是此配置应与maven-surefire-plugin一起保留,第二件事是永远不要忘记添加testng的依赖项。
详细的pom.xml可以在程序部分找到。
步骤 5:我们不会在pom.xml或testng.xml中添加任何组名。
步骤 6:现在,使用命令行运行它。
示例
以下代码演示了如何从大型套件中仅运行1个测试方法
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
这是一个Maven配置文件,用于组织依赖项、插件和运行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>
<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
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP