我的项目分为多个模块,每个模块都有一个 XML 套件,其中包含要运行的测试类。我通过传递变量 -Denv=ENV_ALIAS
在不同的环境上使用 maven 命令运行测试,并使用 System.getProperty("env")
处理它们。
我正在重新调整我的解决方案以处理不同国家/地区版本的应用程序(不同版本会禁用某些功能或某些流程不同)。我传递了一个变量 -DcountryCode=COUNTRY_ALIAS
,因此测试使用不同的 URL,但默认情况下所有测试都会运行。
我想根据套件级别的 countryCode
值禁用一些测试,而不是在每个测试方法中实现切换机制。
我认为 Groups 是 native testNG 解决方案,但它们相当静态,因为我需要将它们硬编码到套件文件中。有没有办法让它变得更有活力?
或者,我可以在专用于每个国家/地区的每个模块内创建套件,但我需要指向 TestNG 来运行单个适当的套件(我该怎么做?)。
最好的处理方法是什么?有没有更好的方法来处理?
我的父 pom 最重要的部分:
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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.autoqa</groupId>
<artifactId>autoqa-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>autoqa-parent</name>
<modules>
<module>autoqa-common</module>
<module>autoqa-newcrm</module>
<module>autoqa-website</module>
<module>autoqa-giftshop</module>
<module>autoqa-unlimited</module>
<module>autoqa-booking</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefireVersion}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefireVersion}</version>
</dependency>
</dependencies>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/${file.name}.xml</suiteXmlFile>
</suiteXmlFiles>
<systemPropertyVariables>
<browserName>${browserName}</browserName>
<suiteName>${suiteName}</suiteName>
<countryCode>${countryCode}</countryCode>
<env>${env}</env>
</systemPropertyVariables>
<properties>
<property>
<name>surefire.testng.verbose</name>
<value>10</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
最佳答案
处理此问题的最简单方法是通过组。
是的,默认情况下组名称必须硬连接到您的套件 xml 文件中。
但是 TestNG 还允许您定义自己的方法选择器,可用于挑选要运行的测试以及要跳过的测试。
构建方法选择器的最简单方法之一是利用 TestNG 支持的 Beanshell 脚本。
您基本上可以创建一个如下所示的 Beanshell 方法选择器,然后继续传递组名称以通过 JVM
参数运行。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
<test name="Test">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[whatGroup = System.getProperty("groupToRun");
groups.containsKey(whatGroup);
]]>
</script>
</method-selector>
</method-selectors>
<classes>
<class name="organized.chaos.GroupsPlayGround" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
在上面的示例中,您可以通过 JVM 参数 -DgroupToRun
指定要运行的组名称。
您可以非常轻松地增强此功能以考虑多个组名称,处理未提供组的情况等,
更多信息请引用我的博文here .
关于java - TestNG 使用 maven -Dparameter 进行测试控制(组?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47455759/