1 package test.alwaysrun; 2 3 import static org.testng.Assert.assertTrue; 4 5 import org.testng.TestListenerAdapter; 6 import org.testng.TestNG; 7 import org.testng.annotations.Test; 8 9 import test.SimpleBaseTest; 10 import testhelper.OutputDirectoryPatch; 11 12 public class AlwaysRunTest extends SimpleBaseTest { 13 14 @Test withAlwaysRunAfter()15 public void withAlwaysRunAfter() { 16 TestListenerAdapter tla = new TestListenerAdapter(); 17 TestNG testng = create(); 18 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); 19 testng.setTestClasses(new Class[] { AlwaysRunAfter1.class }); 20 testng.addListener(tla); 21 testng.run(); 22 assertTrue(AlwaysRunAfter1.success(), "afterTestMethod should have run"); 23 } 24 25 @Test withoutAlwaysRunAfter()26 public void withoutAlwaysRunAfter() { 27 TestListenerAdapter tla = new TestListenerAdapter(); 28 TestNG testng = create(); 29 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); 30 testng.setTestClasses(new Class[] { AlwaysRunAfter2.class }); 31 testng.addListener(tla); 32 testng.run(); 33 assertTrue(AlwaysRunAfter2.success(), "afterTestMethod should not have run"); 34 } 35 36 @Test withoutAlwaysRunBefore()37 public void withoutAlwaysRunBefore() { 38 TestListenerAdapter tla = new TestListenerAdapter(); 39 TestNG testng = create(); 40 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory()); 41 testng.setTestClasses(new Class[] { AlwaysRunBefore1.class }); 42 testng.setGroups("A"); 43 testng.addListener(tla); 44 testng.run(); 45 assertTrue(AlwaysRunBefore1.success(), "before alwaysRun methods should have been run"); 46 } 47 ppp(String s)48 private static void ppp(String s) { 49 System.out.println("[AlwaysRunTest] " + s); 50 } 51 } 52