如何从 Maven 传递参数到 Java 代码进行测试?
TestNG 是一个测试框架,可以使用 Maven 作为构建工具。它有助于在一个地方(pom.xml)维护依赖项及其版本。
Maven 提供了使用 surefire 插件运行的灵活性。Surefire 插件具有从 pom.xml 向 TestNG 类或任何 Java 代码传递参数的功能。为了实现这一点,surefire 插件还有一个名为 systemPropertyVariables 的标签,用户可以在其中传递任意数量的变量,并在运行时使用命令行将值赋予这些变量。
在本文中,我们将说明如何从 Maven 向 Java 代码传递参数。
解决此问题的方法/算法
步骤 1:创建 TestNG 类 - NewTestngClass
步骤 2:在类中编写 2 个 @Test 方法。
步骤 3:在 pom.xml 中添加 systemPropertyVariable,如程序代码部分所示。变量为 ${env.user}
步骤 4:现在在命令中运行时传递变量的值。
mvn test -Denv.user=UAT
步骤 5:现在,验证是否访问了相同的变量值。
示例
以下代码展示了如何从大型套件中运行单个测试方法。
src/test/java/NewTest.java
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class NewTest {
@Test()
public void testCase1() {
System.out.println("in test case 1 of NewTestngClass");
System.out.println("Regression" + System.getProperty("environment"));
}
@Parameters("environment")
@Test()
public void testCase2(String environment) {
System.out.println("in test case 2 of NewTestngClass");
System.out.println("Environment name is- "+environment);
}
}
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>
<systemPropertyVariables>
<environment>${env.user}</environment>
</systemPropertyVariables>
</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 clean test -Denv.user=UAT
输出
[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 1 source file 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 newTest in test case 1 of NewTestngClass RegressionUAT in test case 2 of NewTestngClass Environment name is- UAT [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.614 s - in newTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.468 s [INFO] Finished at: 2022-02-11T09:06:48+05:30 [INFO] ------------------------------------------------------------------------ Process finished with exit code 0
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP