1 package test.dependent; 2 3 import org.testng.Assert; 4 import org.testng.TestNG; 5 import org.testng.annotations.ExpectedExceptions; 6 import org.testng.annotations.Test; 7 8 import test.BaseTest; 9 import test.SimpleBaseTest; 10 11 import java.util.List; 12 13 public class DependentTest extends BaseTest { 14 15 @Test simpleSkip()16 public void simpleSkip() { 17 addClass(SampleDependent1.class.getName()); 18 run(); 19 String[] passed = {}; 20 String[] failed = { "fail" }; 21 String[] skipped = { "shouldBeSkipped" }; 22 verifyTests("Passed", passed, getPassedTests()); 23 verifyTests("Failed", failed, getFailedTests()); 24 verifyTests("Skipped", skipped, getSkippedTests()); 25 } 26 27 @Test dependentMethods()28 public void dependentMethods() { 29 addClass(SampleDependentMethods.class.getName()); 30 run(); 31 String[] passed = { "oneA", "oneB", "secondA", "thirdA", "canBeRunAnytime" }; 32 String[] failed = {}; 33 String[] skipped = {}; 34 verifyTests("Passed", passed, getPassedTests()); 35 verifyTests("Failed", failed, getFailedTests()); 36 verifyTests("Skipped", skipped, getSkippedTests()); 37 } 38 39 @Test dependentMethodsWithSkip()40 public void dependentMethodsWithSkip() { 41 addClass(SampleDependentMethods4.class.getName()); 42 run(); 43 String[] passed = { "step1", }; 44 String[] failed = { "step2", }; 45 String[] skipped = { "step3" }; 46 verifyTests("Passed", passed, getPassedTests()); 47 verifyTests("Failed", failed, getFailedTests()); 48 verifyTests("Skipped", skipped, getSkippedTests()); 49 } 50 51 @Test 52 @ExpectedExceptions({ org.testng.TestNGException.class }) dependentMethodsWithNonExistentMethod()53 public void dependentMethodsWithNonExistentMethod() { 54 addClass(SampleDependentMethods5.class.getName()); 55 run(); 56 String[] passed = { "step1", "step2" }; 57 String[] failed = {}; 58 String[] skipped = {}; 59 verifyTests("Passed", passed, getPassedTests()); 60 verifyTests("Failed", failed, getFailedTests()); 61 verifyTests("Skipped", skipped, getSkippedTests()); 62 } 63 64 @Test(expectedExceptions = org.testng.TestNGException.class) dependentMethodsWithCycle()65 public void dependentMethodsWithCycle() { 66 addClass(SampleDependentMethods6.class.getName()); 67 run(); 68 } 69 70 @Test(expectedExceptions = org.testng.TestNGException.class) dependentGroupsWithCycle()71 public void dependentGroupsWithCycle() { 72 addClass("test.dependent.SampleDependentMethods7"); 73 run(); 74 } 75 76 @Test multipleSkips()77 public void multipleSkips() { 78 addClass(MultipleDependentSampleTest.class.getName()); 79 run(); 80 String[] passed = { "init", }; 81 String[] failed = { "fail", }; 82 String[] skipped = { "skip1", "skip2" }; 83 verifyTests("Passed", passed, getPassedTests()); 84 verifyTests("Failed", failed, getFailedTests()); 85 verifyTests("Skipped", skipped, getSkippedTests()); 86 } 87 88 @Test instanceDependencies()89 public void instanceDependencies() { 90 addClass(InstanceSkipSampleTest.class.getName()); 91 run(); 92 verifyInstanceNames("Passed", getPassedTests(), 93 new String[] { "f#1", "f#3", "g#1", "g#3"}); 94 verifyInstanceNames("Failed", getFailedTests(), 95 new String[] { "f#2" }); 96 verifyInstanceNames("Skipped", getSkippedTests(), 97 new String[] { "g#" }); 98 } 99 100 @Test dependentWithDataProvider()101 public void dependentWithDataProvider() { 102 TestNG tng = SimpleBaseTest.create(DependentWithDataProviderSampleTest.class); 103 tng.setGroupByInstances(true); 104 List<String> log = DependentWithDataProviderSampleTest.m_log; 105 log.clear(); 106 tng.run(); 107 for (int i = 0; i < 12; i += 4) { 108 String[] s = log.get(i).split("#"); 109 String instance = s[1]; 110 Assert.assertEquals(log.get(i), "prepare#" + instance); 111 Assert.assertEquals(log.get(i + 1), "test1#" + instance); 112 Assert.assertEquals(log.get(i + 2), "test2#" + instance); 113 Assert.assertEquals(log.get(i + 3), "clean#" + instance); 114 } 115 } 116 } // DependentTest 117 118