I came across a requirement to validate the group names for the TestNG tests. The validation is required to ensure that the QA engineers tag their tests with only the list of published group names. Since group names in TestNG are Strings, which can accept any value, it becomes difficult to pick all tests belonging to a particular category unless the group names are validated.

For example, if there are 50,000 tests which can be grouped under three buckets, as ‘ui’ and ‘backend’ and ‘cli’, it is common for engineer to write various names for these buckets like ‘gui’, ‘UI’, ‘browser’, etc. To avoid such variations in naming, the validation of group name is required.

Here is the code to validate groups before running tests.

package com.example.testng.listeners;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;

public class GroupValidator implements IMethodInterceptor {
	/**
	 * Checks if the group names of the list of tests being run are valid.
	 */
	@Override
	public List intercept(List methods,
			ITestContext context) {
		/* Put your valid group names in a map so that it is easy to validate
		 * One can also put the list in an external file and read it here.
		 */
		Map validGroupNames = new HashMap();
		validGroupNames.put("ui", true);
                validGroupNames.put("backend", true);
                validGroupNames.put("cli", true);
		for(IMethodInstance mInstance: methods) {
			ITestNGMethod method = mInstance.getMethod();
			if (method.isTest()) {
				System.out.println("Validating: " + method.getMethodName());
				for(String group : method.getGroups()) {
					if (! validGroupNames.containsKey(group)) {
						throw new RuntimeException("Group name validation failed. Cannoot run the tests");
					}
				}
			}
		}
		return methods;
	}
}

Add the above class as listener to your testng and it will validate the group names of the test methods.
NOTE that the code above allows a @Test without any group. The validation is only done when there is a group name specified.