You can do this easily via a Beanshell method selector in TestNG. Here's a sample.
My test classes look like below
package com.rationaleemotions.stackoverflow.qn48163083;
import org.testng.annotations.Test;
public class SampleA {
@Test
public void testForAndroid() {
System.err.println(getClass().getName() + ".testForAndroid() ran");
}
@Test
public void testForWindows() {
System.err.println(getClass().getName() + ".testForWindows() ran");
}
}
package com.rationaleemotions.stackoverflow.qn48163083;
import org.testng.annotations.Test;
public class SampleB {
@Test
public void testForAndroid() {
System.err.println(getClass().getName() + ".testForAndroid() ran");
}
@Test
public void testForWindows() {
System.err.println(getClass().getName() + ".testForWindows() ran");
}
}
Here's my suite xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="1265_Suite" parallel="false" verbose="4">
<test name="92" parallel="false">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[
methodPattern = System.getProperty("methodPattern");
(methodPattern == null || methodPattern.trim().isEmpty()) || testngMethod.getMethodName().contains(methodPattern);
]]>
</script>
</method-selector>
</method-selectors>
<packages>
<package name="com.rationaleemotions.stackoverflow.qn48163083"/>
</packages>
</test>
</suite>
Here I am getting the method name to be executed via the JVM argument -DmethodPattern
. If nothing was provided we default to running everything.
For more information on how to work with beanshells in TestNG take a look at my blog post here.
Here's the output of a sample execution with -DmethodPattern=testForAndroid
...
... TestNG 6.13.1 by Cédric Beust (cedric@beust.com)
...
com.rationaleemotions.stackoverflow.qn48163083.SampleA.testForAndroid() ran
com.rationaleemotions.stackoverflow.qn48163083.SampleB.testForAndroid() ran
===== Invoked methods
SampleA.testForAndroid()[pri:0, instance:com.rationaleemotions.stackoverflow.qn48163083.SampleA@df27fae] 233996206
SampleB.testForAndroid()[pri:0, instance:com.rationaleemotions.stackoverflow.qn48163083.SampleB@2f0a87b3] 789219251
=====
PASSED: testForAndroid
PASSED: testForAndroid
===============================================
92
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
1265_Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================