private void writeSuiteGroups(XMLStringBuffer xmlBuffer, ISuite suite) { xmlBuffer.push(XMLReporterConfig.TAG_GROUPS); Map<String, Collection<ITestNGMethod>> methodsByGroups = suite .getMethodsByGroups(); for (Map.Entry<String, Collection<ITestNGMethod>> entry : methodsByGroups .entrySet()) { Properties groupAttrs = new Properties(); groupAttrs.setProperty(XMLReporterConfig.ATTR_NAME, entry.getKey()); xmlBuffer.push(XMLReporterConfig.TAG_GROUP, groupAttrs); Set<ITestNGMethod> groupMethods = getUniqueMethodSet(entry .getValue()); for (ITestNGMethod groupMethod : groupMethods) { Properties methodAttrs = new Properties(); methodAttrs.setProperty(XMLReporterConfig.ATTR_NAME, groupMethod.getMethodName()); methodAttrs.setProperty(XMLReporterConfig.ATTR_METHOD_SIG, groupMethod.toString()); methodAttrs.setProperty(XMLReporterConfig.ATTR_CLASS, groupMethod.getRealClass().getName()); xmlBuffer.addEmptyElement(XMLReporterConfig.TAG_METHOD, methodAttrs); } xmlBuffer.pop(); } xmlBuffer.pop(); }
/** * Add started-at, finished-at and duration-ms attributes to the <suite> tag */ public static void addDurationAttributes(XMLReporterConfig config, Properties attributes, Date minStartDate, Date maxEndDate) { SimpleDateFormat format = new SimpleDateFormat( XMLReporterConfig.getTimestampFormat()); TimeZone utc = TimeZone.getTimeZone("UTC"); format.setTimeZone(utc); String startTime = format.format(minStartDate); String endTime = format.format(maxEndDate); long duration = maxEndDate.getTime() - minStartDate.getTime(); attributes.setProperty(XMLReporterConfig.ATTR_STARTED_AT, startTime); attributes.setProperty(XMLReporterConfig.ATTR_FINISHED_AT, endTime); attributes.setProperty(XMLReporterConfig.ATTR_DURATION_MS, Long.toString(duration)); }
private void addTestResults(XMLStringBuffer xmlBuffer, Set<ITestResult> testResults) { Map<String, List<ITestResult>> testsGroupedByClass = buildTestClassGroups(testResults); for (Map.Entry<String, List<ITestResult>> result : testsGroupedByClass.entrySet()) { Properties attributes = new Properties(); String className = result.getKey(); if (config.isSplitClassAndPackageNames()) { int dot = className.lastIndexOf('.'); attributes.setProperty(XMLReporterConfig.ATTR_NAME, dot > -1 ? className.substring(dot + 1, className.length()) : className); attributes.setProperty(XMLReporterConfig.ATTR_PACKAGE, dot > -1 ? className.substring(0, dot) : "[default]"); } else { attributes.setProperty(XMLReporterConfig.ATTR_NAME, className); } xmlBuffer.push(XMLReporterConfig.TAG_CLASS, attributes); List<ITestResult> sortedResults = result.getValue(); Collections.sort( sortedResults ); for (ITestResult testResult : sortedResults) { addTestResult(xmlBuffer, testResult); } xmlBuffer.pop(); } }
private void addTestResult(XMLStringBuffer xmlBuffer, ITestResult testResult) { Properties attribs = getTestResultAttributes(testResult); attribs.setProperty(XMLReporterConfig.ATTR_STATUS, getStatusString(testResult.getStatus())); String testInstanceName; if (testResult.getName() == null) { testInstanceName = ""; } else { testInstanceName = testResult.getName(); } attribs.setProperty(XMLReporterConfig.ATTR_TEST_INSTANCE_NAME, testInstanceName); xmlBuffer.push(XMLReporterConfig.TAG_TEST_METHOD, attribs); addTestMethodParams(xmlBuffer, testResult); TestStepsXMLReporterUtils.addTestSteps(xmlBuffer, testResult); addTestResultException(xmlBuffer, testResult); addTestResultOutput(xmlBuffer, testResult); if (config.isGenerateTestResultAttributes()) { addTestResultAttributes(xmlBuffer, testResult); } xmlBuffer.pop(); }
private void addTestResultAttributes(XMLStringBuffer xmlBuffer, ITestResult testResult) { if (testResult.getAttributeNames() != null && testResult.getAttributeNames().size() > 0) { xmlBuffer.push(XMLReporterConfig.TAG_ATTRIBUTES); for (String attrName: testResult.getAttributeNames()) { if (attrName == null) { continue; } Object attrValue = testResult.getAttribute(attrName); Properties attributeAttrs = new Properties(); attributeAttrs.setProperty(XMLReporterConfig.ATTR_NAME, attrName); if (attrValue == null) { attributeAttrs.setProperty(XMLReporterConfig.ATTR_IS_NULL, "true"); xmlBuffer.addEmptyElement(XMLReporterConfig.TAG_ATTRIBUTE, attributeAttrs); } else { xmlBuffer.push(XMLReporterConfig.TAG_ATTRIBUTE, attributeAttrs); xmlBuffer.addCDATA(attrValue.toString()); xmlBuffer.pop(); } } xmlBuffer.pop(); } }
@Override public void startElement (String uri, String localName, String qName, Attributes attributes) { p("Start " + qName); if (TAG_SUITE.equals(qName)) { m_suiteListener.onInitialization(new GenericMessage()); m_suiteMethodCount = 0; m_currentSuite = new SuiteMessage(attributes.getValue(ATTR_NAME), true /* start */, m_suiteMethodCount); m_suiteListener.onStart(m_currentSuite); } else if (TAG_TEST.equals(qName)) { m_passed = m_failed = m_skipped = 0; m_currentTest = new TestMessage(true /* start */, m_currentSuite.getSuiteName(), attributes.getValue(ATTR_NAME), m_testMethodCount, m_passed, m_failed, m_skipped, 0); m_testListener.onStart(m_currentTest); } else if (TAG_CLASS.equals(qName)) { m_className = attributes.getValue(ATTR_NAME); } else if (TAG_TEST_METHOD.equals(qName)) { Integer status = XMLReporterConfig.getStatus(attributes.getValue(ATTR_STATUS)); m_currentTestResult = new TestResultMessage(status, m_currentSuite.getSuiteName(), m_currentTest.getTestName(), m_className, attributes.getValue(ATTR_NAME), attributes.getValue(ATTR_DESC), attributes.getValue(ATTR_DESC), new String[0], /* no parameters, filled later */ 0, Long.parseLong(attributes.getValue(ATTR_DURATION_MS)), "" /* stack trace, filled later */, m_invocationCount, m_currentInvocationCount); m_suiteMethodCount++; m_testMethodCount++; if (status == ITestResult.SUCCESS) m_passed++; else if (status == ITestResult.FAILURE) m_failed++; else if (status == ITestResult.SKIP) m_skipped++; } else if (TAG_PARAMS.equals(qName)) { m_params = Lists.newArrayList(); } }
public void postProcess() { groupedTestMethods = new LinkedHashMap<GroupModel, ArrayList<ClassModel>>(); uniqueID = UUID.randomUUID().toString(); for (TestModel tm : getTests()) { tm.postProcess(); totalPassed += tm.getTotalPassed(); totalFailed += tm.getTotalFailed(); totalSkipped += tm.getTotalSkipped(); totalClasses += tm.getClasses().size(); } if (totalFailed > 0 || totalSkipped > 0) { overallStatus = XMLReporterConfig.TEST_FAILED; } totalTests = totalPassed + totalFailed + totalSkipped; }
@Override public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { if (Utils.isStringEmpty(config.getOutputDirectory())) { config.setOutputDirectory(outputDirectory); } // Calculate passed/failed/skipped int passed = 0; int failed = 0; int skipped = 0; for (ISuite s : suites) { for (ISuiteResult sr : s.getResults().values()) { ITestContext testContext = sr.getTestContext(); passed += testContext.getPassedTests().size(); failed += testContext.getFailedTests().size(); skipped += testContext.getSkippedTests().size(); } } rootBuffer = new XMLStringBuffer(); Properties p = new Properties(); p.put("passed", passed); p.put("failed", failed); p.put("skipped", skipped); p.put("total", passed + failed + skipped); rootBuffer.push(XMLReporterConfig.TAG_TESTNG_RESULTS, p); writeReporterOutput(rootBuffer); for (int i = 0; i < suites.size(); i++) { writeSuite(suites.get(i).getXmlSuite(), suites.get(i)); } rootBuffer.pop(); Utils.writeUtf8File(config.getOutputDirectory(), FILE_NAME, rootBuffer, null /* no prefix */); }
private void writeReporterOutput(XMLStringBuffer xmlBuffer) { // TODO: Cosmin - maybe a <line> element isn't indicated for each line xmlBuffer.push(XMLReporterConfig.TAG_REPORTER_OUTPUT); List<String> output = Reporter.getOutput(); for (String line : output) { if (line != null) { xmlBuffer.push(XMLReporterConfig.TAG_LINE); xmlBuffer.addCDATA(line); xmlBuffer.pop(); } } xmlBuffer.pop(); }
private void writeSuite(XmlSuite xmlSuite, ISuite suite) { switch (config.getFileFragmentationLevel()) { case XMLReporterConfig.FF_LEVEL_NONE: writeSuiteToBuffer(rootBuffer, suite); break; case XMLReporterConfig.FF_LEVEL_SUITE: case XMLReporterConfig.FF_LEVEL_SUITE_RESULT: File suiteFile = referenceSuite(rootBuffer, suite); writeSuiteToFile(suiteFile, suite); } }
private File referenceSuite(XMLStringBuffer xmlBuffer, ISuite suite) { String relativePath = suite.getName() + File.separatorChar + FILE_NAME; File suiteFile = new File(config.getOutputDirectory(), relativePath); Properties attrs = new Properties(); attrs.setProperty(XMLReporterConfig.ATTR_URL, relativePath); xmlBuffer.addEmptyElement(XMLReporterConfig.TAG_SUITE, attrs); return suiteFile; }
private void writeSuiteToBuffer(XMLStringBuffer xmlBuffer, ISuite suite) { xmlBuffer.push(XMLReporterConfig.TAG_SUITE, getSuiteAttributes(suite)); writeSuiteGroups(xmlBuffer, suite); Map<String, ISuiteResult> results = suite.getResults(); ATEXMLSuiteResultWriter suiteResultWriter = new ATEXMLSuiteResultWriter( config); for (Map.Entry<String, ISuiteResult> result : results.entrySet()) { suiteResultWriter.writeSuiteResult(xmlBuffer, result.getValue()); } xmlBuffer.pop(); }
private Properties getSuiteAttributes(ISuite suite) { Properties props = new Properties(); props.setProperty(XMLReporterConfig.ATTR_NAME, suite.getName()); // Calculate the duration Map<String, ISuiteResult> results = suite.getResults(); Date minStartDate = new Date(); Date maxEndDate = null; // TODO: We could probably optimize this in order not to traverse this // twice for (Map.Entry<String, ISuiteResult> result : results.entrySet()) { ITestContext testContext = result.getValue().getTestContext(); Date startDate = testContext.getStartDate(); Date endDate = testContext.getEndDate(); if (minStartDate.after(startDate)) { minStartDate = startDate; } if (maxEndDate == null || maxEndDate.before(endDate)) { maxEndDate = endDate != null ? endDate : startDate; } } // The suite could be completely empty if (maxEndDate == null) { maxEndDate = minStartDate; } addDurationAttributes(config, props, minStartDate, maxEndDate); return props; }
/** * Writes the specified ISuiteResult in the given XMLStringBuffer. Please consider that depending on the settings in * the <code>config</code> property it might generate an additional XML file with the actual content and only * reference the file with an <code>url</code> attribute in the passed XMLStringBuffer. * * @param xmlBuffer The XML buffer where to write or reference the suite result * @param suiteResult The <code>ISuiteResult</code> to serialize */ public void writeSuiteResult(XMLStringBuffer xmlBuffer, ISuiteResult suiteResult) { if (XMLReporterConfig.FF_LEVEL_SUITE_RESULT != config.getFileFragmentationLevel()) { writeAllToBuffer(xmlBuffer, suiteResult); } else { String parentDir = config.getOutputDirectory() + File.separatorChar + suiteResult.getTestContext().getSuite().getName(); File file = referenceSuiteResult(xmlBuffer, parentDir, suiteResult); XMLStringBuffer suiteXmlBuffer = new XMLStringBuffer(); writeAllToBuffer(suiteXmlBuffer, suiteResult); Utils.writeUtf8File(file.getAbsoluteFile().getParent(), file.getName(), suiteXmlBuffer.toXML()); } }
private void writeAllToBuffer(XMLStringBuffer xmlBuffer, ISuiteResult suiteResult) { xmlBuffer.push(XMLReporterConfig.TAG_TEST, getSuiteResultAttributes(suiteResult)); Set<ITestResult> testResults = Sets.newHashSet(); ITestContext testContext = suiteResult.getTestContext(); addAllTestResults(testResults, testContext.getPassedTests()); addAllTestResults(testResults, testContext.getFailedTests()); addAllTestResults(testResults, testContext.getSkippedTests()); addAllTestResults(testResults, testContext.getPassedConfigurations()); addAllTestResults(testResults, testContext.getSkippedConfigurations()); addAllTestResults(testResults, testContext.getFailedConfigurations()); addAllTestResults(testResults, testContext.getFailedButWithinSuccessPercentageTests()); addTestResults(xmlBuffer, testResults); xmlBuffer.pop(); }
private File referenceSuiteResult(XMLStringBuffer xmlBuffer, String parentDir, ISuiteResult suiteResult) { Properties attrs = new Properties(); String suiteResultName = suiteResult.getTestContext().getName() + ".xml"; attrs.setProperty(XMLReporterConfig.ATTR_URL, suiteResultName); xmlBuffer.addEmptyElement(XMLReporterConfig.TAG_TEST, attrs); return new File(parentDir + File.separatorChar + suiteResultName); }
private Properties getSuiteResultAttributes(ISuiteResult suiteResult) { Properties attributes = new Properties(); ITestContext tc = suiteResult.getTestContext(); attributes.setProperty(XMLReporterConfig.ATTR_NAME, tc.getName()); ATEXMLReporter.addDurationAttributes(config, attributes, tc.getStartDate(), tc.getEndDate()); return attributes; }
public void addTestMethodParams(XMLStringBuffer xmlBuffer, ITestResult testResult) { Object[] parameters = testResult.getParameters(); if ((parameters != null) && (parameters.length > 0)) { xmlBuffer.push(XMLReporterConfig.TAG_PARAMS); for (int i = 0; i < parameters.length; i++) { addParameter(xmlBuffer, parameters[i], i); } xmlBuffer.pop(); } }
private void addParameter(XMLStringBuffer xmlBuffer, Object parameter, int i) { Properties attrs = new Properties(); attrs.setProperty(XMLReporterConfig.ATTR_INDEX, String.valueOf(i)); xmlBuffer.push(XMLReporterConfig.TAG_PARAM, attrs); if (parameter == null) { Properties valueAttrs = new Properties(); valueAttrs.setProperty(XMLReporterConfig.ATTR_IS_NULL, "true"); xmlBuffer.addEmptyElement(XMLReporterConfig.TAG_PARAM_VALUE, valueAttrs); } else { xmlBuffer.push(XMLReporterConfig.TAG_PARAM_VALUE); xmlBuffer.addCDATA(parameter.toString()); xmlBuffer.pop(); } xmlBuffer.pop(); }
private void addTestResultException(XMLStringBuffer xmlBuffer, ITestResult testResult) { Throwable exception = testResult.getThrowable(); if (exception != null) { Properties exceptionAttrs = new Properties(); exceptionAttrs.setProperty(XMLReporterConfig.ATTR_CLASS, exception.getClass().getName()); xmlBuffer.push(XMLReporterConfig.TAG_EXCEPTION, exceptionAttrs); if (!Utils.isStringEmpty(exception.getMessage())) { xmlBuffer.push(XMLReporterConfig.TAG_MESSAGE); xmlBuffer.addCDATA(exception.getMessage()); xmlBuffer.pop(); } String[] stackTraces = Utils.stackTrace(exception, false); if ((config.getStackTraceOutputMethod() & XMLReporterConfig.STACKTRACE_SHORT) == XMLReporterConfig .STACKTRACE_SHORT) { xmlBuffer.push(XMLReporterConfig.TAG_SHORT_STACKTRACE); xmlBuffer.addCDATA(stackTraces[0]); xmlBuffer.pop(); } if ((config.getStackTraceOutputMethod() & XMLReporterConfig.STACKTRACE_FULL) == XMLReporterConfig .STACKTRACE_FULL) { xmlBuffer.push(XMLReporterConfig.TAG_FULL_STACKTRACE); xmlBuffer.addCDATA(stackTraces[1]); xmlBuffer.pop(); } xmlBuffer.pop(); } }
private void addTestResultOutput(XMLStringBuffer xmlBuffer, ITestResult testResult) { // TODO: Cosmin - maybe a <line> element isn't indicated for each line xmlBuffer.push(XMLReporterConfig.TAG_REPORTER_OUTPUT); List<String> output = Reporter.getOutput(testResult); for (String line : output) { if (line != null) { xmlBuffer.push(XMLReporterConfig.TAG_LINE); xmlBuffer.addCDATA(line); xmlBuffer.pop(); } } xmlBuffer.pop(); }
/** * Creates an XML reporter that suppresses stack traces and includes test * result attributes (if set). * * @return A customized reporter that generates an XML representation of the * test results. The document element is <testng-results>. */ XMLReporter createCustomXMLReporter() { // config data syntax: "class-name:prop1=val1,prop2=val2" StringBuilder reporterConfig = new StringBuilder( XMLReporter.class.getName() + ":"); reporterConfig.append("stackTraceOutputMethod=").append( XMLReporterConfig.STACKTRACE_NONE); reporterConfig.append(",").append("generateTestResultAttributes=true"); reporterConfig.append(",").append("generateGroupsAttribute=true"); ReporterConfig reporterConf = ReporterConfig.deserialize(reporterConfig .toString()); return (XMLReporter) reporterConf.newReporterInstance(); }
@Test public void createCustomXMLReporter() { BasicXMLReporter iut = new BasicXMLReporter(); XMLReporter result = iut.createCustomXMLReporter(); Assert.assertEquals("Unexpected stackTraceOutputMethod", XMLReporterConfig.STACKTRACE_NONE, result.getStackTraceOutputMethod()); Assert.assertNull("Expected null outputDirectory", result.getOutputDirectory()); }
public String getTimestampFormat() { return XMLReporterConfig.getTimestampFormat(); }
public ATEXMLSuiteResultWriter(XMLReporterConfig config) { this.config = config; }
/** * Run suites. * * @throws ClassNotFoundException * @throws IOException * @throws ParseException */ @TestProjectLoggable (level=ATELogLevel.INFO) public void runSuites() throws ClassNotFoundException, ParseException, IOException { final TestProjectListener tla = new TestProjectListener(this); final TestCaseResultModifier repeatStepResultModifier = new TestCaseResultModifier(); testng.addListener(tla); testng.addListener(repeatStepResultModifier); ATEXMLReporter rng = new ATEXMLReporter(); rng.setStackTraceOutputMethod(XMLReporterConfig.STACKTRACE_NONE); testng.addListener(rng); CaseRunnerGenerator crg = new CaseRunnerGenerator(this.getSuiteList()); crg.createCaseRunners(); if (0 == crg.loadCaseRunnerClasses()) { throw new ParseException("case runner generator error"); } final List<XmlPackage> packages = new ArrayList<XmlPackage>(); for (TestSuite tempSuite : getSuiteList()) { XmlPackage xmlpackage = new XmlPackage(); xmlpackage.setName(crg.getBasePackageName() + "." + tempSuite.getSuiteName()); packages.add(xmlpackage); } List<XmlSuite> xmlSuites = new ArrayList<XmlSuite>(); XmlSuite xmlProject = new XmlSuite(); XmlTest test = new XmlTest(xmlProject); test.setPackages(packages); xmlSuites.add(xmlProject); if (xmlSuites.isEmpty()) { throw new IllegalStateException("xmlsuites are not populated."); } else { testng.setXmlSuites(xmlSuites); testng.run(); } }