我目前正在使用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中运行Test Suite时的输出:
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
运行Test Suite时的输出[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
关于可能发生的事情的任何想法,在这里?提前谢谢!
答案 0 :(得分:0)
原来我的项目中有一个包含旧版本类文件的jar。我将以下几行添加到我的测试类中以找到它:
Class cls = Class.forName("com.package.KeywordTools");
System.out.println("Location: " + cls.getResource("KeywordTools.class"));
我从jar中删除了类文件,问题解决了。