Reputation: 13
This is how my POM.xml looks like.
<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>
This is how my testng.xml looks like:
<!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>
What I want is: run only specific groups of tests. Meaning, either group1, or group2 or group3, but not all of them to run together at once. For the same reason, I have provided {testng.groups} to run the specific group. In the JVM arguments when I run the testng.xml I specify: -Dtestng.groups= group2 to run only the group2 tests. However, it just runs all tests in the group1, group2 and group3.
Am I approaching the problem correctly? How can I make sure only a specific set of group runs. Thank you for the help guys.
Upvotes: 1
Views: 1123
Reputation: 14746
You can try leveraging the Beanshell capabilities that TestNG has to offer, to get this done.
That way, irrespective of how you run your test suite :
Your suite file's method selector will always be honoured.
In a nutshell, you need to define a method selector that uses Beanshell as shown below
<?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 -->
Once you have a suite xml file like this, you can specify the group names via the JVM argument -DwhatGroup
.
You can also spice up the beanshell script by choosing to run everything if no groups were found via the JVM argument.
For more information you can refer to my blog post to get a complete understanding of how to work with the beanshell.
Upvotes: 2