TestNG 测试控制与 maven -D 参数(组?)

TestNG test control with maven -Dparameter (groups?)

我将项目分成模块,每个模块都有单独的 XML 套件,测试 类 到 运行。我 运行 通过传递变量 -Denv=ENV_ALIAS 在不同环境中使用 maven 命令进行测试,使用 System.getProperty("env").

处理它们

我正在调整我的解决方案以处理不同国家/地区版本的应用程序(不同版本会禁用某些功能或某些流程不同)。我传递了一个变量 -DcountryCode=COUNTRY_ALIAS,因此测试使用不同的 URL,但默认情况下所有测试都是 运行。

我想根据套件级别的 countryCode 值禁用某些测试,而不是在每个测试方法中实施切换机制。 我认为 Groups 是原生的 testNG 解决方案,但它们相当静态,因为我需要 hard-code 它们在套件文件中。有没有办法让它更有活力? 或者,我可以在专用于每个国家/地区的每个模块中创建套件,但我需要将 TestNG 指向 运行 一个合适的套件(我该怎么做?)。

最好的处理方法是什么?有没有更好的方法来处理它?

我的 parent 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.

将组名指定为 运行

您可以很容易地增强它以考虑多个组名称、处理未提供组的情况等,

更多信息请参考我的博客post here.