一 、基本用法
package test;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class BetweenValue {
//待测试方法
public boolean isBetween(int n, int lower, int upper){
if( n >= lower && n <= upper){
return true;
}
else return false;
}
//测试方法
@Test(dataProvider = "range-provider")
public void testIsBetween(int n, int lower, int upper, boolean expected){
System.out.println("received n "+ n + "lower " + lower + "upper " + upper + " expected" + expected);
Assert.assertEquals(expected, isBetween(n,lower,upper));
}
//测试方法的数据源
@DataProvider(name="range-provider")
public Object[][] rangeData(){
return new Object[][]{{5,5,10,false}};
}
}
二、TestNG在调用数据提供者之前会设置两个参数(Method,ITestcontext),这两个参数为提供了某种上下文,然后再决定做什么
1、Method参数(java.lang.reflect.Method), 如下代码显示数据提供者会根据method的值不同而返回不同的数据
package test;
import org.testng.annotations.Test;
import java.lang.reflect.Method;
import org.testng.annotations.DataProvider;
public class DataMethod {
@DataProvider
public Object[][] provideNumbers(Method method){
Object [][] result =null;
if(method.getName().equals("two")){
result = new Object[][]{ new Object[]{2}};
}else if (method.getName().equals("three")){
result = new Object[][]{ new Object[]{3}};
}
return result;
}
@Test(dataProvider="provideNumbers")
public void two(int param){
System.out.println("Method two received:" + param);
}
@Test(dataProvider="provideNumbers")
public void three(int param){
System.out.println("Method three received:" + param);
}
}
2、ITestcontext, ITestcontext支持的完整的属性内容可以查阅 org.testng.ITestcontext 源码
package test;
import org.testng.annotations.Test;
import java.util.Arrays;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
public class DataGroup {
@DataProvider
public Object[][] provideNumbers(ITestContext context){
Object [][] result =null;
String[] groups = context.getIncludedGroups();
//String testName = context.getName();
//执行的组中包含groupOne则数据为3
if(Arrays.asList(groups).contains("groupOne")){
result = new Object[][]{ new Object[]{2}};
}else {
result = new Object[][]{ new Object[]{3}};
}
return result;
/*
*执行的用例名为"Group Test1"则返回3
* if(testName.equals("Group Test1")){
result = new Object[][]{ new Object[]{3}};
}
return result;
}*/
}
@Test(dataProvider="provideNumbers", groups={"groupOne","groupTwo"})
public void two(int param){
System.out.println("Method two received:" + param);
}
}
ITestContext支持的属性列表:
package org.testng;
import com.google.inject.Injector;
import com.google.inject.Module;
import org.testng.internal.ClassImpl;
import org.testng.xml.XmlTest;
import java.util.Collection;
import java.util.Date;
import java.util.List;
/**
* This class defines a test context which contains all the information
* for a given test run. An instance of this context is passed to the
* test listeners so they can query information about their
* environment.
*
* @author Cedric Beust, Aug 6, 2004
*/
public interface ITestContext extends IAttributes {
/**
* The name of this test.
*/
public String getName();
/**
* When this test started running.
*/
public Date getStartDate();
/**
* When this test stopped running.
*/
public Date getEndDate();
/**
* @return A list of all the tests that run successfully.
*/
public IResultMap getPassedTests();
/**
* @return A list of all the tests that were skipped
*/
public IResultMap getSkippedTests();
/**
* @return A list of all the tests that failed but are being ignored because
* annotated with a successPercentage.
*/
public IResultMap getFailedButWithinSuccessPercentageTests();
/**
* @return A map of all the tests that passed, indexed by
* their ITextMethor.
*
* @see org.testng.ITestNGMethod
*/
public IResultMap getFailedTests();
/**
* @return All the groups that are included for this test run.
*/
public String[] getIncludedGroups();
/**
* @return All the groups that are excluded for this test run.
*/
public String[] getExcludedGroups();
/**
* @return Where the reports will be generated.
*/
public String getOutputDirectory();
/**
* @return The Suite object that was passed to the runner
* at start-up.
*/
public ISuite getSuite();
/**
* @return All the test methods that were run.
*/
public ITestNGMethod[] getAllTestMethods();
/**
* @return The host where this test was run, or null if it was run locally. The
* returned string has the form: host:port
*/
public String getHost();
/**
* @return All the methods that were not included in this test run.
*/
public Collection<ITestNGMethod> getExcludedMethods();
/**
* Retrieves information about the successful configuration method invocations.
*/
public IResultMap getPassedConfigurations();
/**
* Retrieves information about the skipped configuration method invocations.
*/
public IResultMap getSkippedConfigurations();
/**
* Retrieves information about the failed configuration method invocations.
*/
public IResultMap getFailedConfigurations();
/**
* @return the current XmlTest.
*/
public XmlTest getCurrentXmlTest();
public List<Module> getGuiceModules(Class<? extends Module> cls);
public Injector getInjector(List<Module> moduleInstances);
Injector getInjector(IClass iClass);
public void addInjector(List<Module> moduleInstances, Injector injector);
}