Unable to get hudson to parse JUnit test output XML

你离开我真会死。 提交于 2019-11-29 00:28:18

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>

Edit: Google test has fixed this issue, which is included in the gtest 1.4.0 release. See the original bug report for more info.

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:

<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.

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. ;)

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).

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!