@Override public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { List<ITestResult> list = new ArrayList<ITestResult>(); for (ISuite suite : suites) { Map<String, ISuiteResult> suiteResults = suite.getResults(); for (ISuiteResult suiteResult : suiteResults.values()) { ITestContext testContext = suiteResult.getTestContext(); IResultMap passedTests = testContext.getPassedTests(); IResultMap failedTests = testContext.getFailedTests(); IResultMap skippedTests = testContext.getSkippedTests(); IResultMap failedConfig = testContext.getFailedConfigurations(); list.addAll(this.listTestResult(passedTests)); list.addAll(this.listTestResult(failedTests)); list.addAll(this.listTestResult(skippedTests)); list.addAll(this.listTestResult(failedConfig)); } } this.sort(list); this.outputResult(list, outputDirectory+"/test.txt"); }
@Test public void testFinishHasFailedTests() { IResultMap failedTests = mock(IResultMap.class); ResultMap empty = new ResultMap(); when(testContext.getFailedConfigurations()).thenReturn(empty); when(testContext.getSkippedTests()).thenReturn(empty); when(testContext.getSkippedConfigurations()).thenReturn(empty); when(failedTests.size()).thenReturn(1); when(testContext.getFailedTests()).thenReturn(failedTests); FinishTestItemRQ rq = testNGService.buildFinishTestRq(testContext); assertThat("Incorrect status", rq.getStatus(), is(Statuses.FAILED)); }
@Test public void testFinishHasFailedConfigurations() { IResultMap failedConfigurations = mock(IResultMap.class); ResultMap empty = new ResultMap(); when(testContext.getFailedTests()).thenReturn(empty); when(testContext.getSkippedTests()).thenReturn(empty); when(testContext.getSkippedConfigurations()).thenReturn(empty); when(failedConfigurations.size()).thenReturn(1); when(testContext.getFailedConfigurations()).thenReturn(failedConfigurations); FinishTestItemRQ rq = testNGService.buildFinishTestRq(testContext); assertThat("Incorrect status", rq.getStatus(), is(Statuses.FAILED)); }
@Test public void testFinishHasSkippedTest() { IResultMap skippedTests = mock(IResultMap.class); ResultMap empty = new ResultMap(); when(testContext.getFailedTests()).thenReturn(empty); when(testContext.getFailedConfigurations()).thenReturn(empty); when(testContext.getSkippedConfigurations()).thenReturn(empty); when(skippedTests.size()).thenReturn(1); when(testContext.getSkippedTests()).thenReturn(skippedTests); FinishTestItemRQ rq = testNGService.buildFinishTestRq(testContext); assertThat("Incorrect status", rq.getStatus(), is(Statuses.FAILED)); }
@Test public void testFinishHasSkippedConfigurationsTest() { IResultMap skippedConfigurations = mock(IResultMap.class); ResultMap empty = new ResultMap(); when(testContext.getFailedTests()).thenReturn(empty); when(testContext.getFailedConfigurations()).thenReturn(empty); when(testContext.getSkippedTests()).thenReturn(empty); when(skippedConfigurations.size()).thenReturn(1); when(testContext.getSkippedConfigurations()).thenReturn(skippedConfigurations); FinishTestItemRQ rq = testNGService.buildFinishTestRq(testContext); assertThat("Incorrect status", rq.getStatus(), is(Statuses.FAILED)); }
@Override public void onFinish(ITestContext context) { final ISuite suite = context.getSuite(); final String suiteName = suite == null ? "[UNKNOWN]" : suite.getName(); final IResultMap passed = context.getPassedTests(); final IResultMap failed = context.getFailedTests(); final IResultMap skipped = context.getSkippedTests(); final int passedCount = passed == null ? -1 : passed.size(); final int failedCount = failed == null ? -1 : failed.size(); final int skippedCount = skipped == null ? -1 : skipped.size(); Log.i(TAG, "Finished test run \"" + suiteName + "\" with " + passedCount + " successful tests, " + failedCount + " failures and " + skippedCount + " tests skipped"); }
private void addAllTestResults(Set<ITestResult> testResults, IResultMap resultMap) { if (resultMap != null) { // Sort the results chronologically before adding them List<ITestResult> allResults = new ArrayList<ITestResult>(); allResults.addAll(resultMap.getAllResults()); Collections.sort(new ArrayList<>(allResults), new Comparator<ITestResult>() { @Override public int compare(ITestResult o1, ITestResult o2) { return (int) (o1.getStartMillis() - o2.getStartMillis()); } }); testResults.addAll(allResults); } }
private void resultDetail(IResultMap tests) { for (ITestResult result : tests.getAllResults()) { ITestNGMethod method = result.getMethod(); int methodId = getId(result); String cname = method.getTestClass().getName(); m_out.println("<h2 id=\"m" + methodId + "\" name=\"m" + methodId + "\" >" + cname + ":" + method.getMethodName() + "</h2>"); Set<ITestResult> resultSet = tests.getResults(method); generateForResult(result, method, resultSet.size()); m_out.println("<p class=\"totop\"><a href=\"#summary\">back to summary</a></p>"); } }
/** * Get All tests id by class + method + parameters hash code. * * @param context * @param suite */ private void getAllTestIds(ITestContext context, ISuite suite) { IResultMap passTests = context.getPassedTests(); IResultMap failTests = context.getFailedTests(); List<IInvokedMethod> invokedMethods = suite.getAllInvokedMethods(); for (IInvokedMethod im : invokedMethods) { if (passTests.getAllMethods().contains(im.getTestMethod()) || failTests.getAllMethods().contains(im.getTestMethod())) { int testId = getId(im.getTestResult()); // m_out.println("ALLtestid=" + testId); allRunTestIds.add(testId); } } }
/** * @param tests * @return */ private Collection<ITestNGMethod> getMethodSet(IResultMap tests) { Set r = new TreeSet<ITestNGMethod>(new TestSorter<ITestNGMethod>()); r.addAll(tests.getAllMethods()); return r; }
@Override public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { List<ITestResult> list = new ArrayList<ITestResult>(); for (ISuite suite : suites) { Map<String, ISuiteResult> suiteResults = suite.getResults(); for (ISuiteResult suiteResult : suiteResults.values()) { ITestContext testContext = suiteResult.getTestContext(); IResultMap passedTests = testContext.getPassedTests(); IResultMap failedTests = testContext.getFailedTests(); IResultMap skippedTests = testContext.getSkippedTests(); IResultMap failedConfig = testContext.getFailedConfigurations(); list.addAll(this.listTestResult(passedTests)); list.addAll(this.listTestResult(failedTests)); list.addAll(this.listTestResult(skippedTests)); //list.addAll(this.listTestResult(failedConfig)); } } CopyReportResources reportReource=new CopyReportResources(); try { reportReource.copyResources(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } this.sort(list); this.outputResult(list, outputDirectory+"/report.html"); }
@Test public void testFinishSuiteFailed() { ISuiteResult suiteResult = mock(ISuiteResult.class); Map<String, ISuiteResult> suiteResults = new HashMap<String, ISuiteResult>(1); suiteResults.put("", suiteResult); IResultMap resultMap = mock(ResultMap.class); when(iSuite.getResults()).thenReturn(suiteResults); when(suiteResult.getTestContext()).thenReturn(testContext); when(testContext.getFailedTests()).thenReturn(resultMap); when(resultMap.size()).thenReturn(1); FinishTestItemRQ rq = testNGService.buildFinishTestSuiteRq(iSuite); assertThat(rq.getStatus(), is(Statuses.FAILED)); }
/** * Get All tests id by class + method + parameters hash code. * @param context * @param suite */ private void getAllTestIds(ITestContext context, ISuite suite) { IResultMap passTests = context.getPassedTests(); IResultMap failTests = context.getFailedTests(); List<IInvokedMethod> invokedMethods = suite.getAllInvokedMethods(); for (IInvokedMethod im : invokedMethods) { if (passTests.getAllMethods().contains(im.getTestMethod()) || failTests.getAllMethods().contains(im.getTestMethod())) { int testId = getId(im.getTestResult()); // m_out.println("ALLtestid=" + testId); allRunTestIds.add(testId); } } }
private boolean hasFailedBefore(IResultMap failureList, ITestResult passedResult) { for (ITestResult result : failureList.getAllResults()) { String failedTest = result.getMethod().getRealClass().toString()+ "." + result.getMethod().getMethodName(); String passedTest = passedResult.getMethod().getRealClass().toString() + "." + passedResult.getMethod().getMethodName(); if (failedTest.equals(passedTest) && methodParameterEqual(result, passedResult)) { return true; } } return false; }
protected int findFailed(ITestContext context) { IResultMap failedConfigurations = context.getFailedConfigurations(); IResultMap skippedConfigurations = context.getSkippedConfigurations(); int countfailed = 0; if (failedConfigurations!= null) { countfailed += failedConfigurations.size(); } if (skippedConfigurations != null) { countfailed += skippedConfigurations.size(); } return countfailed; }
/** * @param tests */ private void resultSummary(IResultMap tests, String testname, String style, String details) { if (tests.getAllResults().size() > 0) { StringBuffer buff = new StringBuffer(); String lastClassName = ""; int mq = 0; int cq = 0; for (ITestNGMethod method : getMethodSet(tests)) { m_row += 1; m_methodIndex += 1; ITestClass testClass = method.getTestClass(); String className = testClass.getName(); if (mq == 0) { titleRow(testname + " — " + style + details, 4); } if (!className.equalsIgnoreCase(lastClassName)) { if (mq > 0) { cq += 1; m_out.println("<tr class=\"" + style + (cq % 2 == 0 ? "even" : "odd") + "\">" + "<td rowspan=\"" + mq + "\">" + lastClassName + buff); } mq = 0; buff.setLength(0); lastClassName = className; } Set<ITestResult> resultSet = tests.getResults(method); long end = Long.MIN_VALUE; long start = Long.MAX_VALUE; for (ITestResult testResult : tests.getResults(method)) { if (testResult.getEndMillis() > end) { end = testResult.getEndMillis(); } if (testResult.getStartMillis() < start) { start = testResult.getStartMillis(); } } mq += 1; if (mq > 1) { buff.append("<tr class=\"" + style + (cq % 2 == 0 ? "odd" : "even") + "\">"); } String description = method.getDescription(); String testInstanceName = resultSet.toArray(new ITestResult[]{})[0].getTestName(); buff.append("<td><a href=\"#m" + m_methodIndex + "\">" + qualifiedName(method) + " " + (description != null && description.length() > 0 ? "(\"" + description + "\")" : "") + "</a>" + (null == testInstanceName ? "" : "<br>(" + testInstanceName + ")") + "</td>" + "<td class=\"numi\">" + resultSet.size() + "</td><td class=\"numi\">" + (end - start) + "</td></tr>"); } if (mq > 0) { cq += 1; m_out.println("<tr class=\"" + style + (cq % 2 == 0 ? "even" : "odd") + "\">" + "<td rowspan=\"" + mq + "\">" + lastClassName + buff); } } }
private ArrayList<ITestResult> listTestResult(IResultMap resultMap){ Set<ITestResult> results = resultMap.getAllResults(); return new ArrayList<ITestResult>(results); }
/** * Creates EARL statements from the given test results. A test result is * described by an Assertion resource. The TestResult and TestCase resources * are linked to the Assertion in accord with the EARL schema; the latter is * also linked to a TestRequirement. * * @param earl * An RDF Model containing EARL statements. * @param results * The results of invoking a collection of test methods. */ void processTestResults(Model earl, IResultMap results) { for (ITestResult tngResult : results.getAllResults()) { // create earl:Assertion long endTime = tngResult.getEndMillis(); GregorianCalendar calTime = new GregorianCalendar(TimeZone.getDefault()); calTime.setTimeInMillis(endTime); Resource assertion = earl.createResource("assert-" + ++this.resultCount, EARL.Assertion); assertion.addProperty(EARL.mode, EARL.AutomaticMode); assertion.addProperty(EARL.assertedBy, this.assertor); assertion.addProperty(EARL.subject, this.testSubject); // link earl:TestResult to earl:Assertion Resource earlResult = earl.createResource("result-" + this.resultCount, EARL.TestResult); earlResult.addProperty(DCTerms.date, earl.createTypedLiteral(calTime)); switch (tngResult.getStatus()) { case ITestResult.FAILURE: earlResult.addProperty(DCTerms.description, getDetailMessage(tngResult)); if (AssertionError.class.isInstance(tngResult.getThrowable())) { earlResult.addProperty(EARL.outcome, EARL.Fail); } else { // an exception occurred earlResult.addProperty(EARL.outcome, EARL.CannotTell); } processResultAttributes(earlResult, tngResult); break; case ITestResult.SKIP: earlResult.addProperty(DCTerms.description, getDetailMessage(tngResult)); earlResult.addProperty(EARL.outcome, EARL.NotTested); break; default: earlResult.addProperty(EARL.outcome, EARL.Pass); break; } assertion.addProperty(EARL.result, earlResult); // link earl:TestCase to earl:Assertion and earl:TestRequirement String testMethodName = tngResult.getMethod().getMethodName(); String testClassName = tngResult.getTestClass().getName().replaceAll("\\.", "/"); StringBuilder testCaseId = new StringBuilder(testClassName); testCaseId.append('#').append(testMethodName); Resource testCase = earl.createResource(testCaseId.toString(), EARL.TestCase); testCase.addProperty(DCTerms.title, breakIntoWords(testMethodName)); String testDescr = tngResult.getMethod().getDescription(); if (null != testDescr && !testDescr.isEmpty()) { testCase.addProperty(DCTerms.description, testDescr); } assertion.addProperty(EARL.test, testCase); String testReqName = tngResult.getTestContext().getName().replaceAll("\\s", "-"); earl.createResource(testReqName).addProperty(DCTerms.hasPart, testCase); } }