C++ 无法让 hudson 解析 JUnit 测试输出 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/428553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to get hudson to parse JUnit test output XML
提问by Nik Reiman
EDIT: This issue has been fixed by google in gtest 1.4.0; see the original bug reportfor more information.
编辑:此问题已由 google 在 gtest 1.4.0 中修复;有关更多信息,请参阅原始错误报告。
I've recently switched to gtest for my C++ testing framework, and one great feature of it which I am presently unable to use is the ability to generate JUnit-style XML test reports, which could then be read in by our hudson build server.
我最近为我的 C++ 测试框架切换到 gtest,我目前无法使用它的一个重要功能是能够生成 JUnit 样式的 XML 测试报告,然后可以由我们的 hudson 构建服务器读取。
The XML output generated by the gtest test suite all looks legit:
gtest 测试套件生成的 XML 输出看起来都是合法的:
<?xml version="1.0" encoding="UTF-8"?>
<testsuite tests="370" failures="0" disabled="0" errors="0" time="45.61" name="AllTests">
<testsuite name="application" tests="7" failures="0" disabled="0" errors="0" time="8.953">
<testcase name="zero_tasks_on_bootup" status="run" time="0" classname="application" />
...etc.
</testsuite>
</testsuite>
I've also tried adding a JUnitReport task to my ant build script, which works fine, and generates XML like so:
我还尝试将 JUnitReport 任务添加到我的 ant 构建脚本中,它工作正常,并像这样生成 XML:
<?xml version="1.0" encoding="UTF-8"?>
<testsuite tests="370" failures="0" disabled="0" errors="0" time="45.61" name="AllTests">
<testsuite name="application" tests="7" failures="0" disabled="0" errors="0" time="8.953">
<testcase name="zero_tasks_on_bootup" status="run" time="0" classname="application" />
...etc.
</testsuite>
</testsuite>
The problem is, whenever I tell ant to publish the JUnit test results, and then point it to either the raw test result XML, or the compiled result generated in the ant JUnitReport task, hudson always complains about finding no test results there.
问题是,每当我告诉 ant 发布 JUnit 测试结果,然后将其指向原始测试结果 XML 或在 ant JUnitReport 任务中生成的编译结果时,hudson 总是抱怨在那里找不到测试结果。
I'm not a java guy, so I can't tell what's going on here, and I can't find an example of how the JUnit XML ought to look like. Can someone help to point me in the right direction?
我不是 Java 专家,所以我不知道这里发生了什么,而且我找不到 JUnit XML 应该是什么样子的示例。有人可以帮助我指出正确的方向吗?
采纳答案by Nik Reiman
Edit: Google test has fixed this issue, which is included in the gtest 1.4.0 release. See the original bug reportfor more info.
编辑:Google 测试已修复此问题,该问题已包含在 gtest 1.4.0 版本中。 有关更多信息,请参阅原始错误报告。
Bah! I've finally found the cause of this problem -- it's because gtest produces one giant XML file for all test results, and hudson expects one XML test report per class. I've written a perl script as a workaround for this issue. To use it, you would make a target in your ant xml script which looks something like this:
呸! 我终于找到了这个问题的原因——这是因为 gtest 为所有测试结果生成了一个巨大的 XML 文件,而 hudson 期望每个类有一个 XML 测试报告。我已经编写了一个 perl 脚本来解决这个问题。要使用它,您需要在 ant xml 脚本中创建一个目标,如下所示:
<target name="runtests">
<exec executable="wherever/${ant.project.name}Test" failonerror="false" dir="tests">
<arg value="--gtest_output=xml:${build.dir}\reports${ant.project.name}.xml"/>
</exec>
<!-- Workaround for broken gtest output -->
<mkdir dir="${build.dir}/reports/output"/>
<exec executable="perl" failonerror="false" dir="tests">
<arg value="gtest-hudson.pl"/>
<arg value="${build.dir}/reports/${ant.project.name}.xml"/>
<arg value="${build.dir}/reports/output"/>
</exec>
</target>
For some reason, gtest also doesn't like the wrong style of slashes being passed to it from ant, so I made my exec for windows only, as my hudson is running on a windows server. Change to '/' for unix, obviously.
出于某种原因,gtest 也不喜欢从 ant 传递给它的错误样式的斜杠,所以我只为 windows 制作了 exec,因为我的 hudson 在 windows 服务器上运行。显然,对于unix,更改为'/'。
I've also filed an issue for this on the gtest page, and also one on hudson's issue tracker, so hopefully one of the two teams will pick up on the issue, as I don't have enough time to jump in and make a patch myself.... though if this doesn't get fixed in the near future, I might just have to. ;)
我还在gtest 页面上为此提交了一个问题,也在哈德森的问题跟踪器上提交了一个问题,所以希望两个团队中的一个能够解决这个问题,因为我没有足够的时间介入并做出自己打补丁……不过如果这在不久的将来没有得到解决,我可能只需要这样做。;)
回答by duffymo
Here's how I do it:
这是我的方法:
<target name="junit" depends="compile-tests" description="run all unit tests">
<mkdir dir="${reports}"/>
<junit haltonfailure="false">
<jvmarg value="-Xms128m"/>
<jvmarg value="-Xmx128m"/>
<classpath>
<path refid="project.classpath"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${reports}">
<fileset dir="${test}/classes">
<include name="**/*Test*.class"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="generate-reports" depends="junit" description="create JUnit test HTML reports">
<mkdir dir="${reports}"/>
<junitreport todir="${reports}">
<fileset dir="${reports}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${reports}"/>
</junitreport>
</target>
回答by Dan Dyer
I'm almost certain that this is not a problem parsing the XML but rather a problem finding the XML files. If you are using a relative path in the Hudson config, make sure you are clear which directory it is relative to (I seem to remember it being non-obvious under certain circumstances).
我几乎可以肯定,这不是解析 XML 的问题,而是查找 XML 文件的问题。如果您在 Hudson 配置中使用相对路径,请确保您清楚它相对于哪个目录(我似乎记得在某些情况下它并不明显)。
As for examples of what the JUnit XML files are supposed to look like, good luck with that. It's not precisely specified anywhere. Different tools have differing dialects. That said, Hudson does a good job of recognising all of them. I believe it was the developers of JUnitReport who first introduced the XML format, so if you're using that, that's about as canonical as you are going to get.
至于 JUnit XML 文件应该是什么样子的示例,祝你好运。它没有在任何地方精确指定。不同的工具有不同的方言。也就是说,哈德森在识别所有这些方面做得很好。我相信是 JUnitReport 的开发人员首先引入了 XML 格式,所以如果您正在使用它,那将与您将获得的一样规范。