当前位置: 首页 > 知识库问答 >
问题:

JUnit套件在从构建运行时会产生不同的输出。xml

薛修能
2023-03-14

我目前正在使用Ant从JUnit测试套件生成html报告,但遇到了一些非常奇怪的问题。当我在Eclipse中运行我的测试套件时,我获得了30次成功、0次错误和0次失败。当我将测试套件作为Ant目标运行时(同样,在Eclipse中),我获得了7次成功、10次错误和13次失败,以及不同的输出。为了回答这个问题,我将只关注一个测试用例。

Ant目标:

  <target name="run-unit-test" depends="build-unit-test" description="Run all unit tests.">
    <delete failonerror="false" dir="${unittest.dir}"/>
    <mkdir dir="${unittest.dir}"/>
    <junit printsummary="true" haltonfailure="false" fork="true" forkmode="once" showoutput="true">
      <env key="BATON_HOME" value="/home/natshiel/git"/>
      <classpath>
        <path refid="libs"/>
        <path refid="junit-libs"/>      
        <pathelement location="${sep.classes.dir}"/>
        <pathelement location="${classes.dir}"/>
      </classpath>
      <formatter type="xml" usefile="true"/>
      <batchtest fork="yes" todir="${unittest.dir}">
        <fileset dir="${basedir}/build/classes">
          <!--include name="**/*Test.class"/> -->
            <include name="**/KeywordToolsTest.class"/>
          <!-- Exclude non-junit tests -->
          <exclude name="**/nbi/test/*"/>
          <exclude name="**/configmgmt/test/*"/>
          <exclude name="**/valuecollection/**"/>
        </fileset>
      </batchtest>
    </junit>
  </target>

有问题的JUnit测试:

@Test
public void testExtractKeywordsInclInvalid() {
    String fullString = "${CP_IM} ${b} ${1} ${} ${LAST-NAM[E]} ${IP}i ${12} ${34}${56} ${[BRACKETS]}";
    fullString += " hello test b ${hg ${$} ${hello word} ${12-34_ab%}";

    Set<String> validExpected = new HashSet<String>();
    Set<String> invalidExpected = new HashSet<String>();


    validExpected.addAll(Arrays.asList("", "1", "b", "IP", "34", "56", "LAST-NAM[E]", "12", "CP_IM", "[BRACKETS]"));
    invalidExpected.addAll(Arrays.asList("hg ${$", "hello word", "12-34_ab%"));

    Map<String,Set<String>> keywordMap = KeywordTools.extractKeywordsInclInvalid(fullString);

    Set<String> validReturned = keywordMap.get(KeywordTools.VALID);
    Set<String> invalidReturned = keywordMap.get(KeywordTools.INVALID);
    System.out.println("String: " + fullString);
    System.out.println("InclInvalid expected: " + validExpected.toString());
    System.out.println("InclInvalid returned: " + validReturned.toString());
    assertTrue(validExpected.equals(validReturned));
    assertTrue(invalidExpected.equals(invalidReturned));
}

测试中的分类方法:

public static Map<String,Set<String>> extractKeywordsInclInvalid(String input) {
    Set<String> validKW = new HashSet<String>();
    Set<String> invalidKW = new HashSet<String>();

    Map<String,Set<String>> results = new HashMap<String,Set<String>>();
    results.put(VALID, validKW);
    results.put(INVALID, invalidKW);

    Pattern kwAllPattern = Pattern.compile("\\$\\{(.*?)\\}");

    Matcher matcher = kwAllPattern.matcher(input);
    while (matcher.find()) {
        String kw = matcher.group(1);
        if (Pattern.matches("[\\w-_\\[\\]]*", kw) {
            validKW.add(kw);
        } else {
            invalidKW.add(kw);
        }
    }

    return results;
}

在Eclipse中运行测试套件时的输出:

String: ${CP_IM} ${b} ${1} ${} ${LAST-NAM[E]} ${IP}i ${12} ${34}${56}${[BRACKETS]} hello test b ${hg ${$} ${hello word} ${12-34_ab%}
InclInvalid expected: [, 1, b, 56, [BRACKETS], IP, LAST-NAM[E], 34, CP_IM, 12]
InclInvalid returned: [, 1, b, 56, [BRACKETS], IP, LAST-NAM[E], 34, 12, CP_IM]

从生成运行测试套件时的输出。xml

[junit] String: ${CP_IM} ${b} ${1} ${} ${LAST-NAM[E]} ${IP}i ${12} ${34}${56} ${[BRACKETS]} hello test b ${hg ${$} ${hello word} ${12-34_ab%}
[junit] InclInvalid expected: [, 1, b, 56, [BRACKETS], IP, LAST-NAM[E], 34, CP_IM, 12]
[junit] InclInvalid returned: [, 1, b, 56, IP, 34, 12, CP_IM]

如您所见,在使用构建运行测试套件时。xml中,包含方括号的字符串处理不正确。我一辈子都搞不明白为什么会发生这种事。

在使用Eclipse的调试器检查代码时,看起来Regex在Ant运行时以不同的方式工作。我不知道如何解决这个问题。

系统信息:

CentOS 6.6

Eclipse 3.6.1

JRE 1.6.0-openjdk。x86\u 64

Apache蚂蚁1.9.6

这里有什么想法吗?提前感谢!

共有1个答案

严俊彦
2023-03-14

原来我的项目中有一个jar,其中包含一个旧版本的类文件。为了找到它,我在测试类中添加了以下行:

Class cls = Class.forName("com.package.KeywordTools");
System.out.println("Location: " + cls.getResource("KeywordTools.class"));

我从jar中删除了类文件,我的问题解决了。

 类似资料: