• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package test;
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.collections.Lists;
8 import org.testng.internal.Utils;
9 import org.testng.xml.XmlClass;
10 import org.testng.xml.XmlInclude;
11 import org.testng.xml.XmlSuite;
12 import org.testng.xml.XmlTest;
13 
14 import java.io.File;
15 import java.util.Arrays;
16 import java.util.Iterator;
17 import java.util.List;
18 
19 public class SimpleBaseTest {
20   // System property specifying where the resources (e.g. xml files) can be found
21   private static final String TEST_RESOURCES_DIR = "test.resources.dir";
22 
create()23   public static TestNG create() {
24     TestNG result = new TestNG();
25     result.setUseDefaultListeners(false);
26     result.setVerbose(0);
27     return result;
28   }
29 
create(Class<?>.... testClasses)30   public static TestNG create(Class<?>... testClasses) {
31     TestNG result = create();
32     result.setTestClasses(testClasses);
33     return result;
34   }
35 
create(XmlSuite... suites)36   protected TestNG create(XmlSuite... suites) {
37     TestNG result = create();
38     result.setXmlSuites(Arrays.asList(suites));
39     return result;
40   }
41 
createTests(String suiteName, Class<?>... testClasses)42   protected TestNG createTests(String suiteName, Class<?>... testClasses) {
43     XmlSuite suite = createXmlSuite(suiteName);
44     int i=0;
45     for (Class<?> testClass : testClasses) {
46       createXmlTest(suite, testClass.getName() + i, testClass);
47       i++;
48     }
49     return create(suite);
50   }
51 
createXmlSuite(String name)52   protected XmlSuite createXmlSuite(String name) {
53     XmlSuite result = new XmlSuite();
54     result.setName(name);
55     return result;
56   }
57 
createXmlTest(XmlSuite suite, String name, Class clazz, Class... classes)58   protected XmlTest createXmlTest(XmlSuite suite, String name, Class clazz, Class... classes) {
59     XmlTest result = new XmlTest(suite);
60     int index = 0;
61     result.setName(name);
62     XmlClass xc = new XmlClass(clazz.getName(), index++, true /* load classes */);
63     result.getXmlClasses().add(xc);
64     for (Class c : classes) {
65       xc = new XmlClass(c.getName(), index++, true /* load classes */);
66       result.getXmlClasses().add(xc);
67     }
68 
69     return result;
70   }
71 
createXmlTest(XmlSuite suite, String name, String... classes)72   protected XmlTest createXmlTest(XmlSuite suite, String name, String... classes) {
73     XmlTest result = new XmlTest(suite);
74     int index = 0;
75     result.setName(name);
76     for (String c : classes) {
77       XmlClass xc = new XmlClass(c, index++, true /* load classes */);
78       result.getXmlClasses().add(xc);
79     }
80 
81     return result;
82   }
83 
addMethods(XmlClass cls, String... methods)84   protected void addMethods(XmlClass cls, String... methods) {
85     int index = 0;
86     for (String m : methods) {
87       XmlInclude include = new XmlInclude(m, index++);
88       cls.getIncludedMethods().add(include);
89     }
90   }
91 
getPathToResource(String fileName)92   public static String getPathToResource(String fileName) {
93     String result = System.getProperty(TEST_RESOURCES_DIR);
94     if (result == null) {
95       throw new IllegalArgumentException("System property " + TEST_RESOURCES_DIR + " was not defined.");
96     }
97     return result + File.separatorChar + fileName;
98   }
99 
verifyPassedTests(TestListenerAdapter tla, String... methodNames)100   protected void verifyPassedTests(TestListenerAdapter tla, String... methodNames) {
101     Iterator<ITestResult> it = tla.getPassedTests().iterator();
102     Assert.assertEquals(tla.getPassedTests().size(), methodNames.length);
103 
104     int i = 0;
105     while (it.hasNext()) {
106       Assert.assertEquals(it.next().getName(), methodNames[i++]);
107     }
108   }
109 
110   /**
111    * Compare a list of ITestResult with a list of String method names,
112    */
assertTestResultsEqual(List<ITestResult> results, List<String> methods)113   public static void assertTestResultsEqual(List<ITestResult> results, List<String> methods) {
114     List<String> resultMethods = Lists.newArrayList();
115     for (ITestResult r : results) {
116       resultMethods.add(r.getMethod().getMethodName());
117     }
118     Assert.assertEquals(resultMethods, methods);
119   }
120 
121 }
122