简体   繁体   English

在持续集成构建中无头地运行JavaScript单元测试

[英]Running JavaScript unit tests headlessly in a Continuous Integration build

I have a webapp build plan running on a Continuous Integration system ( Atlassian Bamboo 2.5). 我有一个在持续集成系统( Atlassian Bamboo 2.5)上运行的webapp构建计划。 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. 我需要将基于QUnit的JavaScript单元测试合并到构建计划中,以便在每个构建时运行Javascript测试,Bamboo将解释测试结果。

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. 运行构建过程的CI系统位于Ubuntu Linux服务器上。

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. 随意发布改进和建议。

What I did in a nutshell: 我简单地说了一下:

  • Launch an instance of Xvfb , a virtual framebuffer 启动Xvfb的实例,一个虚拟帧缓冲区
  • Using JsTestDriver : 使用JsTestDriver
    • launch an instance of Firefox into the virtual framebuffer (headlessly) 将Firefox实例启动到虚拟帧缓冲区(无头)
    • capture the Firefox instance and run the test suite 捕获Firefox实例并运行测试套件
    • generate JUnit-compliant test results .XML 生成符合JUnit的测试结果.XML
  • Use Bamboo to inspect the results file to pass or fail the build 使用Bamboo检查结果文件以使构建通过或失败

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

On the build server: 在构建服务器上:

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

Into your application to be built: 进入你要建立的应用程序:

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

Create a script file for running the unit tests and generating test results (example in Bash, run_js_tests.sh ): 创建一个脚本文件来运行单元测试并生成测试结果(例如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."

Create an Ant target that calls the script: 创建一个调用脚本的Ant目标:

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

Finally, tell the Bamboo build plan to both invoke the test target and look for JUnit test results. 最后,告诉Bamboo构建计划同时调用test目标并查找JUnit测试结果。 Here the default "**/test-reports/*.xml" will do fine. 这里默认的"**/test-reports/*.xml"会很好。

For anyone interested in running their Jasmine BDD specs headlessly in maven, you might be interested in the jasmine-maven-plugin I maintain: 对于有兴趣在maven中无头地运行Jasmine BDD规范的人,您可能会对我维护的jasmine-maven-plugin感兴趣:

http://github.com/searls/jasmine-maven-plugin http://github.com/searls/jasmine-maven-plugin

I've played around with a number of solutions over the past year but I didn't find anything in the ballpark of Karma (formerly testacular). 在过去的一年里,我玩过很多解决方案,但我没有在Karma(以前的测试版)的球场找到任何东西。 Give it a try 试试看

http://karma-runner.github.com/ http://karma-runner.github.com/

As an alternative, you could also try TestSwarm. 作为替代方案,您也可以尝试TestSwarm。 I've got it up and running using QUnit to run my JS tests. 我已经使用QUnit运行我的JS测试了。

JS Test Runner is a pretty good solution. JS Test Runner是一个非常好的解决方案。 It uses PhantomJS and QUnit. 它使用PhantomJS和QUnit。

You may be able to use rhino, the headless browser, to run your unit tests on your CI machine. 您可以使用无头浏览器rhino在CI计算机上运行单元测试。 Of course, the disadvantage here is that it won't find bugs specific to browser X... but it does beat installing 2-3 OSes on your CI box, to cover all the main platforms... 当然,这里的缺点是它不会发现特定于浏览器X的错误...但它确实在你的CI盒上安装2-3个操作系统,覆盖所有主要平台......

But yes, this kind of sucks... but it might work just well enough in a CI scenario. 但是,是的,这种糟糕的......但它可能在CI场景中运作得很好。

I have used maven and junit to call rhino. 我用maven和junit打电话给犀牛。 It is not elegant, but I use it to test basic services and utility code. 它不优雅,但我用它来测试基本服务和实用程序代码。

It requires mocking unsupported classes, like XHR with Java libraries. 它需要模拟不受支持的类,例如XHR和Java库。

I found that it is best code everything in javascript (tests, etc) and only use junit for build organization and a hook into the CI. 我发现它最好用javascript(测试等)编写所有内容,并且只使用junit进行构建组织和挂钩到CI。

I'd like to see if JsTestDriver can do it though. 我想看看JsTestDriver是否可以做到这一点。 Or mocha w/ a junit reporter. 或摩卡与junit记者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM