Exemple #1
0
  /**
   * The main entry point to convert an XmlSuite into YAML. This method is allowed to be used by
   * external tools (e.g. Eclipse).
   */
  public static StringBuilder toYaml(XmlSuite suite) {
    StringBuilder result = new StringBuilder();

    maybeAdd(result, "name", suite.getName(), null);
    maybeAdd(result, "junit", suite.isJUnit(), XmlSuite.DEFAULT_JUNIT);
    maybeAdd(result, "verbose", suite.getVerbose(), XmlSuite.DEFAULT_VERBOSE);
    maybeAdd(result, "threadCount", suite.getThreadCount(), XmlSuite.DEFAULT_THREAD_COUNT);
    maybeAdd(
        result,
        "dataProviderThreadCount",
        suite.getDataProviderThreadCount(),
        XmlSuite.DEFAULT_DATA_PROVIDER_THREAD_COUNT);
    maybeAdd(result, "timeOut", suite.getTimeOut(), null);
    maybeAdd(result, "parallel", suite.getParallel(), XmlSuite.DEFAULT_PARALLEL);
    maybeAdd(
        result,
        "skipFailedInvocationCounts",
        suite.skipFailedInvocationCounts(),
        XmlSuite.DEFAULT_SKIP_FAILED_INVOCATION_COUNTS);

    toYaml(result, "parameters", "", suite.getParameters());
    toYaml(result, suite.getPackages());

    if (suite.getListeners().size() > 0) {
      result.append("listeners:\n");
      toYaml(result, "  ", suite.getListeners());
    }

    if (suite.getPackages().size() > 0) {
      result.append("packages:\n");
      toYaml(result, suite.getPackages());
    }
    if (suite.getTests().size() > 0) {
      result.append("tests:\n");
      for (XmlTest t : suite.getTests()) {
        toYaml(result, "  ", t);
      }
    }

    if (suite.getChildSuites().size() > 0) {
      result.append("suite-files:\n");
      toYaml(result, "  ", suite.getSuiteFiles());
    }

    return result;
  }
  /** Fill the top of the XML suiteBuffer with the top of the XML template file */
  private void createXmlFileFromTemplate(XMLStringBuffer suiteBuffer, String fileName) {
    try {
      Parser parser = new Parser(fileName);
      parser.setLoadClasses(false); // we don't want to load the classes from the template file
      Collection<XmlSuite> suites = parser.parse();
      if (suites.size() > 0) {
        XmlSuite s = suites.iterator().next();

        // Retrieve the <suite> attributes from the template file and transfer
        // them in the suite we are creating.
        Properties attr = new Properties();
        put(attr, "name", s.getName());
        put(attr, "junit", s.isJUnit());
        put(attr, "verbose", s.getVerbose());
        put(attr, "parallel", s.getParallel());
        put(attr, "thread-count", s.getThreadCount());
        //        put(attr, "annotations", s.getAnnotations());
        put(attr, "time-out", s.getTimeOut());
        put(attr, "skipfailedinvocationcounts", s.skipFailedInvocationCounts());
        put(attr, "configfailurepolicy", s.getConfigFailurePolicy());
        put(attr, "data-provider-thread-count", s.getDataProviderThreadCount());
        put(attr, "object-factory", s.getObjectFactory());
        put(attr, "allow-return-values", s.getAllowReturnValues());
        put(attr, "preserve-order", s.getPreserveOrder());
        put(attr, "group-by-instances", s.getGroupByInstances());
        suiteBuffer.push("suite", attr);

        // Children of <suite>

        // Listeners
        if (s.getListeners().size() > 0) {
          suiteBuffer.push("listeners");
          for (String l : s.getListeners()) {
            Properties p = new Properties();
            p.put("class-name", l);
            suiteBuffer.addEmptyElement("listener", p);
          }
          suiteBuffer.pop("listeners");
        }

        // Parameters
        for (Map.Entry<String, String> parameter : s.getParameters().entrySet()) {
          Properties p = new Properties();
          p.put("name", parameter.getKey());
          p.put("value", parameter.getValue());
          suiteBuffer.addEmptyElement("parameter", p);
        }

        // Method selectors
        if (s.getMethodSelectors().size() > 0) {
          suiteBuffer.push("method-selectors");
          for (XmlMethodSelector ms : s.getMethodSelectors()) {
            String cls = ms.getClassName();
            if (cls != null && cls.length() > 0) {
              suiteBuffer.push("method-selector");
              Properties p = new Properties();
              p.put("name", cls);
              p.put("priority", ms.getPriority());
              suiteBuffer.addEmptyElement("selector-class", p);
              suiteBuffer.pop("method-selector");
            }
            // TODO: <script> tag of method-selector
          }
          suiteBuffer.pop("method-selectors");
        }
      }
    } catch (IOException e) {
      throw new TestNGException(e);
    }
  }
  /**
   * Runs a suite and its children suites
   *
   * @param result populates this list with execution results
   * @param suiteRunnerMap map of suiteRunners that are updated with test results
   * @param xmlSuite XML suites to run
   */
  private void runSuite(XmlSuite xmlSuite) {
    //    System.out.println("Running suite:" + xmlSuite);
    if (m_verbose > 0) {
      StringBuffer allFiles = new StringBuffer();
      allFiles
          .append("  ")
          .append(xmlSuite.getFileName() != null ? xmlSuite.getFileName() : m_defaultSuiteName)
          .append('\n');
      Utils.log("TestNG", 0, "Running:\n" + allFiles.toString());
    }

    PoolService.initialize(xmlSuite.getDataProviderThreadCount());
    //    for (XmlSuite s : suiteRunnerMap.keySet()) {
    //      System.out.println(s.equals(xmlSuite) + " " + s.hashCode() + " " + xmlSuite.hashCode());
    //    }
    m_suiteRunner.run();
    //    PoolService.getInstance().shutdown();

    //
    // Display the final statistics
    //
    int passed = 0;
    int failed = 0;
    int skipped = 0;
    int confFailures = 0;
    int confSkips = 0;
    int total = 0;
    if (xmlSuite.getVerbose() > 0) {
      //      SuiteResultCounts counts = new SuiteResultCounts();
      //      counts.calculateResultCounts(xmlSuite, suiteRunnerMap);
      m_verboseOutput =
          new StringBuilder("\n===============================================\n")
              .append(xmlSuite.getName());
      for (ISuiteResult isr : m_suiteRunner.getResults().values()) {
        passed += isr.getTestContext().getPassedTests().size();
        failed += isr.getTestContext().getFailedTests().size();
        skipped += isr.getTestContext().getSkippedTests().size();
        confFailures += isr.getTestContext().getFailedConfigurations().size();
        confSkips += isr.getTestContext().getSkippedConfigurations().size();
      }
      total += passed + failed + skipped;

      m_verboseOutput
          .append("\nTotal tests run: ")
          .append(total)
          .append(", Failures: ")
          .append(failed)
          .append(", Skips: ")
          .append(skipped);
      ;
      if (confFailures > 0 || confSkips > 0) {
        m_verboseOutput
            .append("\nConfiguration Failures: ")
            .append(confFailures)
            .append(", Skips: ")
            .append(confSkips);
      }
      m_verboseOutput.append("\n===============================================\n");

      System.out.println(m_verboseOutput);
    }
  }