private static void doTest(XmlSuite suite, String expected) {
    final StringBuffer buf = new StringBuffer();
    final IDEATestNGRemoteListener listener = createListener(buf);

    for (XmlTest test : suite.getTests()) {
      for (XmlClass aClass : test.getClasses()) {
        final String classFQName = aClass.getName();
        for (XmlInclude include : aClass.getIncludedMethods()) {
          final String methodName = include.getName();
          List<Integer> numbers = include.getInvocationNumbers();
          if (numbers.isEmpty()) {
            numbers = Collections.singletonList(0);
          }
          for (Integer integer : numbers) {
            final MockTestNGResult result =
                new MockTestNGResult(classFQName, methodName, null, new Object[] {integer});
            listener.onTestStart(result);
            listener.onTestFinished(result);
          }
        }
      }
    }

    Assert.assertEquals(
        "output: " + buf, expected, StringUtil.convertLineSeparators(buf.toString()));
  }
  @Test
  public void testOneTestMethodWithMultipleInvocationCount() throws Exception {
    final XmlSuite suite = new XmlSuite();
    final XmlTest test = new XmlTest();
    final XmlClass xmlClass = new XmlClass("a.ATest", false);
    xmlClass.getIncludedMethods().add(new XmlInclude("test1", Arrays.asList(0, 1, 2), 0));
    test.getClasses().add(xmlClass);
    suite.getTests().add(test);

    doTest(
        suite,
        "##teamcity[testCount count='1']\n"
            + "\n"
            + "##teamcity[testSuiteStarted name ='ATest' locationHint = 'java:suite://a.ATest']\n"
            + "\n"
            + "##teamcity[testStarted name='ATest.test1|[0|]' locationHint='java:test://a.ATest.test1|[0|]']\n"
            + "\n"
            + "##teamcity[testFinished name='ATest.test1|[0|]']\n"
            + "##teamcity[testCount count='1']\n"
            + "\n"
            + "##teamcity[testStarted name='ATest.test1|[1|] (1)' locationHint='java:test://a.ATest.test1|[1|]']\n"
            + "\n"
            + "##teamcity[testFinished name='ATest.test1|[1|] (1)']\n"
            + "##teamcity[testCount count='1']\n"
            + "\n"
            + "##teamcity[testStarted name='ATest.test1|[2|] (2)' locationHint='java:test://a.ATest.test1|[2|]']\n"
            + "\n"
            + "##teamcity[testFinished name='ATest.test1|[2|] (2)']\n");
  }
示例#3
0
  /**
   * @param methods The methods we want to represent
   * @param srcXmlTest
   * @return A list of XmlClass objects (each representing a <class> tag) based on the parameter
   *     methods
   */
  private List<XmlClass> createXmlClasses(List<ITestNGMethod> methods, XmlTest srcXmlTest) {
    List<XmlClass> result = Lists.newArrayList();
    Map<Class, Set<ITestNGMethod>> methodsMap = Maps.newHashMap();

    for (ITestNGMethod m : methods) {
      Object[] instances = m.getInstances();
      Class clazz =
          instances == null || instances.length == 0 || instances[0] == null
              ? m.getRealClass()
              : instances[0].getClass();
      Set<ITestNGMethod> methodList = methodsMap.get(clazz);
      if (null == methodList) {
        methodList = new HashSet<ITestNGMethod>();
        methodsMap.put(clazz, methodList);
      }
      methodList.add(m);
    }

    // Ideally, we should preserve each parameter in each class but putting them
    // all in the same bag for now
    Map<String, String> parameters = Maps.newHashMap();
    for (XmlClass c : srcXmlTest.getClasses()) {
      parameters.putAll(c.getLocalParameters());
    }

    int index = 0;
    for (Map.Entry<Class, Set<ITestNGMethod>> entry : methodsMap.entrySet()) {
      Class clazz = entry.getKey();
      Set<ITestNGMethod> methodList = entry.getValue();
      // @author Borojevic
      // Need to check all the methods, not just @Test ones.
      XmlClass xmlClass = new XmlClass(clazz.getName(), index++, false /* don't load classes */);
      List<XmlInclude> methodNames = Lists.newArrayList(methodList.size());
      int ind = 0;
      for (ITestNGMethod m : methodList) {
        methodNames.add(
            new XmlInclude(m.getMethod().getName(), m.getFailedInvocationNumbers(), ind++));
      }
      xmlClass.setIncludedMethods(methodNames);
      xmlClass.setParameters(parameters);
      result.add(xmlClass);
    }

    return result;
  }
示例#4
0
  public static XmlSuite parse(String filePath, InputStream is) throws FileNotFoundException {
    Constructor constructor = new TestNGConstructor(XmlSuite.class);
    {
      TypeDescription suiteDescription = new TypeDescription(XmlSuite.class);
      suiteDescription.putListPropertyType("packages", XmlPackage.class);
      suiteDescription.putListPropertyType("listeners", String.class);
      suiteDescription.putListPropertyType("tests", XmlTest.class);
      suiteDescription.putListPropertyType("method-selectors", XmlMethodSelector.class);
      constructor.addTypeDescription(suiteDescription);
    }

    {
      TypeDescription testDescription = new TypeDescription(XmlTest.class);
      testDescription.putListPropertyType("classes", XmlClass.class);
      testDescription.putMapPropertyType("metaGroups", String.class, List.class);
      testDescription.putListPropertyType("method-selectors", XmlMethodSelector.class);
      constructor.addTypeDescription(testDescription);
    }

    org.yaml.snakeyaml.Yaml y = new org.yaml.snakeyaml.Yaml(constructor);
    if (is == null) is = new FileInputStream(new File(filePath));
    XmlSuite result = (XmlSuite) y.load(is);

    result.setFileName(filePath);
    // DEBUG
    //    System.out.println("[Yaml] " + result.toXml());

    // Adjust XmlTest parents and indices
    for (XmlTest t : result.getTests()) {
      t.setSuite(result);
      int index = 0;
      for (XmlClass c : t.getClasses()) {
        c.setIndex(index++);
      }
    }

    return result;
  }