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