private void init(IClass cls, ITestMethodFinder testMethodFinder, IAnnotationFinder annotationFinder, RunInfo runInfo, XmlTest xmlTest, XmlClass xmlClass) { log(3, "Creating TestClass for " + cls); m_iClass = cls; m_testClass = cls.getRealClass(); m_xmlTest = xmlTest; m_xmlClass = xmlClass; m_runInfo = runInfo; m_testMethodFinder = testMethodFinder; m_annotationFinder = annotationFinder; initTestClassesAndInstances(); initMethods(); }
@Test public void testOneTestMethod() throws Exception { final XmlSuite suite = new XmlSuite(); final XmlTest test = new XmlTest(); final XmlClass xmlClass = new XmlClass("a.ATest", false); xmlClass.getIncludedMethods().add(new XmlInclude("test1")); test.getClasses().add(xmlClass); suite.getTests().add(test); doTest(suite,"##teamcity[enteredTheMatrix]\n" + "\n" + "##teamcity[testSuiteStarted name ='ATest' locationHint = 'java:suite://a.ATest']\n" + "\n" + "##teamcity[testStarted name='ATest.test1|[0|]' locationHint='java:test://a.ATest.test1|[0|]']\n" + "\n" + "##teamcity[testFinished name='ATest.test1|[0|]']\n"); }
@Test public void testOneTestMethodWithMultipleInvocationCount() throws Exception { final XmlSuite suite = new XmlSuite(); final XmlTest test = new XmlTest(); final XmlClass xmlClass = new XmlClass("a.ATest", false); xmlClass.getIncludedMethods().add(new XmlInclude("test1", Arrays.asList(0, 1, 2), 0)); test.getClasses().add(xmlClass); suite.getTests().add(test); doTest(suite, "##teamcity[enteredTheMatrix]\n" + "\n" + "##teamcity[testSuiteStarted name ='ATest' locationHint = 'java:suite://a.ATest']\n" + "\n" + "##teamcity[testStarted name='ATest.test1|[0|]' locationHint='java:test://a.ATest.test1|[0|]']\n" + "\n" + "##teamcity[testFinished name='ATest.test1|[0|]']\n" + "\n" + "##teamcity[testStarted name='ATest.test1|[1|] (1)' locationHint='java:test://a.ATest.test1|[1|]']\n" + "\n" + "##teamcity[testFinished name='ATest.test1|[1|] (1)']\n" + "\n" + "##teamcity[testStarted name='ATest.test1|[2|] (2)' locationHint='java:test://a.ATest.test1|[2|]']\n" + "\n" + "##teamcity[testFinished name='ATest.test1|[2|] (2)']\n"); }
private static void doTest(XmlSuite suite, String expected) { final StringBuffer buf = new StringBuffer(); final IDEATestNGRemoteListener listener = createListener(buf); for (XmlTest test : suite.getTests()) { for (XmlClass aClass : test.getClasses()) { final String classFQName = aClass.getName(); for (XmlInclude include : aClass.getIncludedMethods()) { final String methodName = include.getName(); List<Integer> numbers = include.getInvocationNumbers(); if (numbers.isEmpty()) { numbers = Collections.singletonList(0); } for (Integer integer : numbers) { final MockTestNGResult result = new MockTestNGResult(classFQName, methodName, null, new Object[] {integer}); listener.onTestStart(result); listener.onTestFinished(result); } } } } Assert.assertEquals("output: " + buf, expected, StringUtil.convertLineSeparators(buf.toString())); }
@Override public void onStart(ITestContext context) { // This will only work if each test is run individually (test suite has only one running test) // If there are multiple tests the the sandbox folder name is not provided and the GraphSandboxUtil provisions // a unique name List<XmlClass> testClassesToRun = context.getCurrentXmlTest().getClasses(); if (CollectionUtils.isNotEmpty(testClassesToRun) && 1 == testClassesToRun.size()) { XmlClass currentTestClass = testClassesToRun.get(0); if (null != currentTestClass && StringUtils.isNotEmpty(currentTestClass.getName())) { GraphSandboxUtil.create(currentTestClass.getName()); } else { GraphSandboxUtil.create(); } } else { GraphSandboxUtil.create(); } }
private XmlSuite getXmlSuite() { // we cannot use singletonLists here as testNG later // modifies lists stored in XmlSuite ... zonk XmlSuite testSuite = new XmlSuite(); testSuite.setName("tempto-tests"); testSuite.setFileName("tempto-tests"); XmlTest test = new XmlTest(testSuite); test.setName("all"); XmlPackage testPackage = new XmlPackage(options.getTestsPackage()); List<XmlPackage> testPackages = newArrayList(testPackage); test.setPackages(testPackages); XmlClass conventionBasedTestsClass = new XmlClass("io.prestodb.tempto.internal.convention.ConventionBasedTestFactory"); List<XmlClass> classes = newArrayList(conventionBasedTestsClass); test.setClasses(classes); test.setParallel(XmlSuite.ParallelMode.METHODS); return testSuite; }
public List<XmlClass> writeXmlClass(List<String> testcases, Map<String, List<Method>> methods, List<XmlClass> xmlClasses) { for (String className : methods.keySet()) { if (className.contains("Test")) { if (testcases.size() == 0) { xmlClasses.add(createClass(className)); } else { for (String s : testcases) { for (int j = 0; j < items.size(); j++) { String testName = items.get(j).concat("." + s).toString(); if (testName.equals(className)) { xmlClasses.add(createClass(className)); } } } } } } return xmlClasses; }
public static Optional<XmlConfig> getClassBrowserConfiguration(final XmlTest xmlTest, final String method) { return StreamEx.of(xmlTest.getClasses()) .filter(xmlClass -> isMethodPresent(xmlClass, method)) .map(XmlClass::getAllParameters) .map(parameters -> mapConfiguration(parameters, method)) .findFirst(); }
@Override public XmlTest object() throws TestException { XmlTest xmlTest = new XmlTest(); xmlTest.setName(test.toString()); xmlTest.setXmlClasses(Collections.singletonList(new XmlClass(test.object(), false))); return xmlTest; }
public void runTests(KWDTestCase kwdTestCase, String testSuiteName, String outputDirectory) { this.kwdTestCase = kwdTestCase; XmlSuite suite = new XmlSuite(); suite.setName(testSuiteName); XmlTest xmlTest = new XmlTest(suite); xmlTest.setName(kwdTestCase.getTestCaseName()); xmlTest.setVerbose(0); System.out.println("Total number of Tests to be run: " + kwdTestCase.getTestMethods().size()); { XmlClass xmlClass = new XmlClass("com.carteblanche.kwd.testng.TestNGDriver"); xmlTest.getClasses().add(xmlClass); List<XmlInclude> xmlIncludeMethods = xmlClass.getIncludedMethods(); XmlInclude xmlInclude = new XmlInclude("runnableTest"); xmlIncludeMethods.add(xmlInclude); xmlClass.setIncludedMethods(xmlIncludeMethods); } System.out.println("Running Tests using command .."); testNG.setXmlSuites(Arrays.asList(suite)); testNG.setPreserveOrder(true); testNG.setUseDefaultListeners(true); testNG.setOutputDirectory(outputDirectory); testNG.run(); }
protected TestClass(IClass cls, ITestMethodFinder testMethodFinder, IAnnotationFinder annotationFinder, RunInfo runInfo, XmlTest xmlTest, XmlClass xmlClass) { init(cls, testMethodFinder, annotationFinder, runInfo, xmlTest, xmlClass); }
public static File writeSuite(Map<String, Map<String, List<String>>> map, Map<String, String> testParams, String name, String rootPath, Logger logger) { File xmlFile; final XmlSuite xmlSuite = new XmlSuite(); xmlSuite.setParameters(testParams); XmlTest xmlTest = new XmlTest(xmlSuite); xmlTest.setName(name); List<XmlClass> xmlClasses = new ArrayList<XmlClass>(); int idx = 0; for (String className : map.keySet()) { final XmlClass xmlClass = new XmlClass(className, idx++, false); final Map<String, List<String>> collection = map.get(className); if (collection != null) { final ArrayList<XmlInclude> includedMethods = new ArrayList<XmlInclude>(); int mIdx = 0; for (String methodName : collection.keySet()) { final List<Integer> includes = new ArrayList<Integer>(); for (String include : collection.get(methodName)) { try { includes.add(Integer.parseInt(include)); } catch (NumberFormatException e) { logger.log(e); } } includedMethods.add(new XmlInclude(methodName, includes, mIdx++)); } xmlClass.setIncludedMethods(includedMethods); } xmlClasses.add(xmlClass); } xmlTest.setXmlClasses(xmlClasses); xmlFile = new File(rootPath, "temp-testng-customsuite.xml"); final String toXml = xmlSuite.toXml(); writeToFile(logger, xmlFile, toXml); return xmlFile; }
protected XmlTest createXmlTest(XmlSuite suite, String name, String... classes) { XmlTest result = new XmlTest(suite); int index = 0; result.setName(name); for (String c : classes) { XmlClass xc = new XmlClass(c, index++, true /* load classes */); result.getXmlClasses().add(xc); } return result; }
protected void addMethods(XmlClass cls, String... methods) { int index = 0; for (String m : methods) { XmlInclude include = new XmlInclude(m, index++); cls.getIncludedMethods().add(include); } }
/** * @author Wasiq B * @since 14-Dec-2014 8:48:22 pm * @param loadClasses * @return */ public XmlClass getXmlClass (final boolean loadClasses) { final XmlClass cls = new XmlClass (this.className, loadClasses); if (this.parameters != null) { cls.setParameters (this.parameters); } cls.setName (this.className); return cls; }
/** * Creates the suite which is represented by one XML file with name che-testng-suite.xml. It can * contain one or more tests. * * @param suitePath path to the suite file * @param suiteName the name of the suite * @param classesAndMethods classes and methods which should be included to the suite. * @return created file with suite */ public File writeSuite( String suitePath, String suiteName, Map<String, List<String>> classesAndMethods) { XmlSuite suite = new XmlSuite(); XmlTest test = new XmlTest(suite); test.setName(suiteName); List<XmlClass> xmlClasses = new ArrayList<>(); for (String className : classesAndMethods.keySet()) { XmlClass xmlClass = new XmlClass(className, false); xmlClasses.add(xmlClass); List<String> methods = classesAndMethods.get(className); if (methods != null) { List<XmlInclude> includedMethods = new ArrayList<>(); for (String method : methods) { includedMethods.add(new XmlInclude(method)); } xmlClass.setIncludedMethods(includedMethods); } } test.setXmlClasses(xmlClasses); File result = new File(suitePath, "che-testng-suite.xml"); try { com.google.common.io.Files.write(suite.toXml().getBytes("UTF-8"), result); } catch (IOException e) { LOG.error("Can't write TestNG suite xml file.", e); } return result; }
private void addClassToXmlTest(String pkgAndClsName,String testName) { XmlTest xmltest=new XmlTest(); XmlClass classe=new XmlClass(pkgAndClsName); xmltest.setName(testName); xmltest.setClasses(Arrays.asList(classe)); addTest(xmltest); }
public XmlSuite constructXmlSuiteForParallel(String pack, List<String> testcases, Map<String, List<Method>> methods, int deviceCount, ArrayList<String> deviceSerail) { ArrayList<String> listeners = new ArrayList<>(); listeners.add("com.appium.manager.AppiumParallelTestListener"); listeners.add("com.appium.utils.RetryListener"); include(listeners, "LISTENERS"); include(groupsInclude, "INCLUDE_GROUPS"); include(groupsExclude, "EXCLUDE_GROUPS"); XmlSuite suite = new XmlSuite(); suite.setName("TestNG Forum"); suite.setThreadCount(deviceCount); suite.setDataProviderThreadCount(deviceCount); suite.setParallel(ParallelMode.TESTS); suite.setVerbose(2); suite.setListeners(listeners); for (int i = 0; i < deviceCount; i++) { XmlTest test = new XmlTest(suite); test.setName("TestNG Test" + i); test.setPreserveOrder("false"); test.addParameter("device", deviceSerail.get(i)); test.setIncludedGroups(groupsInclude); test.setExcludedGroups(groupsExclude); List<XmlClass> xmlClasses = new ArrayList<>(); writeXmlClass(testcases, methods, xmlClasses); test.setXmlClasses(xmlClasses); } System.out.println(suite.toXml()); writeTestNGFile(suite); return suite; }
public XmlSuite constructXmlSuiteForDistribution(String pack, List<String> tests, Map<String, List<Method>> methods, int deviceCount) { include(listeners, "LISTENERS"); include(groupsInclude, "INCLUDE_GROUPS"); XmlSuite suite = new XmlSuite(); suite.setName("TestNG Forum"); suite.setThreadCount(deviceCount); suite.setParallel(ParallelMode.CLASSES); suite.setVerbose(2); listeners.add("com.appium.manager.AppiumParallelMethodTestListener"); listeners.add("com.appium.utils.RetryListener"); suite.setListeners(listeners); if (prop.getProperty("LISTENERS") != null) { suite.setListeners(listeners); } XmlTest test = new XmlTest(suite); test.setName("TestNG Test"); test.addParameter("device", ""); include(groupsExclude, "EXCLUDE_GROUPS"); test.setIncludedGroups(groupsInclude); test.setExcludedGroups(groupsExclude); List<XmlClass> xmlClasses = new ArrayList<>(); writeXmlClass(tests, methods, xmlClasses); test.setXmlClasses(xmlClasses); System.out.println(suite.toXml()); writeTestNGFile(suite); return suite; }
public XmlSuite constructXmlSuiteForDistributionMethods(String pack, List<String> tests, Map<String, List<Method>> methods, int deviceCount) { include(listeners, "LISTENERS"); include(groupsInclude, "INCLUDE_GROUPS"); XmlSuite suite = new XmlSuite(); suite.setName("TestNG Forum"); suite.setThreadCount(deviceCount); suite.setDataProviderThreadCount(deviceCount); suite.setVerbose(2); suite.setParallel(ParallelMode.METHODS); listeners.add("com.appium.manager.AppiumParallelMethodTestListener"); listeners.add("com.appium.utils.RetryListener"); suite.setListeners(listeners); if (prop.getProperty("LISTENERS") != null) { suite.setListeners(listeners); } List<XmlClass> xmlClasses = new ArrayList<>(); xmlClasses = writeXmlClass(tests, methods, xmlClasses); XmlTest test = new XmlTest(suite); test.setName("TestNG Test"); test.addParameter("device", ""); include(groupsExclude, "EXCLUDE_GROUPS"); test.setIncludedGroups(groupsInclude); test.setExcludedGroups(groupsExclude); List<XmlClass> writeXml = new ArrayList<>(); for (int i = 0; i < xmlClasses.size(); i++) { writeXml.add(new XmlClass(xmlClasses.get(i).getName())); test.setClasses(writeXml); } System.out.println(suite.toXml()); writeTestNGFile(suite); return suite; }
private XmlSuite createSuite() { final XmlSuite suite = new XmlSuite(); suite.setName(this.clazz.getName()); suite.setSkipFailedInvocationCounts(true); final XmlTest test = new XmlTest(suite); test.setName(this.clazz.getName()); final XmlClass xclass = new XmlClass(this.clazz.getName()); test.setXmlClasses(Collections.singletonList(xclass)); if (!this.includedTestMethods.isEmpty()) { List<XmlInclude> xmlIncludedTestMethods = new ArrayList<>(); for (String includedTestMethod : includedTestMethods) { XmlInclude includedMethod = new XmlInclude(includedTestMethod); xmlIncludedTestMethods.add(includedMethod); } xclass.setIncludedMethods(xmlIncludedTestMethods); } if (!this.config.getExcludedGroups().isEmpty()) { suite.setExcludedGroups(this.config.getExcludedGroups()); } if (!this.config.getIncludedGroups().isEmpty()) { suite.setIncludedGroups(this.config.getIncludedGroups()); } return suite; }
private static XmlSuite createSuite(String testName) { XmlSuite suite = new XmlSuite(); suite.setName(SUITE_PREFIX + testName); XmlTest test = new XmlTest(suite); test.setName(testName); List<XmlClass> classes = new ArrayList<>(); classes.add(new XmlClass(ConfigMethodsTest.class)); test.setXmlClasses(classes); return suite; }
public static boolean isMethodPresent(final XmlClass xmlClass, final String method) { return StreamEx.of(xmlClass.getIncludedMethods()) .anyMatch(xmlInclude -> xmlInclude.getName().equals(method)); }
@Override public XmlClass getXmlClass() { return m_xmlClass; }
public void run() { try { initializeSuitesAndJarFile(); List<XmlSuite> suites = Lists.newArrayList(); calculateAllSuites(m_suites, suites); if(suites.size() > 0) { for (XmlSuite suite : suites) { final List<XmlTest> tests = suite.getTests(); for (XmlTest test : tests) { try { if (myParam != null) { for (XmlClass aClass : test.getXmlClasses()) { for (XmlInclude include : aClass.getIncludedMethods()) { final XmlInclude xmlInclude = new XmlInclude(include.getName(), Collections.singletonList(Integer.parseInt(myParam)), 0); aClass.setIncludedMethods(Collections.singletonList(xmlInclude)); } } } } catch (NumberFormatException e) { System.err.println("Invocation number: expected integer but found: " + myParam); } } } final Object listener = createListener(); addListener((ISuiteListener)listener); addListener((ITestListener)listener); super.run(); System.exit(0); } else { System.out.println("##teamcity[enteredTheMatrix]"); System.err.println("Nothing found to run"); } } catch(Throwable cause) { cause.printStackTrace(System.err); } }
private XmlClass createClass(String className) { XmlClass clazz = new XmlClass(); clazz.setName(className); //clazz.setIncludedMethods(constructIncludes(methods)); return clazz; }
private void checkHealth(ISuite suite, String className, String[] methods) { if (className.isEmpty()) { return; } // create runtime XML suite for health check XmlSuite xmlSuite = new XmlSuite(); xmlSuite.setName("HealthCheck XmlSuite - " + className); XmlTest xmltest = new XmlTest(xmlSuite); xmltest.setName("HealthCheck TestCase"); XmlClass healthCheckClass = new XmlClass(); healthCheckClass.setName(className); // TestNG do not execute missed methods so we have to calulate expected methods count to handle potential mistakes in methods naming int expectedMethodsCount = -1; if (methods != null) { // declare particular methods if they are provided List<XmlInclude> methodsToRun = constructIncludes(methods); expectedMethodsCount = methodsToRun.size(); healthCheckClass.setIncludedMethods(methodsToRun); } xmltest.setXmlClasses(Arrays.asList(new XmlClass[] { healthCheckClass })); xmlSuite.setTests(Arrays.asList(new XmlTest[] { xmltest })); LOGGER.info("HealthCheck '" + className + "' is started."); LOGGER.debug("HealthCheck suite content:" + xmlSuite.toXml()); // Second TestNG process to run HealthCheck TestNG testng = new TestNG(); testng.setXmlSuites(Arrays.asList(xmlSuite)); TestListenerAdapter tla = new TestListenerAdapter(); testng.addListener(tla); testng.run(); synchronized (this) { boolean passed = false; if (expectedMethodsCount == -1) { if (tla.getPassedTests().size() > 0 && tla.getFailedTests().size() == 0 && tla.getSkippedTests().size() == 0) { passed = true; } } else { LOGGER.info("Expected passed tests count: " + expectedMethodsCount); if (tla.getPassedTests().size() == expectedMethodsCount && tla.getFailedTests().size() == 0 && tla.getSkippedTests().size() == 0) { passed = true; } } if (passed) { LOGGER.info("HealthCheck suite '" + className + "' is finished successfully."); } else { throw new SkipException("Skip test(s) due to health check failures for '" + className + "'"); } } }