示例#1
0
  protected void setupStreams(ITestResult result, boolean printMethodName) {
    String test_name = result.getTestClass().getName();
    File dir = new File(output_dir + File.separator + test_name);
    if (!dir.exists()) dir.mkdirs();
    File _tests = new File(dir, TESTS),
        _stdout = new File(dir, STDOUT),
        _stderr = new File(dir, STDERR);
    try {
      Class<?> clazz = result.getTestClass().getRealClass();
      if (!tests.containsKey(clazz)) {
        DataOutputStream output = new DataOutputStream(new FileOutputStream(_tests, true));
        DataOutputStream tmp = tests.putIfAbsent(clazz, output);
        if (tmp != null) {
          Util.close(output);
          output = tmp;
        }
      }

      if (stdout.get() == null) stdout.set(new PrintStream(new FileOutputStream(_stdout, true)));
      if (stderr.get() == null) stderr.set(new PrintStream(new FileOutputStream(_stderr, true)));
      if (printMethodName)
        stdout.get().println("\n\n------------- " + getMethodName(result) + " -----------");
    } catch (IOException e) {
      error(e.toString());
    }
  }
 @Override
 public void onTestFailure(ITestResult tr) {
   //    System.out.print("\033[31m");	// set red font color
   System.out.println("F");
   System.out.println(tr.getTestClass().getName() + "." + tr.getName() + " FAILED");
   //   System.out.print("\033[0m");	// reset to normal
 }
  @Override
  public void onTestStart(ITestResult result) {
    logsResults.clear();
    StringBuilder builder = new StringBuilder();
    String testName = result.getName().toString();
    String className = result.getTestClass().getName().toString();

    Method testMethod = result.getMethod().getConstructorOrMethod().getMethod();

    builder.append("<table>" + "<h1>Class: <em>" + className + "." + testName + " </em></h1>");
    if (testMethod.isAnnotationPresent(RelatedIssue.class)) {
      String issueID = testMethod.getAnnotation(RelatedIssue.class).issueID();
      String jiraUrl = jiraPath + issueID;
      builder.append(
          "<tr class=\"step\"><td>Known failure</td><td><h1><em>"
              + testName
              + " - "
              + "<a href=\""
              + jiraUrl
              + "\">"
              + issueID
              + "</a> "
              + testMethod.getAnnotation(RelatedIssue.class).comment()
              + "</em></h1></td><td> <br/> &nbsp;</td></tr>");
    } else {
      builder.append(
          "<tr class=\"step\"><td>&nbsp</td><td><h1><em>"
              + testName
              + "</em></h1></td><td> <br/> &nbsp;</td></tr>");
    }
    CommonUtils.appendTextToFile(logPath, builder.toString());
    System.out.println(className + " " + testName);
  }
示例#4
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));
 }
 @AfterMethod
 public void printError(ITestResult result) {
   if (result.getStatus() == ITestResult.FAILURE) {
     String clsName = result.getTestClass().getName();
     clsName = clsName.substring(clsName.lastIndexOf(".") + 1);
     System.out.println("Test " + clsName + "." + result.getName() + " FAILED");
   }
 }
 private String getMethodContext(ITestResult result) {
   // String browser = result.getTestContext().getCurrentXmlTest()
   // .getParameter("browser");
   String testClasss = result.getTestClass().getName();
   String name = result.getName();
   String rs = testClasss + " | " + name;
   System.out.println("rs--->" + rs);
   return rs;
 }
  @Override
  public void onConfigurationFailure(ITestResult iTestResult) {
    super.onConfigurationFailure(iTestResult);
    String testName = iTestResult.getTestClass().getName();
    String configurationName = iTestResult.getMethod().toString().split("\\(|\\)")[0];
    if (!enableLogstash && isAfter(iTestResult)) {
      DumpUtils.copyBeforeConfigurationsLogToTestDir(testName, suiteName);
      testName = testMethodName;
    }
    if (suiteName
        == null) { // this is in case the suite has a @BeforeSuite method. which is invoked before
                   // the onStart is.
      suiteName = System.getProperty("iTests.suiteName", "sgtest");
    }
    LogUtils.log("Configuration Failed: " + configurationName, iTestResult.getThrowable());

    if (enableLogstash && iTestResult.getMethod().isBeforeClassConfiguration()) {
      initLogstash2(iTestResult);
    }

    String newmanTestFolder = System.getProperty("newman.test.path");
    if (newmanTestFolder != null) {
      File testFolder = new File(newmanTestFolder);
      ZipUtils.unzipArchive(testFolder);
      try {
        copyAllFilesToLogDir(testFolder, testFolder);
      } catch (IOException e) {
        LogUtils.log("Failed to copy all log files");
      }
    } else {
      ZipUtils.unzipArchive(testMethodName, suiteName);
    }

    if (enableLogstash
        && isAfter(iTestResult)
        && !iTestResult.getMethod().isAfterClassConfiguration()
        && !iTestResult.getMethod().isAfterSuiteConfiguration()) {
      testName = testMethodName;
    }
    File testFolder = DumpUtils.createTestFolder(testName, suiteName);
    write2LogFile(iTestResult, testFolder);
    write2ErrorTxtFile(iTestResult, testFolder);

    if (isAfter(iTestResult)) {
      if (enableLogstash) {
        if (process != null) {
          killLogstashAgent(1, logstashLogPath);
        }
        if (process2 != null && iTestResult.getMethod().isAfterClassConfiguration()) {
          killLogstashAgent(2, logstashLogPath2);
        }
      }
    }
  }
 private void logEndOfTest(ITestResult result, String outcome) {
   logger.info(
       String.format(
           "Testing going to end for: %s.%s(%s) %s",
           result.getTestClass().getName(),
           result.getName(),
           Arrays.toString(result.getParameters()),
           outcome));
   NDC.pop();
   logLine();
 }
 @Override
 public void onTestStart(ITestResult result) {
   logLine();
   logger.info(
       String.format(
           "Testing going to start for: %s.%s %s",
           result.getTestClass().getName(),
           result.getName(),
           Arrays.toString(result.getParameters())));
   NDC.push(result.getName());
 }
示例#11
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 onTestStart(ITestResult iTestResult) {
   log.info(
       "Running the test method --- "
           + iTestResult.getTestClass().getName()
           + "."
           + iTestResult.getMethod().getMethodName()
           + " ----");
   try {
     TestNGExtensionExecutor.executeExtensible(
         ExtensionConstants.TEST_LISTENER, ExtensionConstants.TEST_LISTENER_ON_TEST_START, false);
   } catch (Exception e) {
     handleException("Error while running tests", e);
   }
 }
 private void injectIntoField(
     Field matchingField,
     Object testInstance,
     KnowledgeBase knowledgeBaseForMethod,
     ITestResult result) {
   matchingField.setAccessible(true);
   try {
     matchingField.set(testInstance, knowledgeBaseForMethod);
   } catch (IllegalAccessException iae) {
     throw new TestFluxException(
         "Error injecting KnowledgeBase into "
             + result.getTestClass().getName()
             + "."
             + matchingField.getName());
   }
 }
示例#14
0
 @Override
 public void onTestSuccess(ITestResult tr) {
   success++;
   String location =
       tr.getTestClass().getRealClass().getSimpleName() + '#' + tr.getMethod().getMethodName();
   try {
     Method realTestMethod = tr.getMethod().getMethod();
     Test specAssert = realTestMethod.getAnnotation(Test.class);
     if (specAssert != null
         && specAssert.description() != null
         && !specAssert.description().isEmpty()) {
       log("[SUCCESS] " + specAssert.description() + "(" + location + ")");
     } else {
       log("[SUCCESS] " + location);
     }
   } catch (IOException e) {
     throw new IllegalStateException("IO Error", e);
   }
 }
  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));
  }
 private void printTestResults(ITestResult result) {
   Reporter.log("TestName = " + method, true);
   Reporter.log("Test Method resides in " + result.getTestClass().getName(), true);
   if (result.getParameters().length != 0) {
     String params = null;
     for (Object parameter : result.getParameters()) {
       params += parameter.toString() + ",";
     }
     Reporter.log("Test Method had the following parameters : " + params, true);
   }
   String status = null;
   switch (result.getStatus()) {
     case ITestResult.SUCCESS:
       status = "Pass";
       break;
     case ITestResult.FAILURE:
       status = "Failed";
       break;
     case ITestResult.SKIP:
       status = "Skipped";
   }
   Reporter.log("Test Status: " + status, true);
 }
示例#17
0
    @Override
    public void onTestFailure(ITestResult tr) {
      failed++;
      count++;
      String location =
          tr.getTestClass().getRealClass().getSimpleName() + '#' + tr.getMethod().getMethodName();
      try {
        Method realTestMethod = tr.getMethod().getMethod();
        Test testAnnot = realTestMethod.getAnnotation(Test.class);
        if (testAnnot != null
            && testAnnot.description() != null
            && !testAnnot.description().isEmpty()) {
          if (tr.getThrowable() != null) {
            StringWriter sw = new StringWriter();
            PrintWriter w = new PrintWriter(sw);
            tr.getThrowable().printStackTrace(w);
            w.flush();
            log("[FAILED]  " + testAnnot.description() + "(" + location + "):\n" + sw.toString());
          } else {
            log("[FAILED]  " + testAnnot.description() + "(" + location + ")");
          }
        } else {

          if (tr.getThrowable() != null) {
            StringWriter sw = new StringWriter();
            PrintWriter w = new PrintWriter(sw);
            tr.getThrowable().printStackTrace(w);
            w.flush();
            log("[FAILED]  " + location + ":\n" + sw.toString());
          } else {
            log("[FAILED]  " + location);
          }
        }
      } catch (IOException e) {
        throw new IllegalStateException("IO Error", e);
      }
    }
  @Override
  public void onTestFailure(ITestResult result) {
    logEndOfTest(result, "FAILED");
    if (BaseUITestClass.getDRIVER() != null) {
      byte[] scrFile =
          ((TakesScreenshot) BaseUITestClass.getDRIVER()).getScreenshotAs(OutputType.BYTES);
      try {
        String filename =
            OSUtil.getPath(
                "target",
                "surefire-reports",
                "screenshots",
                String.format(
                    "%s.%s.png",
                    result.getTestClass().getRealClass().getSimpleName(), result.getName()));
        FileUtils.writeByteArrayToFile(new File(filename), scrFile);
      } catch (IOException e) {
        logger.info("Saving screenshot FAILED: " + e.getCause());
      }
    }

    logger.info(ExceptionUtils.getStackTrace(result.getThrowable()));
    logLine();
  }
示例#19
0
 protected void onTestCompleted(ITestResult tr, String message, PrintStream out) {
   Class<?> real_class = tr.getTestClass().getRealClass();
   addTest(real_class, tr);
   print(out, message, real_class.getName(), getMethodName(tr));
   closeStreams();
 }
 private String getTestDesc(ITestResult res) {
   return res.getMethod().getMethodName() + "(" + res.getTestClass().getName() + ")";
 }
  private void initLogstash2(ITestResult tr) {

    initLogstashHost();

    String simpleClassName = tr.getTestClass().getRealClass().getSimpleName();
    String pathToLogstash =
        SGTestHelper.getSGTestRootDir().replace("\\", "/") + "/src/main/resources/logstash";
    String confFilePath2 = pathToLogstash + "/logstash-shipper-client-2.conf";
    String backupFilePath2 =
        pathToLogstash + "/logstash-shipper-client-2-" + simpleClassName + ".conf";
    File backupFile2 = new File(backupFilePath2);

    LogUtils.log(
        "trying to start logstash agent number 2. simple class name is " + simpleClassName);
    if (backupFile2.exists()) {
      LogUtils.log("the file " + backupFilePath2 + " already exists. not starting logstash");
    }

    if (!isAfter(tr) && !backupFile2.exists()) {

      try {
        //                backupFilePath2 = IOUtils.backupFile(confFilePath2);
        LogUtils.log("copying file " + confFilePath2 + " to " + backupFilePath2);
        IOUtils.copyFile(confFilePath2, backupFilePath2);
        IOUtils.replaceTextInFile(backupFilePath2, "<path_to_build>", SGTestHelper.getBuildDir());
        IOUtils.replaceTextInFile(
            backupFilePath2,
            "<suite_number>",
            "suite_" + System.getProperty("iTests.suiteId", "0"));
        IOUtils.replaceTextInFile(
            backupFilePath2,
            "<path_to_test_class_folder>",
            SGTestHelper.getSGTestRootDir().replace("\\", "/")
                + "/../"
                + suiteName
                + "/"
                + tr.getTestClass().getName());
        IOUtils.replaceTextInFile(backupFilePath2, "<suite_name>", suiteName);
        IOUtils.replaceTextInFile(backupFilePath2, "<test_name>", simpleClassName);
        IOUtils.replaceTextInFile(backupFilePath2, "<build_number>", buildNumber);
        IOUtils.replaceTextInFile(backupFilePath2, "<version>", version);
        IOUtils.replaceTextInFile(backupFilePath2, "<host>", logstashHost);

        String logstashJarPath =
            DeploymentUtils.getLocalRepository() + "net/logstash/1.2.2/logstash-1.2.2.jar";
        logstashLogPath2 = pathToLogstash + "/logstash-" + simpleClassName + "-2.txt";
        String cmdLine =
            "java -jar "
                + logstashJarPath
                + " agent -f "
                + backupFilePath2
                + " -l "
                + logstashLogPath2;

        final String[] parts = cmdLine.split(" ");
        final ProcessBuilder pb = new ProcessBuilder(parts);
        LogUtils.log("Executing Command line: " + cmdLine);

        process2 = pb.start();

      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
 // returns an ID for each test result
 private int getTestId(ITestResult result) {
   int id = result.getTestClass().getName().hashCode();
   id = 31 * id + result.getMethod().getMethodName().hashCode();
   id = 31 * id + (result.getParameters() != null ? Arrays.hashCode(result.getParameters()) : 0);
   return id;
 }
 public void onTestStart(ITestResult res) {
   log.info("Starting test " + getTestDesc(res));
   addOomLoggingSupport();
   threadTestClass.set(res.getTestClass());
 }
 private Class<?> obtainJavaTestClass(ITestResult result) {
   return result.getTestClass().getRealClass();
 }
 private ClassInfo getClassInfo(ITestResult result) {
   return info.get(result.getTestClass().getName());
 }
示例#26
0
 /**
  * Get last containing package name from ITestResult
  *
  * @param result from the fine-grained listener's method such as onTestFailure(ITestResult)
  * @return the package name in current context
  */
 public static String getContainingPackageName(ITestResult result) {
   Package dPackage = result.getTestClass().getRealClass().getPackage();
   return dPackage.getName().replaceFirst("^.*\\.", "");
 }
示例#27
0
 /**
  * Get class name from ITestResult
  *
  * @param result from the fine-grained listener's method such as onTestFailure(ITestResult)
  * @return the class name in current context
  */
 public static String getClassName(ITestResult result) {
   Class<?> dClass = result.getTestClass().getRealClass();
   return dClass.getSimpleName();
 }