1 package test; 2 3 import static org.testng.Assert.assertEquals; 4 import static org.testng.Assert.assertTrue; 5 6 import org.testng.Assert; 7 import org.testng.ITestContext; 8 import org.testng.ITestResult; 9 import org.testng.TestListenerAdapter; 10 import org.testng.TestNG; 11 import org.testng.annotations.Test; 12 13 import test.sample.JUnitSample1; 14 import testhelper.OutputDirectoryPatch; 15 16 import java.util.List; 17 18 public class CommandLineTest { 19 20 /** 21 * Test -junit 22 */ 23 @Test(groups = { "current" } ) junitParsing()24 public void junitParsing() { 25 String[] argv = { 26 "-log", "0", 27 "-d", OutputDirectoryPatch.getOutputDirectory(), 28 "-junit", 29 "-testclass", "test.sample.JUnitSample1" 30 }; 31 TestListenerAdapter tla = new TestListenerAdapter(); 32 TestNG.privateMain(argv, tla); 33 34 List<ITestResult> passed = tla.getPassedTests(); 35 assertEquals(passed.size(), 2); 36 String test1 = passed.get(0).getMethod().getMethodName(); 37 String test2 = passed.get(1).getMethod().getMethodName(); 38 39 assertTrue(JUnitSample1.EXPECTED1.equals(test1) && JUnitSample1.EXPECTED2.equals(test2) || 40 JUnitSample1.EXPECTED1.equals(test2) && JUnitSample1.EXPECTED2.equals(test1)); 41 } 42 43 /** 44 * Test the absence of -junit 45 */ 46 @Test(groups = { "current" } ) junitParsing2()47 public void junitParsing2() { 48 String[] argv = { 49 "-log", "0", 50 "-d", OutputDirectoryPatch.getOutputDirectory(), 51 "-testclass", "test.sample.JUnitSample1" 52 }; 53 TestListenerAdapter tla = new TestListenerAdapter(); 54 TestNG.privateMain(argv, tla); 55 56 List<ITestResult> passed = tla.getPassedTests(); 57 assertEquals(passed.size(), 0); 58 } 59 60 /** 61 * Test the ability to override the default command line Suite name 62 */ 63 @Test(groups = { "current" } ) suiteNameOverride()64 public void suiteNameOverride() { 65 String suiteName="MySuiteName"; 66 String[] argv = { 67 "-log", "0", 68 "-d", OutputDirectoryPatch.getOutputDirectory(), 69 "-junit", 70 "-testclass", "test.sample.JUnitSample1", 71 "-suitename", "\""+suiteName+"\"" 72 }; 73 TestListenerAdapter tla = new TestListenerAdapter(); 74 TestNG.privateMain(argv, tla); 75 76 List<ITestContext> contexts = tla.getTestContexts(); 77 assertTrue(contexts.size()>0); 78 for (ITestContext context:contexts) { 79 assertEquals(context.getSuite().getName(),suiteName); 80 } 81 } 82 83 /** 84 * Test the ability to override the default command line test name 85 */ 86 @Test(groups = { "current" } ) testNameOverride()87 public void testNameOverride() { 88 String testName="My Test Name"; 89 String[] argv = { 90 "-log", "0", 91 "-d", OutputDirectoryPatch.getOutputDirectory(), 92 "-junit", 93 "-testclass", "test.sample.JUnitSample1", 94 "-testname", "\""+testName+"\"" 95 }; 96 TestListenerAdapter tla = new TestListenerAdapter(); 97 TestNG.privateMain(argv, tla); 98 99 List<ITestContext> contexts = tla.getTestContexts(); 100 assertTrue(contexts.size()>0); 101 for (ITestContext context:contexts) { 102 assertEquals(context.getName(),testName); 103 } 104 } 105 106 @Test testUseDefaultListenersArgument()107 public void testUseDefaultListenersArgument() { 108 TestNG.privateMain(new String[] { 109 "-log", "0", "-usedefaultlisteners", "false", "-testclass", "test.sample.JUnitSample1" 110 }, null); 111 } 112 113 @Test testMethodParameter()114 public void testMethodParameter() { 115 String[] argv = { 116 "-log", "0", 117 "-d", OutputDirectoryPatch.getOutputDirectory(), 118 "-methods", "test.sample.Sample2.method1,test.sample.Sample2.method3", 119 }; 120 TestListenerAdapter tla = new TestListenerAdapter(); 121 TestNG.privateMain(argv, tla); 122 123 List<ITestResult> passed = tla.getPassedTests(); 124 Assert.assertEquals(passed.size(), 2); 125 Assert.assertTrue((passed.get(0).getName().equals("method1") && 126 passed.get(1).getName().equals("method3")) 127 || 128 (passed.get(1).getName().equals("method1") && 129 passed.get(0).getName().equals("method3"))); 130 } 131 ppp(String s)132 private static void ppp(String s) { 133 System.out.println("[CommandLineTest] " + s); 134 } 135 136 } 137