当 运行 来自 build.xml 时,JUnit Suite 会产生不同的输出
JUnit Suite produces different output when run from a build.xml
我目前正致力于使用 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} ${} ${LAST-NAM[E]} ${IP}i ${[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));
}
Class 待测方法:
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 中 运行ning 测试套件时的输出:
String: ${CP_IM} ${b} ${} ${LAST-NAM[E]} ${IP}i ${[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]
运行ning 测试套件来自 build.xml
时的输出
[junit] String: ${CP_IM} ${b} ${} ${LAST-NAM[E]} ${IP}i ${[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
日食 3.6.1
JRE 1.6.0-openjdk.x86_64
Apache 蚂蚁 1.9.6
关于这里可能发生的事情有什么想法吗?提前致谢!
原来我的项目中隐藏了一个 jar,其中包含 class 文件的旧版本。我在测试 class 中添加了以下几行以找到它:
Class cls = Class.forName("com.package.KeywordTools");
System.out.println("Location: " + cls.getResource("KeywordTools.class"));
我从jar中删除了class文件,我的问题就解决了。
我目前正致力于使用 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} ${} ${LAST-NAM[E]} ${IP}i ${[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));
}
Class 待测方法:
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 中 运行ning 测试套件时的输出:
String: ${CP_IM} ${b} ${} ${LAST-NAM[E]} ${IP}i ${[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]
运行ning 测试套件来自 build.xml
时的输出[junit] String: ${CP_IM} ${b} ${} ${LAST-NAM[E]} ${IP}i ${[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
日食 3.6.1
JRE 1.6.0-openjdk.x86_64
Apache 蚂蚁 1.9.6
关于这里可能发生的事情有什么想法吗?提前致谢!
原来我的项目中隐藏了一个 jar,其中包含 class 文件的旧版本。我在测试 class 中添加了以下几行以找到它:
Class cls = Class.forName("com.package.KeywordTools");
System.out.println("Location: " + cls.getResource("KeywordTools.class"));
我从jar中删除了class文件,我的问题就解决了。