在Continuous Integration构建中无头运行JavaScript单元测试 [英] Running JavaScript unit tests headlessly in a Continuous Integration build

查看:254
本文介绍了在Continuous Integration构建中无头运行JavaScript单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在持续集成系统上运行的webapp构建计划( Atlassian Bamboo 2.5 )。我需要将 QUnit 基于JavaScript的单元测试合并到构建计划中,以便在每个构建中,Javascript测试将运行,Bamboo将解释测试结果。

I have a webapp build plan running on a Continuous Integration system (Atlassian Bamboo 2.5). I need to incorporate QUnit-based JavaScript unit tests into the build plan so that on each build, the Javascript tests would be run and Bamboo would interpret the test results.

最好是我想能够使构建过程独立,以便没有连接到外部服务器需要。好的想法如何完成这个?运行构建过程的CI系统在Ubuntu Linux服务器上。

Preferably I would like to be able to make the build process "standalone" so that no connections to external servers would be required. Good ideas on how to accomplish this? The CI system running the build process is on an Ubuntu Linux server.

推荐答案

由于我自己设计了一个解决方案,我认为这是一个好主意,分享它。这种方法可能不是完美的,但它是第一个似乎工作。欢迎随时发布改进建议。

As I managed to come up with a solution myself, I thought it would be a good idea to share it. The approach might not be flawless, but it's the first one that seemed to work. Feel free to post improvements and suggestions.

我做了什么:


  • 启动 Xvfb 的虚拟框架缓冲区实例

  • 使用 JsTestDriver


    • 将Firefox的实例启动到虚拟framebuffer(headlessly)

    • 捕获 Firefox 实例并运行测试套件

    • 生成符合JUnit的测试结果.XML

    • Launch an instance of Xvfb, a virtual framebuffer
    • Using JsTestDriver:
      • launch an instance of Firefox into the virtual framebuffer (headlessly)
      • capture the Firefox instance and run the test suite
      • generate JUnit-compliant test results .XML

      下面我将详细介绍这些步骤。这是我的目录结构最终看起来像:

      I will next go through the more detailed phases. This is what my my directory structure ended up looking like:

      
      lib/
          JsTestDriver.jar
      test/
          qunit/
                  equiv.js
                  QUnitAdapter.js
          jsTestDriver.conf
          run_js_tests.sh
          tests.js
      test-reports/
      build.xml
      

      在构建服务器上


      • 安装Xvfb( apt-get install Xvfb

      • 安装Firefox $ c> apt-get install firefox )

      • Install Xvfb (apt-get install Xvfb)
      • Install Firefox (apt-get install firefox)

      / strong>

      Into your application to be built:


      • 安装JsTestDriver: http://code.google.com/p/js-test-driver/


        • 添加QUnit 适配器 equiv.js 和 QUnitAdapter.js

        • configure JsTestDriver( jsTestDriver.conf ):

        • Install JsTestDriver: http://code.google.com/p/js-test-driver/
          • add the QUnit adapters equiv.js and QUnitAdapter.js
          • configure JsTestDriver (jsTestDriver.conf):
          
          server: http://localhost:4224
          
          load:
          # Load QUnit adapters (may be omitted if QUnit is not used)
            - qunit/equiv.js
            - qunit/QUnitAdapter.js   
          
          # Tests themselves (you'll want to add more files)
            - tests.js
          

          创建一个用于运行单元测试和生成测试结果的脚本文件(例如在Bash, run_js_tests.sh ):

          Create a script file for running the unit tests and generating test results (example in Bash, run_js_tests.sh):

          #!/bin/bash
          # directory to write output XML (if this doesn't exist, the results will not be generated!)
          OUTPUT_DIR="../test-reports"
          mkdir $OUTPUT_DIR
          
          XVFB=`which Xvfb`
          if [ "$?" -eq 1 ];
          then
              echo "Xvfb not found."
              exit 1
          fi
          
          FIREFOX=`which firefox`
          if [ "$?" -eq 1 ];
          then
              echo "Firefox not found."
              exit 1
          fi
          
          $XVFB :99 -ac &    # launch virtual framebuffer into the background
          PID_XVFB="$!"      # take the process ID
          export DISPLAY=:99 # set display to use that of the xvfb
          
          # run the tests
          java -jar ../lib/JsTestDriver.jar --config jsTestDriver.conf --port 4224 --browser $FIREFOX --tests all --testOutput $OUTPUT_DIR
          
          kill $PID_XVFB     # shut down xvfb (firefox will shut down cleanly by JsTestDriver)
          echo "Done."
          

          创建调用脚本的Ant目标:

          Create an Ant target that calls the script:

          <target name="test">        
              <exec executable="cmd" osfamily="windows">
                  <!-- This might contain something different in a Windows environment -->
              </exec>
          
              <exec executable="/bin/bash" dir="test" osfamily="unix">
                  <arg value="run_js_tests.sh" />
              </exec>
          </target>   
          

          最后,告诉Bamboo构建计划,调用 test target并查找JUnit测试结果。这里默认的** / test-reports / *。xml会很好。

          Finally, tell the Bamboo build plan to both invoke the test target and look for JUnit test results. Here the default "**/test-reports/*.xml" will do fine.

          这篇关于在Continuous Integration构建中无头运行JavaScript单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆