1 package test.dataprovider; 2 3 import org.testng.Assert; 4 import org.testng.TestListenerAdapter; 5 import org.testng.TestNG; 6 import org.testng.annotations.Test; 7 8 public class TestContextTest { 9 10 @Test verifyTen()11 public void verifyTen() { 12 verify("10", "verifyTen", 1, 0); 13 } 14 15 @Test verifyFive()16 public void verifyFive() { 17 verify("5", "verifyFive", 1, 0); 18 } 19 20 @Test verifySix()21 public void verifySix() { 22 // Not including any group, so the two test methods should fail 23 verify(null, null, 0, 2); 24 } 25 verify(String groupName, String passed, int passedCount, int failedCount)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