• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package test;
2 
3 import org.testng.TestNG;
4 import org.testng.collections.Lists;
5 import org.testng.xml.XmlClass;
6 import org.testng.xml.XmlSuite;
7 import org.testng.xml.XmlTest;
8 
9 import java.io.File;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Random;
13 
14 public class TestHelper {
15 
createSuite(String cls, String suiteName)16   public static XmlSuite createSuite(String cls, String suiteName) {
17     return createSuite(cls, suiteName, "TmpTest");
18   }
19 
createSuite(Class<?> cls, String suiteName, String testName)20   public static XmlSuite createSuite(Class<?> cls, String suiteName, String testName) {
21     return createSuite(cls.getName(), suiteName, testName);
22   }
23 
createSuite(String cls, String suiteName, String testName)24   private static XmlSuite createSuite(String cls, String suiteName, String testName) {
25     XmlSuite result = new XmlSuite();
26     result.setName(suiteName);
27 
28     XmlTest test = new XmlTest(result);
29     test.setName(testName);
30     List<XmlClass> classes = new ArrayList<>();
31     classes.add(new XmlClass(cls));
32     test.setXmlClasses(classes);
33 
34     return result;
35   }
36 
createTestNG(String outputDir)37   public static TestNG createTestNG(String outputDir) {
38     return createTestNG(null, outputDir);
39   }
40 
createTestNG()41   public static TestNG createTestNG() {
42     return createTestNG(null, null);
43   }
44 
createTestNG(XmlSuite suite)45   public static TestNG createTestNG(XmlSuite suite) {
46     return createTestNG(suite, System.getProperty("java.io.tmpdir"));
47   }
48 
createTestNG(XmlSuite suite, String outputDir)49   public static TestNG createTestNG(XmlSuite suite, String outputDir) {
50     TestNG result = new TestNG();
51     if (suite != null) {
52       List<XmlSuite> suites = Lists.newArrayList();
53       suites.add(suite);
54       result.setXmlSuites(suites);
55     }
56     if (outputDir == null) {
57       outputDir = createRandomDirectory().getAbsolutePath();
58     }
59     result.setOutputDirectory(outputDir);
60     result.setVerbose(-1);
61 
62     return result;
63   }
64 
createRandomDirectory()65   public static File createRandomDirectory() {
66     String dir = System.getProperty("java.io.tmpdir");
67     Random r = new Random(System.currentTimeMillis());
68     String name = "testng-tmp-" + r.nextInt();
69     File result = new File(dir + File.separatorChar + name);
70     result.deleteOnExit();
71     result.mkdirs();
72 
73     return result;
74   }
75 
ppp(String string)76   private static void ppp(String string) {
77     System.out.println("[TestHelper] " + string);
78   }
79 
80 }
81