【发布时间】:2015-12-14 15:25:02
【问题描述】:
我目前正在使用 Ant 从 JUnit 测试套件生成 html 报告,但遇到了一些非常奇怪的问题。当我在 Eclipse 中运行我的测试套件时,我得到了 30 次成功、0 次错误和 0 次失败。当我将测试套件作为 Ant 目标(再次在 Eclipse 中)运行时,我得到 7 次成功、10 次错误和 13 次失败,以及不同的输出。为了这个问题,我将只关注一个测试用例。
蚂蚁目标:
<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]
从 build.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]
如您所见,当使用 build.xml 运行测试套件时,包含方括号的字符串处理不正确。我一辈子都想不通为什么会发生这种情况。
在使用 Eclipse 的调试器检查代码时,看起来 Regex 在由 Ant 运行时以不同的方式工作。我不知道如何解决这个问题。
系统信息:
CentOS 6.6
Eclipse 3.6.1
JRE 1.6.0-openjdk.x86_64
Apache Ant 1.9.6
关于可能发生的事情有什么想法吗?提前致谢!
【问题讨论】:
标签: java eclipse ant junit junit4