03
Sep

@DataProvider customization to respond to data needs of different testing environments

@DataProvider customization to respond to data needs of different testing environments


Generally as a tester, one needs to test same tests on different environments (Integration, Staging, Production to name a few).
In such scenario, it is expected to customize @DataProvider implementation to respond test data based on Environment, where tests needs to be executed.

Following code snippet demonstrates how to customize @DataProvider for different test environment needs

Step 1: Create your custom annotation for method level
=======================================================

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodXY {
	public String Environment();

}

Step 2: Create your @DataProvider implementation to process methods with above Annotation
==========================================================================================

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.testng.annotations.DataProvider;
import org.testng.annotations.TestInstance;

public class LocalDataProvider {
	@DataProvider(name = "data-provider")
	public static Object[][] getData(@TestInstance Object object) {

		// Create a list of all methods in calling class
		List<Method> allMethods = new ArrayList<Method>(Arrays.asList(object.getClass().getMethods()));

		// filter only methods where custom annotation is present
		for (Method method : allMethods) {
			if (method.isAnnotationPresent(MethodXY.class)) {
				MethodXY annotInstance = method.getAnnotation(MethodXY.class);
				if (annotInstance.Environment().equalsIgnoreCase("Staging")) {
					return new Object[][] { { 1 }, { 2 } };
				}

			}
		}
		return new Object[][] { { 3 }, { 4 } };

	}
}

Step 3: Create your TestNG tests which specify Environments to execute against
===============================================================================

import org.testng.annotations.Test;

public class FirstTestClass {
	@MethodXY(Environment="Staging")
	@Test(dataProvider = "data-provider", dataProviderClass = LocalDataProvider.class)
	public void testmethod(int number) {
		System.err.println("Value = " + number);
	}
}



import org.testng.annotations.Test;

public class SecondTestClass {
	@Test(dataProvider = "data-provider", dataProviderClass = LocalDataProvider.class)
	public void testmethod(int number) {
		System.err.println("Value = " + number);
	}
}


Step 4: Create your testng.xml to run all tests
================================================

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
      <class name="testNG.DataPrvdr.examples.SecondTestClass"/>
      <class name="testNG.DataPrvdr.examples.FirstTestClass"/>
    </classes>
  </test> 
</suite> 


Output:
=======
[TestNG] Running:
  C:\SRTemp\guice\guice\testngDP.xml

Value = 3
Value = 4
Value = 1
Value = 2


http://defectracker.com/wp-content/uploads/2019/09/EnvironmentBasedTestData7z.txt