使用 Maven、TestNG - 如何 运行 TestNG 中的特定组。想知道,我正在做的事情是否可行?
Using Maven, TestNG - How to run specific groups in TestNG. Wondering, if what I am doing is even feasible?
这就是我的 POM.xml 的样子。
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.akiraly.reusable-poms</groupId>
<artifactId>pom-parent-with-spring-context</artifactId>
<version>4</version>
</parent>
<groupId>MvnTestFramework</groupId>
<artifactId>MvnTestFrameworkProject</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.6</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<forkMode>always</forkMode>
<systemProperties>
<property>
<name>reporter.debug</name>
<value>false</value>
</property>
</systemProperties>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<reportsDirectory>reports</reportsDirectory>
<parallel>methods</parallel>
<threadCount>5</threadCount>
<skipTests>false</skipTests>
<suitename>${testng.suitename}</suitename>
<groups>${testng.groups}</groups>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>utf8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是我的 testng.xml 的样子:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testgroups" verbose="1" parallel="tests" group-by-instances="true" preserve-order="false" thread-count="5">
<test name="testgroups" preserve-order="true" group-by-instances="true">
<groups>
<run>
<include name="group1" />
<include name="group2" />
<include name="group3" />
</run>
</groups>
<packages>
<package name="testpackage.*" />
</packages>
</test>
</suite>
我想要的是:运行只有特定的测试组。意思是,无论是 group1、group2 还是 group3,但不是所有的人都 运行 一次在一起。
出于同样的原因,我已将 {testng.groups} 提供给 运行 特定组。
在 JVM 参数中,当我 运行 testng.xml 我指定:
-Dtestng.groups= group2 到 运行 只有 group2 测试。但是,它只是 运行 组 1、组 2 和组 3 中的所有测试。
我处理问题的方式是否正确?我怎样才能确保只有一组特定的组 运行s。谢谢你们的帮助。
您可以尝试利用 TestNG 必须提供的 Beanshell 功能来完成此任务。
这样,无论您如何 运行 您的测试套件:
- 通过您的 IDE 使用 TestNG 插件,其中您的 pom 文件没有相关性(或)
- 通过 maven,其中 maven 具有相关性
您的套件文件的方法选择器将始终被接受。
简而言之,您需要定义一个使用 Beanshell 的方法选择器,如下所示
<?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 -->
一旦你有了这样的套件 xml 文件,你就可以通过 JVM 参数 -DwhatGroup
.
指定组名
如果通过 JVM 参数没有找到任何组,您还可以通过选择 运行 一切来为 beanshell 脚本增添趣味。
有关详细信息,您可以参考 my blog post 以全面了解如何使用 beanshell。
这就是我的 POM.xml 的样子。
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.akiraly.reusable-poms</groupId>
<artifactId>pom-parent-with-spring-context</artifactId>
<version>4</version>
</parent>
<groupId>MvnTestFramework</groupId>
<artifactId>MvnTestFrameworkProject</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.6</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<forkMode>always</forkMode>
<systemProperties>
<property>
<name>reporter.debug</name>
<value>false</value>
</property>
</systemProperties>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<reportsDirectory>reports</reportsDirectory>
<parallel>methods</parallel>
<threadCount>5</threadCount>
<skipTests>false</skipTests>
<suitename>${testng.suitename}</suitename>
<groups>${testng.groups}</groups>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>utf8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是我的 testng.xml 的样子:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testgroups" verbose="1" parallel="tests" group-by-instances="true" preserve-order="false" thread-count="5">
<test name="testgroups" preserve-order="true" group-by-instances="true">
<groups>
<run>
<include name="group1" />
<include name="group2" />
<include name="group3" />
</run>
</groups>
<packages>
<package name="testpackage.*" />
</packages>
</test>
</suite>
我想要的是:运行只有特定的测试组。意思是,无论是 group1、group2 还是 group3,但不是所有的人都 运行 一次在一起。 出于同样的原因,我已将 {testng.groups} 提供给 运行 特定组。 在 JVM 参数中,当我 运行 testng.xml 我指定: -Dtestng.groups= group2 到 运行 只有 group2 测试。但是,它只是 运行 组 1、组 2 和组 3 中的所有测试。
我处理问题的方式是否正确?我怎样才能确保只有一组特定的组 运行s。谢谢你们的帮助。
您可以尝试利用 TestNG 必须提供的 Beanshell 功能来完成此任务。
这样,无论您如何 运行 您的测试套件:
- 通过您的 IDE 使用 TestNG 插件,其中您的 pom 文件没有相关性(或)
- 通过 maven,其中 maven 具有相关性
您的套件文件的方法选择器将始终被接受。
简而言之,您需要定义一个使用 Beanshell 的方法选择器,如下所示
<?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 -->
一旦你有了这样的套件 xml 文件,你就可以通过 JVM 参数 -DwhatGroup
.
如果通过 JVM 参数没有找到任何组,您还可以通过选择 运行 一切来为 beanshell 脚本增添趣味。
有关详细信息,您可以参考 my blog post 以全面了解如何使用 beanshell。