@Override
 public void onTestFailure(ITestResult result) {
   ClassInfo classInfo = getClassInfo(result);
   if (classInfo == null) return;
   classInfo.failed(result.getStartMillis(), result.getEndMillis());
   maybeReport(classInfo);
 }
Ejemplo n.º 2
0
    private void recordResult(ITestResult result, ResultType type, Throwable failure) {
      // Restore the original stdout/stderr.
      System.setOut(originalOut);
      System.setErr(originalErr);

      // Get the stdout/stderr written during the test as strings.
      stdOutStream.flush();
      stdErrStream.flush();

      String stdOut = streamToString(rawStdOutBytes);
      String stdErr = streamToString(rawStdErrBytes);

      String className = result.getTestClass().getName();
      String methodName = calculateTestMethodName(result);

      // use exception from suite, if available
      if (suiteFailureListener.getThrowable() != null) {
        failure = suiteFailureListener.getThrowable();
        stdOut = suiteFailureListener.getStdOut();
        stdErr = suiteFailureListener.getStdErr();
      }

      long runTimeMillis = result.getEndMillis() - result.getStartMillis();
      results.add(
          new TestResult(className, methodName, runTimeMillis, type, failure, stdOut, stdErr));
    }
 private void onTestFinished(ITestResult iTestResult, TestResult.ResultType resultType) {
   Object testId;
   TestStartEvent startEvent = null;
   synchronized (lock) {
     testId = tests.remove(iTestResult);
     if (testId == null) {
       // This can happen when a method fails which this method depends on
       testId = idGenerator.generateId();
       Object parentId = testMethodToSuiteMapping.get(iTestResult.getMethod());
       startEvent = new TestStartEvent(iTestResult.getStartMillis(), parentId);
     }
   }
   if (startEvent != null) {
     // Synthesize a start event
     resultProcessor.started(
         new DefaultTestMethodDescriptor(
             testId, iTestResult.getTestClass().getName(), iTestResult.getName()),
         startEvent);
   }
   if (resultType == TestResult.ResultType.FAILURE) {
     resultProcessor.failure(testId, iTestResult.getThrowable());
   }
   resultProcessor.completed(
       testId, new TestCompleteEvent(iTestResult.getEndMillis(), resultType));
 }
Ejemplo n.º 4
0
  protected void addTest(Class<?> clazz, ITestResult result) {
    try {
      TestCase test_case =
          new TestCase(
              result.getStatus(),
              clazz.getName(),
              getMethodName(result),
              result.getStartMillis(),
              result.getEndMillis());
      switch (result.getStatus()) {
        case ITestResult.FAILURE:
        case ITestResult.SKIP:
          Throwable ex = result.getThrowable();
          if (ex != null) {
            String failure_type = ex.getClass().getName();
            String failure_msg = ex.getMessage();
            String stack_trace = printException(ex);
            test_case.setFailure(failure_type, failure_msg, stack_trace);
          } else test_case.setFailure("exception", "SKIPPED", null);
          break;
      }

      synchronized (
          this) { // handle concurrent access by different threads, if test methods are run in
        // parallel
        DataOutputStream output = tests.get(clazz);
        test_case.writeTo(output);
      }
    } catch (Exception e) {
      error(e.toString());
    }
  }
Ejemplo n.º 5
0
 private void log(ITestResult tr, String RESULT) {
   System.out.print(
       String.format(
           "\t%s.%s %s (%dms)\n",
           tr.getTestClass().getName(),
           tr.getName(),
           RESULT,
           tr.getEndMillis() - tr.getStartMillis()));
   if (++m_count % 40 == 0) {
     System.out.println("");
   }
 }
 public void onConfigurationFailure(ITestResult testResult) {
   // Synthesise a test for the broken configuration method
   TestDescriptorInternal test =
       new DefaultTestMethodDescriptor(
           idGenerator.generateId(),
           testResult.getMethod().getTestClass().getName(),
           testResult.getMethod().getMethodName());
   resultProcessor.started(test, new TestStartEvent(testResult.getStartMillis()));
   resultProcessor.failure(test.getId(), testResult.getThrowable());
   resultProcessor.completed(
       test.getId(),
       new TestCompleteEvent(testResult.getEndMillis(), TestResult.ResultType.FAILURE));
 }
 public void onConfigurationFailure(ITestResult testResult) {
   if (failedConfigurations.put(testResult, true) != null) {
     // workaround for bug in TestNG 6.2 (apparently fixed in some 6.3.x): listener is notified
     // twice per event
     return;
   }
   // Synthesise a test for the broken configuration method
   TestDescriptorInternal test =
       new DefaultTestMethodDescriptor(
           idGenerator.generateId(),
           testResult.getMethod().getTestClass().getName(),
           testResult.getMethod().getMethodName());
   resultProcessor.started(test, new TestStartEvent(testResult.getStartMillis()));
   resultProcessor.failure(test.getId(), testResult.getThrowable());
   resultProcessor.completed(
       test.getId(),
       new TestCompleteEvent(testResult.getEndMillis(), TestResult.ResultType.FAILURE));
 }
  public void onTestStart(ITestResult iTestResult) {
    TestDescriptorInternal testInternal;
    Object parentId;
    synchronized (lock) {
      testInternal =
          new DefaultTestMethodDescriptor(
              idGenerator.generateId(),
              iTestResult.getTestClass().getName(),
              iTestResult.getName());
      Object oldTestId = tests.put(iTestResult, testInternal.getId());
      assert oldTestId == null
          : "Apparently some other test has started but it hasn't finished. "
              + "Expect the resultProcessor to break. "
              + "Don't expect to see this assertion stack trace due to the current architecture";

      parentId = testMethodToSuiteMapping.get(iTestResult.getMethod());
      assert parentId != null;
    }
    resultProcessor.started(
        testInternal, new TestStartEvent(iTestResult.getStartMillis(), parentId));
  }
  /** @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);
      }
    }
  }