protected void generateFailureSuite(XmlSuite xmlSuite, ISuite suite, String outputDir) { XmlSuite failedSuite = (XmlSuite) xmlSuite.clone(); failedSuite.setName("Failed suite [" + xmlSuite.getName() + "]"); m_xmlSuite = failedSuite; Map<String, XmlTest> xmlTests = Maps.newHashMap(); for (XmlTest xmlT : xmlSuite.getTests()) { xmlTests.put(xmlT.getName(), xmlT); } Map<String, ISuiteResult> results = suite.getResults(); for (Map.Entry<String, ISuiteResult> entry : results.entrySet()) { ISuiteResult suiteResult = entry.getValue(); ITestContext testContext = suiteResult.getTestContext(); generateXmlTest( suite, xmlTests.get(testContext.getName()), testContext, testContext.getFailedTests().getAllResults(), testContext.getSkippedTests().getAllResults()); } if (null != failedSuite.getTests() && failedSuite.getTests().size() > 0) { Utils.writeUtf8File(outputDir, TESTNG_FAILED_XML, failedSuite.toXml()); Utils.writeUtf8File(suite.getOutputDirectory(), TESTNG_FAILED_XML, failedSuite.toXml()); } }
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())); }
@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[testCount count='1']\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" + "##teamcity[testCount count='1']\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" + "##teamcity[testCount count='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"); }
public TCKRunner() { setName("JSR363-TCK 0.1"); XmlTest test = new XmlTest(this); test.setName("TCK/Test Setup"); List<XmlClass> classes = new ArrayList<>(); classes.add(new XmlClass(TCKSetup.class)); test.setXmlClasses(classes); }
private XmlSuite createXmlSuite() { XmlSuite result = new XmlSuite(); result.setName(getDefaultSuiteName()); XmlTest test = new XmlTest(result); test.setName(getDefaultTestName()); updateXmlSuite(result); return result; }
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; }
private void updateXmlSuite(XmlSuite suite) { p("Updating XML suite"); XmlTest test = suite.getTests().get(0); test.getXmlClasses().clear(); test.getXmlPackages().clear(); if (m_selectionCombo.getSelectionIndex() == 0) { test.getXmlClasses().addAll(m_classes); } else { test.getXmlPackages().addAll(m_packages); } p("Done updating XML suite"); }
// Send the name of the suite and the name of the test as parameters so that the methods can // associate their // execution event logs with them. Specified the delay in seconds to apply for the method // execution. This delay // helps in determining the parallelism or lack thereof for method executions. public static void addParams(XmlSuite suite, String suiteName, String testName, String sleepFor) { Map<String, String> parameters = new HashMap<>(); parameters.put("suiteName", suiteName); parameters.put("testName", testName); parameters.put("sleepFor", sleepFor); for (XmlTest test : suite.getTests()) { if (test.getName().equals(testName)) { test.setParameters(parameters); } } }
private XmlSuite createSuite(String name, Class[] classes) { XmlSuite result = new XmlSuite(); result.setName(name); for (Class c : classes) { XmlTest test1 = new XmlTest(result); test1.setName(c.getName()); XmlClass class1 = new XmlClass(c); test1.getXmlClasses().add(class1); } return result; }
/** * Call testng to run test cases. * * @param testngFile testng running configuration file, such as "testng.xml", * "config1.xml,config2.xml" * @throws Exception */ private static void handleTestNGRun(String testngFile) throws Exception { TestNG testng = new TestNG(); List<XmlSuite> allXmlSuites = new ArrayList<XmlSuite>(); for (String suiteFile : testngFile.split(",")) { Collection<XmlSuite> allSuites = new Parser(suiteFile).parse(); // suiteFile itself has "/testng.xml" for (XmlSuite s : allSuites) { allXmlSuites.add(s); for (XmlTest test : s.getTests()) { // <test> tag if (test.getParameter("testngFile") != null) // if there is a "testngFile" parameter under <test> tag, parse it to a allXmlSuites.addAll( new Parser(test.getParameter("testngFile")) .parse()); // testng configuration file and run it. } } } int globalRunCount = 1; // default value if (globalMap.get("globalRunCount") != null && globalMap.get("globalRunCount").trim().length() != 0) globalRunCount = Integer.valueOf(globalMap.get("globalRunCount")); if (globalRunCount == 0) allXmlSuites.clear(); else if (globalRunCount == 1) ; else { List<XmlSuite> allXmlSuitesOriginal = new ArrayList<XmlSuite>(allXmlSuites); for (int i = 2; i <= globalRunCount; i++) { // notice: if use testng default html report only can recognize one suite if more // suites have the same names allXmlSuites.addAll( allXmlSuitesOriginal); // though changed the suite name. html report still not ok. but // testng-results.xml has the correct values. } } testng.setXmlSuites(allXmlSuites); testng.setOutputDirectory(reportRootdir); testng.setUseDefaultListeners(true); // add customized TestNG listeners testMethodStatusListener = new TestMethodStatusListener(); // enable obtained by forward operation testng.addListener(testMethodStatusListener); testReportListener = new TestReportListener(testMethodStatusListener); testng.addListener(testReportListener); testng.run(); }
/** * Returns this test as a TestNG XmlTest * * @param suite * @param loadClasses whether to load classes when creating the XmlTest * @return this test as TestNG XmlTest */ public XmlTest getTestAsXmlTest(XmlSuite suite, boolean loadClasses) { XmlTest xmltest = new XmlTest(suite); xmltest.setName(this.id + "." + this.name); // set name like // "<id>.<name>" // add parameters to this test case Properties params = this.getParametersAsProperties(); for (Enumeration<?> e = params.keys(); e.hasMoreElements(); ) { String key = (String) e.nextElement(); xmltest.addParameter(key, params.getProperty(key)); } // add test classes xmltest.setXmlClasses(this.getXmlClasses(loadClasses)); return xmltest; }
/** @return The parameters defined in this test tag and the tags above it. */ public Map<String, String> getAllParameters() { Map<String, String> result = Maps.newHashMap(); Map<String, String> parameters = m_xmlTest.getLocalParameters(); result.putAll(parameters); result.putAll(m_parameters); return result; }
@Override public Object[] getInstances(boolean create) { Object[] result = {}; if (m_xmlTest.isJUnit()) { if (create) { result = new Object[] { ClassHelper.createInstance( m_class, m_classes, m_xmlTest, m_annotationFinder, m_objectFactory) }; } } else { result = new Object[] {getDefaultInstance()}; } if (m_instances.size() > 0) { result = m_instances.toArray(new Object[m_instances.size()]); } m_instanceCount = m_instances.size(); m_instanceHashCodes = new long[m_instanceCount]; for (int i = 0; i < m_instanceCount; i++) { m_instanceHashCodes[i] = m_instances.get(i).hashCode(); } return result; }
/** Constructor. */ public TCKRunner() { setName("JSR354-TCK, version 1.1"); XmlTest test = new XmlTest(this); test.setName("TCK/Test Setup"); List<XmlClass> classes = new ArrayList<>(); classes.add(new XmlClass(TCKTestSetup.class)); classes.add(new XmlClass(ModellingCurrenciesTest.class)); classes.add(new XmlClass(ModellingMonetaryAmountsTest.class)); classes.add(new XmlClass(CreatingMonetaryAmountsTest.class)); classes.add(new XmlClass(ExternalizingNumericValueTest.class)); classes.add(new XmlClass(FunctionalExtensionPointsTest.class)); classes.add(new XmlClass(AccessingCurrenciesAmountsRoundingsTest.class)); classes.add(new XmlClass(MonetaryConversionsTest.class)); classes.add(new XmlClass(ExchangeRatesAndRateProvidersTest.class)); classes.add(new XmlClass(ConvertingAmountsTest.class)); classes.add(new XmlClass(ProviderChainsTest.class)); classes.add(new XmlClass(FormattingMonetaryAmountsTest.class)); test.setXmlClasses(classes); }
/** * Clone the <TT>source</TT> <CODE>XmlTest</CODE> by including: - test attributes - groups * definitions - parameters * * <p>The <classes> sub element is ignored for the moment. * * @return a clone of the current XmlTest */ @Override public Object clone() { XmlTest result = new XmlTest(getSuite()); result.setName(getName()); result.setIncludedGroups(getIncludedGroups()); result.setExcludedGroups(getExcludedGroups()); result.setJUnit(isJUnit()); result.setParallel(getParallel()); result.setVerbose(getVerbose()); result.setParameters(getParameters()); result.setXmlPackages(getXmlPackages()); Map<String, List<String>> metagroups = getMetaGroups(); for (Map.Entry<String, List<String>> group : metagroups.entrySet()) { result.addMetaGroup(group.getKey(), group.getValue()); } return result; }
@Test public void verifyTestContextInjection(ITestContext tc, XmlTest xmlTest) { TestNG tng = create(); tng.setTestClasses(new Class[] {Sample.class}); TestListenerAdapter tla = new TestListenerAdapter(); tng.addListener(tla); tng.run(); Assert.assertEquals(xmlTest.getName(), "Injection"); Assert.assertEquals(tla.getPassedTests().size(), 1); Assert.assertEquals(tla.getPassedTests().get(0).getMethod().getMethodName(), "f"); }
public static XmlSuite parse(String filePath, InputStream is) throws FileNotFoundException { Constructor constructor = new TestNGConstructor(XmlSuite.class); { TypeDescription suiteDescription = new TypeDescription(XmlSuite.class); suiteDescription.putListPropertyType("packages", XmlPackage.class); suiteDescription.putListPropertyType("listeners", String.class); suiteDescription.putListPropertyType("tests", XmlTest.class); suiteDescription.putListPropertyType("method-selectors", XmlMethodSelector.class); constructor.addTypeDescription(suiteDescription); } { TypeDescription testDescription = new TypeDescription(XmlTest.class); testDescription.putListPropertyType("classes", XmlClass.class); testDescription.putMapPropertyType("metaGroups", String.class, List.class); testDescription.putListPropertyType("method-selectors", XmlMethodSelector.class); constructor.addTypeDescription(testDescription); } org.yaml.snakeyaml.Yaml y = new org.yaml.snakeyaml.Yaml(constructor); if (is == null) is = new FileInputStream(new File(filePath)); XmlSuite result = (XmlSuite) y.load(is); result.setFileName(filePath); // DEBUG // System.out.println("[Yaml] " + result.toXml()); // Adjust XmlTest parents and indices for (XmlTest t : result.getTests()) { t.setSuite(result); int index = 0; for (XmlClass c : t.getClasses()) { c.setIndex(index++); } } return result; }
/** * @param methods The methods we want to represent * @param srcXmlTest * @return A list of XmlClass objects (each representing a <class> tag) based on the parameter * methods */ private List<XmlClass> createXmlClasses(List<ITestNGMethod> methods, XmlTest srcXmlTest) { List<XmlClass> result = Lists.newArrayList(); Map<Class, Set<ITestNGMethod>> methodsMap = Maps.newHashMap(); for (ITestNGMethod m : methods) { Object[] instances = m.getInstances(); Class clazz = instances == null || instances.length == 0 || instances[0] == null ? m.getRealClass() : instances[0].getClass(); Set<ITestNGMethod> methodList = methodsMap.get(clazz); if (null == methodList) { methodList = new HashSet<ITestNGMethod>(); methodsMap.put(clazz, methodList); } methodList.add(m); } // Ideally, we should preserve each parameter in each class but putting them // all in the same bag for now Map<String, String> parameters = Maps.newHashMap(); for (XmlClass c : srcXmlTest.getClasses()) { parameters.putAll(c.getLocalParameters()); } int index = 0; for (Map.Entry<Class, Set<ITestNGMethod>> entry : methodsMap.entrySet()) { Class clazz = entry.getKey(); Set<ITestNGMethod> methodList = entry.getValue(); // @author Borojevic // Need to check all the methods, not just @Test ones. XmlClass xmlClass = new XmlClass(clazz.getName(), index++, false /* don't load classes */); List<XmlInclude> methodNames = Lists.newArrayList(methodList.size()); int ind = 0; for (ITestNGMethod m : methodList) { methodNames.add( new XmlInclude(m.getMethod().getName(), m.getFailedInvocationNumbers(), ind++)); } xmlClass.setIncludedMethods(methodNames); xmlClass.setParameters(parameters); result.add(xmlClass); } return result; }
/** Generate testng-failed.xml */ private void createXmlTest( ITestContext context, List<ITestNGMethod> methods, XmlTest srcXmlTest) { XmlTest xmlTest = new XmlTest(m_xmlSuite); xmlTest.setName(context.getName() + "(failed)"); xmlTest.setBeanShellExpression(srcXmlTest.getExpression()); xmlTest.setIncludedGroups(srcXmlTest.getIncludedGroups()); xmlTest.setExcludedGroups(srcXmlTest.getExcludedGroups()); xmlTest.setParallel(srcXmlTest.getParallel()); xmlTest.setParameters(srcXmlTest.getLocalParameters()); xmlTest.setJUnit(srcXmlTest.isJUnit()); List<XmlClass> xmlClasses = createXmlClasses(methods, srcXmlTest); xmlTest.setXmlClasses(xmlClasses); }
private static void toYaml(StringBuilder result, String sp, XmlTest t) { String sp2 = sp + " "; result.append(sp).append("- name: ").append(t.getName()).append("\n"); maybeAdd(result, sp2, "junit", t.isJUnit(), XmlSuite.DEFAULT_JUNIT); maybeAdd(result, sp2, "verbose", t.getVerbose(), XmlSuite.DEFAULT_VERBOSE); maybeAdd(result, sp2, "timeOut", t.getTimeOut(), null); maybeAdd(result, sp2, "parallel", t.getParallel(), XmlSuite.DEFAULT_PARALLEL); maybeAdd( result, sp2, "skipFailedInvocationCounts", t.skipFailedInvocationCounts(), XmlSuite.DEFAULT_SKIP_FAILED_INVOCATION_COUNTS); maybeAdd(result, "preserveOrder", sp2, t.getPreserveOrder(), XmlSuite.DEFAULT_PRESERVE_ORDER); toYaml(result, "parameters", sp2, t.getTestParameters()); if (t.getIncludedGroups().size() > 0) { result .append(sp2) .append("includedGroups: [ ") .append(Utils.join(t.getIncludedGroups(), ",")) .append(" ]\n"); } if (t.getExcludedGroups().size() > 0) { result .append(sp2) .append("excludedGroups: [ ") .append(Utils.join(t.getExcludedGroups(), ",")) .append(" ]\n"); } Map<String, List<String>> mg = t.getMetaGroups(); if (mg.size() > 0) { result.append(sp2).append("metaGroups: { "); boolean first = true; for (String group : mg.keySet()) { if (!first) result.append(", "); result.append(group).append(": [ ").append(Utils.join(mg.get(group), ",")).append(" ] "); first = false; } result.append(" }\n"); } if (t.getXmlPackages().size() > 0) { result.append(sp2).append("xmlPackages:\n"); for (XmlPackage xp : t.getXmlPackages()) { toYaml(result, sp2 + " - ", xp); } } if (t.getXmlClasses().size() > 0) { result.append(sp2).append("classes:\n"); for (XmlClass xc : t.getXmlClasses()) { toYaml(result, sp2 + " ", xc); } } result.append("\n"); }