Home | History | Annotate | Download | only in dataprovider
      1 package test.dataprovider;
      2 
      3 import org.testng.Assert;
      4 import org.testng.ITestContext;
      5 import org.testng.annotations.DataProvider;
      6 import org.testng.annotations.Test;
      7 
      8 /**
      9  * Tests that when a DataProvider is declared with an ITestContext,
     10  * this parameter is correctly passed.
     11  *
     12  * Created on Dec 28, 2006
     13  * @author <a href="mailto:cedric (at) beust.com">Cedric Beust</a>
     14  */
     15 public class TestContextSampleTest {
     16 
     17   /**
     18    * @return As many parameters as the name of the included group
     19    */
     20   @DataProvider(name = "testContext")
     21   public Object[][] createContext(ITestContext ctx) {
     22 //    ppp("CONTEXT:" + ctx);
     23     String[] groups = ctx.getIncludedGroups();
     24 
     25     int n = groups.length > 0 ? new Integer(groups[0]): 0;
     26     Object[] result = new Object[n];
     27     for (int i = 0; i < n; i++) {
     28       result[i] = "foo";
     29     }
     30 
     31     return new Object[][] {
     32         new Object[] { result },
     33     };
     34   }
     35 
     36   private static void ppp(String s) {
     37     System.out.println("[TestContextSampleTest] " + s);
     38   }
     39 
     40   @Test(dataProvider = "testContext", groups="10")
     41   public void verifyTen(Object[] objects) {
     42     Assert.assertEquals(objects.length, 10);
     43   }
     44 
     45   @Test(dataProvider = "testContext", groups="5")
     46   public void verifyFive(Object[] objects) {
     47     Assert.assertEquals(objects.length, 5);
     48   }
     49 
     50 }
     51