1 package test.methodselectors; 2 3 import org.testng.Assert; 4 import org.testng.ITestResult; 5 import org.testng.TestListenerAdapter; 6 import org.testng.TestNG; 7 import org.testng.annotations.BeforeMethod; 8 import org.testng.annotations.Test; 9 10 import test.SimpleBaseTest; 11 import testhelper.OutputDirectoryPatch; 12 13 import java.util.ArrayList; 14 import java.util.List; 15 16 public class CommandLineTest extends SimpleBaseTest { 17 18 private String[] ARG_WITHOUT_CLASSES = 19 new String[]{ 20 "-log", "0", 21 "-d", OutputDirectoryPatch.getOutputDirectory(), 22 "-methodselectors", "", 23 "" 24 }; 25 26 private String[] ARG_WITH_GROUPS = 27 new String[]{ 28 "-log", "0", 29 "-d", OutputDirectoryPatch.getOutputDirectory(), 30 "-testclass", "test.methodselectors.SampleTest", 31 "-methodselectors", "", 32 "-groups", "" 33 }; 34 35 private String[] ARG_WITHOUT_GROUPS = 36 new String[]{ 37 "-log", "0", 38 "-d", OutputDirectoryPatch.getOutputDirectory(), 39 "-testclass", "test.methodselectors.SampleTest", 40 "-methodselectors", "", 41 }; 42 43 private TestListenerAdapter tla; 44 45 @BeforeMethod setup()46 public void setup() { 47 ppp("setup()"); 48 tla = new TestListenerAdapter(); 49 } 50 51 @Test commandLineNegativePriorityAllGroups()52 public void commandLineNegativePriorityAllGroups() { 53 ppp("commandLineNegativePriorityAllGroups()"); 54 ARG_WITHOUT_GROUPS[7] = "test.methodselectors.AllTestsMethodSelector:-1"; 55 TestNG.privateMain(ARG_WITHOUT_GROUPS, tla); 56 String[] passed = { 57 "test1", "test2", "test3" 58 }; 59 String[] failed = { 60 }; 61 verifyTests("Passed", passed, tla.getPassedTests()); 62 verifyTests("Failed", failed, tla.getFailedTests()); 63 } 64 65 @Test commandLineNegativePriorityGroup2()66 public void commandLineNegativePriorityGroup2() { 67 ppp("commandLineNegativePriorityGroup2()"); 68 ARG_WITHOUT_GROUPS[7] = "test.methodselectors.Test2MethodSelector:-1"; 69 TestNG.privateMain(ARG_WITHOUT_GROUPS, tla); 70 String[] passed = { 71 "test2" 72 }; 73 String[] failed = { 74 }; 75 verifyTests("Passed", passed, tla.getPassedTests()); 76 verifyTests("Failed", failed, tla.getFailedTests()); 77 } 78 79 @Test commandLineLessThanPriorityTest1Test()80 public void commandLineLessThanPriorityTest1Test() { 81 ppp("commandLineLessThanPriorityTest1Test()"); 82 ARG_WITH_GROUPS[7] = "test.methodselectors.Test2MethodSelector:5"; 83 ARG_WITH_GROUPS[9] = "test1"; 84 TestNG.privateMain(ARG_WITH_GROUPS, tla); 85 String[] passed = { 86 "test1", "test2" 87 }; 88 String[] failed = { 89 }; 90 verifyTests("Passed", passed, tla.getPassedTests()); 91 verifyTests("Failed", failed, tla.getFailedTests()); 92 } 93 94 @Test commandLineGreaterThanPriorityTest1Test2()95 public void commandLineGreaterThanPriorityTest1Test2() { 96 ppp("commandLineGreaterThanPriorityTest1Test2()"); 97 ARG_WITH_GROUPS[7] = "test.methodselectors.Test2MethodSelector:15"; 98 ARG_WITH_GROUPS[9] = "test1"; 99 TestNG.privateMain(ARG_WITH_GROUPS, tla); 100 String[] passed = { 101 "test2" 102 }; 103 String[] failed = { 104 }; 105 verifyTests("Passed", passed, tla.getPassedTests()); 106 verifyTests("Failed", failed, tla.getFailedTests()); 107 } 108 @Test commandLineLessThanPriorityAllTests()109 public void commandLineLessThanPriorityAllTests() { 110 ppp("commandLineLessThanPriorityAllTests()"); 111 ARG_WITH_GROUPS[7] = "test.methodselectors.AllTestsMethodSelector:5"; 112 ARG_WITH_GROUPS[9] = "test1"; 113 TestNG.privateMain(ARG_WITH_GROUPS, tla); 114 String[] passed = { 115 "test1", "test2", "test3" 116 }; 117 String[] failed = { 118 }; 119 verifyTests("Passed", passed, tla.getPassedTests()); 120 verifyTests("Failed", failed, tla.getFailedTests()); 121 } 122 123 @Test commandLineMultipleSelectors()124 public void commandLineMultipleSelectors() { 125 ppp("commandLineMultipleSelectors()"); 126 ARG_WITH_GROUPS[7] = "test.methodselectors.NoTestSelector:7,test.methodselectors.Test2MethodSelector:5"; 127 ARG_WITH_GROUPS[9] = "test1"; 128 TestNG.privateMain(ARG_WITH_GROUPS, tla); 129 String[] passed = { 130 "test1", "test2" 131 }; 132 String[] failed = { 133 }; 134 verifyTests("Passed", passed, tla.getPassedTests()); 135 verifyTests("Failed", failed, tla.getFailedTests()); 136 } 137 138 @Test commandLineNoTest1Selector()139 public void commandLineNoTest1Selector() { 140 ppp("commandLineNoTest1Selector()"); 141 ARG_WITHOUT_GROUPS[7] = "test.methodselectors.NoTest1MethodSelector:5"; 142 TestNG.privateMain(ARG_WITHOUT_GROUPS, tla); 143 String[] passed = { 144 "test2", "test3" 145 }; 146 String[] failed = { 147 }; 148 verifyTests("Passed", passed, tla.getPassedTests()); 149 verifyTests("Failed", failed, tla.getFailedTests()); 150 } 151 152 @Test commandLineTestWithXmlFile()153 public void commandLineTestWithXmlFile() { 154 ppp("commandLineTestWithXmlFile()"); 155 ARG_WITHOUT_CLASSES[5] = "test.methodselectors.NoTest1MethodSelector:5"; 156 ARG_WITHOUT_CLASSES[6] = getPathToResource("testng-methodselectors.xml"); 157 TestNG.privateMain(ARG_WITHOUT_CLASSES, tla); 158 String[] passed = { 159 "test2", "test3" 160 }; 161 String[] failed = { 162 }; 163 verifyTests("Passed", passed, tla.getPassedTests()); 164 verifyTests("Failed", failed, tla.getFailedTests()); 165 } 166 verifyTests(String title, String[] expected, List<ITestResult> found)167 private void verifyTests(String title, String[] expected, List<ITestResult> found) { 168 List<String> resultMethods = new ArrayList<>(); 169 for( ITestResult result : found ) { 170 resultMethods.add( result.getName() ); 171 } 172 173 Assert.assertEquals(resultMethods.size(), expected.length, "wrong number of " + title + " tests"); 174 175 for(String e : expected) { 176 Assert.assertTrue(resultMethods.contains(e), "Expected to find method " + e + " in " 177 + title + " but didn't find it."); 178 } 179 } 180 ppp(String s)181 public static void ppp(String s) { 182 //System.out.println("[CommandLineTest] " + s); 183 } 184 } 185