1package test.dataprovider;
2
3import org.testng.Assert;
4import org.testng.TestListenerAdapter;
5import org.testng.TestNG;
6import org.testng.annotations.Test;
7
8public class TestContextTest {
9
10  @Test
11  public void verifyTen() {
12    verify("10", "verifyTen", 1, 0);
13  }
14
15  @Test
16  public void verifyFive() {
17    verify("5", "verifyFive", 1, 0);
18  }
19
20  @Test
21  public void verifySix() {
22    // Not including any group, so the two test methods should fail
23    verify(null, null, 0, 2);
24  }
25
26  private void verify(String groupName, String passed, int passedCount, int failedCount) {
27    TestNG tng = new TestNG();
28    tng.setVerbose(0);
29    tng.setTestClasses(new Class[] { TestContextSampleTest.class });
30    if (groupName != null) {
31      tng.setGroups(groupName);
32    }
33    TestListenerAdapter al = new TestListenerAdapter();
34    tng.addListener(al);
35    tng.run();
36
37    if (passedCount > 0) {
38      Assert.assertEquals(al.getPassedTests().size(), passedCount);
39      Assert.assertEquals(al.getPassedTests().get(0).getMethod().getMethodName(), passed);
40    }
41
42    if (failedCount > 0) {
43      Assert.assertEquals(al.getFailedTests().size(), failedCount);
44    }
45  }
46}
47