/**
  * Get All tests id by class + method + parameters hash code.
  *
  * @param context
  * @param suite
  * @author kevinkong
  */
 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 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>");
    }
  }
  /**
   * Since the methods will be sorted chronologically, we want to return the ITestNGMethod from the
   * invoked methods.
   */
  private Collection<ITestNGMethod> getMethodSet(IResultMap tests, ISuite suite) {
    List<IInvokedMethod> r = Lists.newArrayList();
    List<IInvokedMethod> invokedMethods = suite.getAllInvokedMethods();

    // Eliminate the repeat retry methods
    for (IInvokedMethod im : invokedMethods) {
      if (tests.getAllMethods().contains(im.getTestMethod())) {
        int testId = getId(im.getTestResult());
        if (!testIds.contains(testId)) {
          testIds.add(testId);
          r.add(im);
        }
      }
    }
    Arrays.sort(r.toArray(new IInvokedMethod[r.size()]), new TestSorter());
    List<ITestNGMethod> result = Lists.newArrayList();

    // Add all the invoked methods
    for (IInvokedMethod m : r) {
      result.add(m.getTestMethod());
    }

    // Add all the methods that weren't invoked (e.g. skipped) that we
    // haven't added yet
    // for (ITestNGMethod m : tests.getAllMethods()) {
    // if (!result.contains(m)) {
    // result.add(m);
    // }
    // }

    for (ITestResult allResult : tests.getAllResults()) {
      int testId = getId(allResult);
      if (!testIds.contains(testId)) {
        result.add(allResult.getMethod());
      }
    }

    return result;
  }
Пример #4
0
  @Override
  public void onFinish(ITestContext testContext) {
    super.onFinish(testContext);
    IResultMap passedMap = testContext.getPassedTests();
    IResultMap failedMap = testContext.getFailedTests();

    testResult.setEndTime(ts.getTime(":"));
    testResult.setDuration("");
    testResult.setFailCount(String.valueOf(failedMap.size()));
    testResult.setPassCount(String.valueOf(passedMap.size()));
    testResult.setTotalCount(String.valueOf(failedMap.size() + passedMap.size()));
    logger.info("testContext.getName()=" + testContext.getName());
    initTestCaseInfo();

    ITestClass className = testContext.getAllTestMethods()[0].getTestClass();
    WriteResultUtils utils = new WriteResultUtils();
    utils.writeResultSummary(testResult, getTemplatePath(className.getName()));
  }
  /** @param tests */
  private void resultSummary(
      ISuite suite, 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;
      Map<String, Integer> methods = new HashMap<String, Integer>();
      Set<String> setMethods = new HashSet<String>();
      for (ITestNGMethod method : getMethodSet(tests, suite)) {
        m_row += 1;

        ITestClass testClass = method.getTestClass();
        String className = testClass.getName();
        if (mq == 0) {
          String id = (m_testIndex == null ? null : "t" + Integer.toString(m_testIndex));
          titleRow(testname + " &#8212; " + style + details, 5, id);
          m_testIndex = null;
        }
        if (!className.equalsIgnoreCase(lastClassName)) {
          if (mq > 0) {
            cq += 1;
            m_out.print("<tr class=\"" + style + (cq % 2 == 0 ? "even" : "odd") + "\">" + "<td");
            if (mq > 1) {
              m_out.print(" rowspan=\"" + mq + "\"");
            }
            m_out.println(">" + lastClassName + "</td>" + 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();
        // Calculate each test run times, the result shown in the html
        // report.
        ITestResult[] results = resultSet.toArray(new ITestResult[] {});
        String methodName = method.getMethodName();
        if (setMethods.contains(methodName)) {
          methods.put(methodName, methods.get(methodName) + 1);
        } else {
          setMethods.add(methodName);
          methods.put(methodName, 0);
        }
        String parameterString = "";
        int count = 0;

        ITestResult result = null;
        if (results.length > methods.get(methodName)) {
          result = results[methods.get(methodName)];
          int testId = getId(result);

          for (Integer id : allRunTestIds) {
            if (id.intValue() == testId) count++;
          }
          Object[] parameters = result.getParameters();

          boolean hasParameters = parameters != null && parameters.length > 0;
          if (hasParameters) {
            for (Object p : parameters) {
              parameterString = parameterString + Utils.escapeHtml(p.toString()) + " ";
            }
          }
        }

        int methodId = method.getTestClass().getName().hashCode();
        methodId = methodId + method.getMethodName().hashCode();
        if (result != null)
          methodId =
              methodId
                  + (result.getParameters() != null ? Arrays.hashCode(result.getParameters()) : 0);

        buff.append(
            "<td><a href=\"#m"
                + methodId
                + "\">"
                + qualifiedName(method)
                + " "
                + (description != null && description.length() > 0
                    ? "(\"" + description + "\")"
                    : "")
                + "</a>"
                + (null == testInstanceName ? "" : "<br>(" + testInstanceName + ")")
                + "</td><td>"
                + this.getAuthors(className, method)
                + "</td><td class=\"numi\">"
                + resultSet.size()
                + "</td>"
                + "<td>"
                + (count == 0 ? "" : count)
                + "</td>"
                + "<td>"
                + parameterString
                + "</td>"
                + "<td>"
                + start
                + "</td>"
                + "<td class=\"numi\">"
                + (end - start)
                + "</td>"
                + "</tr>");
      }
      if (mq > 0) {
        cq += 1;
        m_out.print("<tr class=\"" + style + (cq % 2 == 0 ? "even" : "odd") + "\">" + "<td");
        if (mq > 1) {
          m_out.print(" rowspan=\"" + mq + "\"");
        }
        m_out.println(">" + lastClassName + "</td>" + buff);
      }
    }
  }