@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())); }
public static Optional<XmlConfig> getMethodBrowserConfiguration(final XmlTest xmlTest, final String method) { return StreamEx.of(xmlTest.getClasses()) .flatMap(xmlClass -> StreamEx.of(xmlClass.getIncludedMethods())) .filter(xmlInclude -> xmlInclude.getName().equals(method)) .map(XmlInclude::getAllParameters) .map(parameters -> mapConfiguration(parameters, method)) .findFirst(); }
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(); }
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 void addMethods(XmlClass cls, String... methods) { int index = 0; for (String m : methods) { XmlInclude include = new XmlInclude(m, index++); cls.getIncludedMethods().add(include); } }
/** * 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 List<XmlInclude> constructIncludes(List<Method> methods) { List<XmlInclude> includes = new ArrayList<>(); for (Method m : methods) { includes.add(new XmlInclude(m.getName())); } return includes; }
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 List<XmlInclude> constructIncludes(String... methodNames) { List<XmlInclude> includes = new ArrayList<XmlInclude>(); for (String eachMethod : methodNames) { includes.add(new XmlInclude(eachMethod)); } return includes; }
private void privateRunJUnit(XmlTest xmlTest) { final ClassInfoMap cim = new ClassInfoMap(m_testClassesFromXml, false); final Set<Class<?>> classes = cim.getClasses(); final List<ITestNGMethod> runMethods = Lists.newArrayList(); List<IWorker<ITestNGMethod>> workers = Lists.newArrayList(); // FIXME: directly referencing JUnitTestRunner which uses JUnit classes // may result in an class resolution exception under different JVMs // The resolution process is not specified in the JVM spec with a specific implementation, // so it can be eager => failure workers.add(new IWorker<ITestNGMethod>() { /** * @see TestMethodWorker#getTimeOut() */ @Override public long getTimeOut() { return 0; } /** * @see java.lang.Runnable#run() */ @Override public void run() { for(Class<?> tc: classes) { List<XmlInclude> includedMethods = cim.getXmlClass(tc).getIncludedMethods(); List<String> methods = Lists.newArrayList(); for (XmlInclude inc: includedMethods) { methods.add(inc.getName()); } IJUnitTestRunner tr= ClassHelper.createTestRunner(TestRunner.this); tr.setInvokedMethodListeners(m_invokedMethodListeners); try { tr.run(tc, methods.toArray(new String[methods.size()])); } catch(Exception ex) { ex.printStackTrace(); } finally { runMethods.addAll(tr.getTestMethods()); } } } @Override public List<ITestNGMethod> getTasks() { throw new TestNGException("JUnit not supported"); } @Override public int getPriority() { if (m_allTestMethods.length == 1) { return m_allTestMethods[0].getPriority(); } else { return 0; } } @Override public int compareTo(IWorker<ITestNGMethod> other) { return getPriority() - other.getPriority(); } }); runJUnitWorkers(workers); m_allTestMethods= runMethods.toArray(new ITestNGMethod[runMethods.size()]); }
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 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 + "'"); } } }