TestNG入门——@DataProvider

一 、基本用法

[java]  view plain  copy
  1. package test;  
  2. import org.testng.Assert;  
  3. import org.testng.annotations.DataProvider;  
  4. import org.testng.annotations.Test;  
  5.   
  6.   
  7. public class BetweenValue {  
  8.     //待测试方法  
  9.     public boolean isBetween(int n, int lower, int upper){  
  10.           
  11.         if( n >= lower && n <= upper){  
  12.             return true;  
  13.         }  
  14.         else return false;  
  15.               
  16.     }  
  17.     //测试方法  
  18.     @Test(dataProvider = "range-provider")  
  19.     public void testIsBetween(int n, int lower, int upper, boolean expected){  
  20.         System.out.println("received n "+ n + "lower " + lower + "upper " + upper + " expected" + expected);  
  21.         Assert.assertEquals(expected, isBetween(n,lower,upper));  
  22.           
  23.                   
  24.     }  
  25.       
  26.     //测试方法的数据源  
  27.     @DataProvider(name="range-provider")  
  28.     public Object[][] rangeData(){  
  29.           
  30.         return new Object[][]{{5,5,10,false}};  
  31.         }  
  32.   
  33.       
  34. }  

二、TestNG在调用数据提供者之前会设置两个参数(Method,ITestcontext),这两个参数为提供了某种上下文,然后再决定做什么

1、Method参数(java.lang.reflect.Method), 如下代码显示数据提供者会根据method的值不同而返回不同的数据

[java]  view plain  copy
  1. package test;  
  2. import org.testng.annotations.Test;  
  3.   
  4. import java.lang.reflect.Method;  
  5.   
  6. import org.testng.annotations.DataProvider;  
  7. public class DataMethod {  
  8.     @DataProvider  
  9.     public Object[][] provideNumbers(Method method){  
  10.         Object [][] result =null;  
  11.         if(method.getName().equals("two")){  
  12.             result = new Object[][]{ new Object[]{2}};  
  13.         }else if (method.getName().equals("three")){  
  14.             result = new Object[][]{ new Object[]{3}};  
  15.         }  
  16.         return result;  
  17.     }  
  18.       
  19.     @Test(dataProvider="provideNumbers")  
  20.     public void  two(int param){  
  21.         System.out.println("Method two received:" + param);  
  22.     }  
  23.       
  24.     @Test(dataProvider="provideNumbers")  
  25.     public void three(int param){  
  26.         System.out.println("Method three received:" + param);  
  27.     }  
  28.   
  29. }  
2、ITestcontext, ITestcontext支持的完整的属性内容可以查阅  org.testng.ITestcontext 源码

[java]  view plain  copy
  1. package test;  
  2. import org.testng.annotations.Test;  
  3. import java.util.Arrays;  
  4.   
  5. import org.testng.ITestContext;  
  6. import org.testng.annotations.DataProvider;  
  7. public class DataGroup {  
  8.     @DataProvider  
  9.     public Object[][] provideNumbers(ITestContext context){  
  10.         Object [][] result =null;  
  11.         String[]  groups = context.getIncludedGroups();  
  12.         //String testName = context.getName();  
  13.         //执行的组中包含groupOne则数据为3        
  14.         if(Arrays.asList(groups).contains("groupOne")){  
  15.             result = new Object[][]{ new Object[]{2}};  
  16.         }else {  
  17.             result = new Object[][]{ new Object[]{3}};  
  18.         }  
  19.         return result;  
  20.           
  21.     /*   
  22.      *执行的用例名为"Group Test1"则返回3 
  23.      * if(testName.equals("Group Test1")){ 
  24.             result = new Object[][]{ new Object[]{3}}; 
  25.              
  26.         } 
  27.         return result; 
  28.     }*/  
  29.     }  
  30.       
  31.     @Test(dataProvider="provideNumbers", groups={"groupOne","groupTwo"})  
  32.     public void  two(int param){  
  33.         System.out.println("Method two received:" + param);  
  34.     }  
  35.       
  36.       
  37.   
  38. }  
ITestContext支持的属性列表:
[java]  view plain  copy
  1. package org.testng;  
  2.   
  3. import com.google.inject.Injector;  
  4. import com.google.inject.Module;  
  5.   
  6. import org.testng.internal.ClassImpl;  
  7. import org.testng.xml.XmlTest;  
  8.   
  9. import java.util.Collection;  
  10. import java.util.Date;  
  11. import java.util.List;  
  12.   
  13.   
  14. /** 
  15.  * This class defines a test context which contains all the information 
  16.  * for a given test run.  An instance of this context is passed to the 
  17.  * test listeners so they can query information about their 
  18.  * environment. 
  19.  * 
  20.  * @author Cedric Beust, Aug 6, 2004 
  21.  */  
  22. public interface ITestContext extends IAttributes {  
  23.   
  24.   /** 
  25.    * The name of this test. 
  26.    */  
  27.   public String getName();  
  28.   
  29.   /** 
  30.    * When this test started running. 
  31.    */  
  32.   public Date getStartDate();  
  33.   
  34.   /** 
  35.    * When this test stopped running. 
  36.    */  
  37.   public Date getEndDate();  
  38.   
  39.   /** 
  40.    * @return A list of all the tests that run successfully. 
  41.    */  
  42.   public IResultMap getPassedTests();  
  43.   
  44.   /** 
  45.    * @return A list of all the tests that were skipped 
  46.    */  
  47.   public IResultMap  getSkippedTests();  
  48.   
  49.   /** 
  50.    * @return A list of all the tests that failed but are being ignored because 
  51.    * annotated with a successPercentage. 
  52.    */  
  53.   public IResultMap  getFailedButWithinSuccessPercentageTests();  
  54.   
  55.   /** 
  56.    * @return A map of all the tests that passed, indexed by 
  57.    * their ITextMethor. 
  58.    * 
  59.    * @see org.testng.ITestNGMethod 
  60.    */  
  61.   public IResultMap getFailedTests();  
  62.   
  63.   /** 
  64.    * @return All the groups that are included for this test run. 
  65.    */  
  66.   public String[] getIncludedGroups();  
  67.   
  68.   /** 
  69.    * @return All the groups that are excluded for this test run. 
  70.    */  
  71.   public String[] getExcludedGroups();  
  72.   
  73.   /** 
  74.    * @return Where the reports will be generated. 
  75.    */  
  76.   public String getOutputDirectory();  
  77.   
  78.   /** 
  79.    * @return The Suite object that was passed to the runner 
  80.    * at start-up. 
  81.    */  
  82.   public ISuite getSuite();  
  83.   
  84.   /** 
  85.    * @return All the test methods that were run. 
  86.    */  
  87.   public ITestNGMethod[] getAllTestMethods();  
  88.   
  89.   /** 
  90.    * @return The host where this test was run, or null if it was run locally.  The 
  91.    * returned string has the form:  host:port 
  92.    */  
  93.   public String getHost();  
  94.   
  95.   /** 
  96.    * @return All the methods that were not included in this test run. 
  97.    */  
  98.   public Collection<ITestNGMethod> getExcludedMethods();  
  99.   
  100.   /** 
  101.    * Retrieves information about the successful configuration method invocations. 
  102.    */  
  103.   public IResultMap getPassedConfigurations();  
  104.   
  105.   /** 
  106.    * Retrieves information about the skipped configuration method invocations. 
  107.    */  
  108.   public IResultMap getSkippedConfigurations();  
  109.   
  110.   /** 
  111.    * Retrieves information about the failed configuration method invocations. 
  112.    */  
  113.   public IResultMap getFailedConfigurations();  
  114.   
  115.   /** 
  116.    * @return the current XmlTest. 
  117.    */  
  118.   public XmlTest getCurrentXmlTest();  
  119.   
  120.   public List<Module> getGuiceModules(Class<? extends Module> cls);  
  121.   
  122.   public Injector getInjector(List<Module> moduleInstances);  
  123.   Injector getInjector(IClass iClass);  
  124.   public void addInjector(List<Module> moduleInstances, Injector injector);  
  125. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值