10
Sep

Running TestNG tests with specific groups from VM Arguments



Running TestNG tests with specific groups from VM Arguments
=============================================================

Pre-requisites Maven Dependencies:

		<!-- https://mvnrepository.com/artifact/org.testng/testng -->
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>7.0.0</version>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.beanshell/bsh -->
		<dependency>
			<groupId>org.beanshell</groupId>
			<artifactId>bsh</artifactId>
			<version>2.0b5</version>
		</dependency>



Step 1: create your regular TestNG scripts
============================================


import org.testng.annotations.Test;

public class ActualTests {

	@Test(groups = { "regression" })
	public void myFunction() {
		System.out.println("regression");
	}

	@Test(groups = { "sanity" })
	public void myFunction2() {
		System.out.println("Sanity ");
	}
}


Step 2: Configure your testng.xml with BeanShell script
=========================================================

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
	<test name="Dynamic Groups execution with TestNG">
		<method-selectors>
			<method-selector>
				<script language="beanshell">
					<!-- groupToRun is a VM argument. -->
				<![CDATA[groupNames = System.getProperty("groupToRun");
				groups.containsKey(groupNames);
				]]>
				</script>
			</method-selector>
		</method-selectors>
		<classes>
			<class name="testNG.Groups.Examples.ActualTests" />
		</classes>
	</test>
</suite>

Step 3: Run testng.xml with following options:

Test: (Suite)  	C:\Users\srasz\eclipse-workspace\TestNGPractice\dynamicGroupsExecution.xml
VM agruments:	-DgroupToRun="sanity"


Output:
========
[RemoteTestNG] detected TestNG version 7.0.0
Sanity 

===============================================
Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================