TestNG DataProvider parametric test of

When the need for parametric testing TestNG, @Parameters may be used to pass parameters and @DataProvider annotations. This paper describes the use of @DataProvider.


First define a DataProvider, designated by the name attribute for the test data;

Then Object [] [] (the number of data is explicitly test) method returns the test data of configuration parameters;

Test data specified last name dataProvider property test method, the test data and parameter correspondence sequential method.

E.g:

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderTest {
	
	@Test(dataProvider = "data")
	public void dataProviderTest(String name, int age, String hobby) {
		System.out.println(name + " " + age + " " + hobby);
	}
	
	@DataProvider(name = "data")
	Object[][] getData(){
		return new Object[][] {
			{"June", 22, "Sing"},
			{"Jack", 22, "Football"},
			{"Duke", 22, "Chess"}
		};
	}
}

Test Results:

June 22 Sing
Jack 22 Football
Duke 22 Chess

Further, the test method may be configured as a parameter in the method of test data for different data structure different test methods, for example as follows:

import java.lang.reflect.Method;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderTest {
		
	@Test(dataProvider = "methodData")
	public void dataTest1(String name, int age, String hobby) {
		System.out.println(name + " " + age + " " + hobby);
	}
	
	@Test(dataProvider = "methodData")
	public void dataTest2(String name, int age, String hobby) {
		System.out.println(name + " " + age + " " + hobby);
	}
	
	@DataProvider(name = "methodData")
	Object[][] methodDataProvider(Method method){
		Object[][] data = null;
		String methodName = method.getName();
		if (methodName.equals("dataTest1")) {
			data = new Object[][] {
				{"Rose", 33, "Dance"}
			};
		}
		else if (methodName.equals("dataTest2")) {
			data = new Object[][] {
				{"Tom", 6, "Tennis"}
			};
		}
		return data;
	}
}

Test Results:

Rose 33 Dance
Tom 6 Tennis

 

Published 37 original articles · won praise 47 · Views 100,000 +

Guess you like

Origin blog.csdn.net/u013378642/article/details/97011773