Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Oct 04, 2020 16:15

A typical testng.xml file can have more than one test tag, such as <test>. At times, we need to run specific tests from a testng.xml file. To select specific tests from a testng.xml file, you can do any of the following:

  • Solution 1: Use an enabled parameter of the <test> tag and set it to false. TestNG will ignore those test tags.
  • Solution 2: Use TestNG's IMethodInterceptor interface to filter tests based on the test name. Following are the steps to implement IMethodInterceptor in Quantum project:

Solution 1

Following is an example of using an enabled parameter of the <test> tag and setting it to false.

Test Tag
	<!-- This test is excluded from Execution -->
	<!-- enabled parameter is set to false -->
	<test name="Web Scenarios Android Test" enabled="false" thread-count="10">

		<parameter name="driver.capabilities.model" value="Galaxy.*"></parameter>
		<groups>
			<run>
				<include name="@Web" />
			</run>
		</groups>
		<classes>
			<class
				name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
		</classes>
	</test>

	<!-- This test will be included in Execution -->
		<!-- enabled parameter is set to true -->
	<test name="Web Scenarios iOS Test" enabled="true" thread-count="10">
		<parameter name="driver.capabilities.model" value="iPhone.*"></parameter>
		<groups>
			<run>
				<include name="@Web" />
			</run>
		</groups>
		<classes>
			<class
				name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
		</classes>
	</test>

Solution 2

To use TestNG's IMethodInterceptor interface to filter tests based on the test name:

  1. Create a package under src/main/java. If  the path does not exists, use com.perfecto.listeners.
  2. In the package created in step 1, create the following class: TestFilterListener
  3. Copy-paste the following code into the class created in step 2. The java file is also attached to this article: TestFilterListener.java

    TestFilterListener.java
    package com.perfecto.listeners;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.regex.Pattern;
    import org.testng.IMethodInstance;
    import org.testng.IMethodInterceptor;
    import org.testng.ITestContext;
    
    public class TestFilterListener implements IMethodInterceptor {
    	
    	private static Set<Pattern> patterns;
    	private boolean includeTest(String testsToInclude,String currentTestName) {
    		boolean result = false;
    		if(patterns==null) {
    			patterns = new HashSet<>();
    			String[] testPatterns = testsToInclude.split(",");
    			for(String testPattern:testPatterns) {
    				patterns.add(Pattern.compile(testPattern, Pattern.CASE_INSENSITIVE));
    			}
    		}
    
    		for(Pattern pattern:patterns) {
    			if(pattern.matcher(currentTestName).find()) {
    				result = true;
    				break;
    			}
    		}
    		return result;
    	}
    
    	@Override
    
    	public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    
    		String testNames = System.getenv("test_names_to_include");
    		if(testNames==null || testNames.trim().isEmpty()) {
    			return methods;
    		}else {
    			if(includeTest(testNames.trim(), context.getName())) {
    				return methods;
    			}else {
    				return new ArrayList<IMethodInstance>();
    			}
    		}
    	}
    }
  4. Register the listener in all testng.xml files of your project, as follows:

    	<listeners>
    		<listener class-name="com.quantum.listeners.QuantumReportiumListener" />
    		<listener class-name="com.perfecto.listeners.TestFilterListener"/>
    	</listeners>
  5. Add an environment variable or create a Jenkins parameter called test_names_to_includetest_names_to_include takes comma-separated test names and regular expressions.
    Consider the above testng.xml file <test> tags:

    ConditionSet test_names_to_include to
    Run Specific Test. For example - "Web Scenarios Android Test"Web Scenarios Android Test
    Run test by sub name - All test having name "Web Scenarios"Web Scenarios.*
    Run test having Android or IOS in its name "Web Scenarios Android Test" and Web Scenarios iOS Test.*android.*,.*ios.*

To select a test from multiple testng.xml files:

  1. Open the pom.xml file.
  2. Locate the maven-surefire-plugin plugin tag under the <build>/<plugins> tag.
  3. Locate the <suiteXmlFile> tag under the maven-surefire-plugin tag.
  4. Add <suiteXmlFile> tags to include more than one testng.xml file.

    Sample Plugin
    <suiteXmlFiles>
    	<suiteXmlFile>${testngXmlDir}/${testngXmlFile1}</suiteXmlFile>
    	<suiteXmlFile>${testngXmlDir}/${testngXmlFile2}</suiteXmlFile>
    </suiteXmlFiles>