为 QUnit + PhantomJS + Jenkins 创建 report.xml 文件时遇到问题

Having issues creating a report.xml file for QUnit + PhantomJS + Jenkins

我一直在尝试让 Jenkins 显示我正在使用 QUnit 测试的示例 js 项目的 JUnit 报告。我确实在互联网上搜索了点点滴滴,到目前为止,Running QUnit tests with Jenkins and Apache Ant? 是我找到的最有用的 post。

我可以确认:

我仍然无法生成 report.xml 以将其提供给 Jenkins。以下是我添加到 build.xml 文件中的目标:

    <target name="qunit" description="runs QUnit tests using PhantomJS">
    <!-- QUnit Javascript Unit Tests -->
    <echo message="Executing QUnit Javascript Unit Tests..."/>
    <apply executable="/usr/local/CI/phantomjs/bin/phantomjs" >
        <arg value="/usr/local/CI/phantomjs-runner/runner.js" />
        <arg line="--qunit /usr/local/CI/jenkins/workspace/PhantomJS/web/js/qunit-1.17.1.js --tests /usr/local/CI/jenkins/workspace/PhantomJS/web/index.html --junit /usr/local/CI/jenkins/workspace/PhantomJS/test-results/report.xml" />
        <fileset dir="${basedir}/web/" includes="/js/prettydate.js" />
        <srcfile/>
    </apply>
    <echo message="Tests complete..."/>
</target>

在 Jenkins 中编译项目得到以下输出:

? PhantomJS/result.xml ? PhantomJS/test-results Using locally configured password for connection to :pserver:user@server:/cvsroot cvs rlog -S -d18 Feb 2015 15:49:54 +0000<18 Feb 2015 15:51:40 +0000 QUnit_Jenkins [PhantomJS] $ /usr/local/CI/ant/bin/ant qunit Buildfile: /usr/local/CI/jenkins/workspace/PhantomJS/build.xml

qunit: [echo] Executing QUnit Javascript Unit Tests... [echo] Tests complete...

BUILD SUCCESSFUL Total time: 0 seconds Recording test results Test reports were found but none of them are new. Did tests run? For example, /usr/local/CI/jenkins/workspace/PhantomJS/test-results/report.xml is 4 hr 18 min old

Build step 'Publish JUnit test result report' changed build result to FAILURE Finished: FAILURE

您可能会注意到,jenkins 找不到更新的 report.xml 文件,因为根本没有生成文件。

你能看出我的build.xml有什么错误吗?如果没有,有什么想法和提示可以帮助我生成 result.xml 文件吗?

我通过执行以下步骤找到了我的答案的解决方案:

1) 添加了 <script src="js/qunit-reporter-junit.js"></script>,因为生成报告需要它。确保您还包含 qunit.js 库。我用了 qunit-1.17.1.js

2) 我将以下代码放在测试我的 js 代码的 html 文件中:

<script>
QUnit.jUnitReport = function(report) {
   console.log(report.xml)
      };
</script>

3) 我在 build.xml 文件中添加了 Ant 代码:

    <target name="build" description="runs QUnit tests using PhantomJS">
        <!-- Clean up output directory -->
        <delete dir="./build/qunit"/>
        <mkdir dir="./build/qunit"/>
        <!-- QUnit Javascript Unit Tests -->
        <echo message="Executing QUnit Javascript Unit Tests..."/>
        <exec executable="/usr/local/CI/phantomjs/bin/phantomjs" output="./build/qunit/qunit-results.xml">
            <arg value="/usr/local/CI/phantomjs-runner/runner-muted.js"/>
            <arg value="./web/index.html"/>
        </exec>
    </target>

你会发现我将 runner.js 的名称更改为 运行ner-muted.js 是这样,因为我已将 runner.js 更改为不包括它的输出到 xml 文件,因为这使得它无法被詹金斯读取。为此:

  1. cp /usr/local/CI/phantomjs-runner/runner.js /usr/local/CI/phantomjs-runner/runner-muted.js
  2. 查找并注释掉在 QUnit.done 和 QUnit.testDone 下发现的 console.log 项,以使 运行ner 停止显示其自己的测试 运行 结果。
  3. 确保您在 Jenkins 中选择了生成的 xml 文件的正确路径。

我希望这对任何试图让它工作的人有所帮助