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

时间:2021-04-30 15:39:45

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.

我有一个在持续集成系统上运行的webapp构建计划(Atlassian Bamboo 2.5)。我需要将基于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服务器上。

7 个解决方案

#1


55  

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实例,这是一个虚拟的framebuffer
  • Using 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的测试结果
  • 使用JsTestDriver:将一个Firefox实例启动到virtual framebuffer(无头的)中,捕获Firefox实例并运行测试套件,生成与junit兼容的测试结果。xml
  • Use Bamboo to inspect the results file to pass or fail the build
  • 使用竹子检查结果文件,以通过或失败构建。

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:

在您的应用程序中构建:

  • Install JsTestDriver: http://code.google.com/p/js-test-driver/
    • add the QUnit adapters equiv.js and QUnitAdapter.js
    • 添加QUnit适配器equiv.js和QUnitAdapter.js。
    • configure JsTestDriver (jsTestDriver.conf):
    • 配置JsTestDriver(jsTestDriver.conf):
  • 安装JsTestDriver: http://code.google.com/p/js-test driver/添加QUnit适配器equiv.js和QUnitAdapter。js配置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

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

创建一个运行单元测试和生成测试结果的脚本文件(例如在Bash、run_js_test .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. Here the default "**/test-reports/*.xml" will do fine.

最后,告诉Bamboo构建计划调用测试目标并查找JUnit测试结果。这里默认“* * /测试报告/ *。xml”就会做的很好的。

#2


4  

For anyone interested in running their Jasmine BDD specs headlessly in maven, you might be interested in the jasmine-maven-plugin I maintain:

对于任何有兴趣在maven中无目的地运行茉莉花BDD规格的人来说,您可能会对我维护的jasminmail -plugin感兴趣:

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

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

#3


3  

As an alternative, you could also try TestSwarm. I've got it up and running using QUnit to run my JS tests.

作为另一种选择,您也可以尝试TestSwarm。我已经安装了它并使用QUnit运行我的JS测试。

#4


3  

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). Give it a try

在过去的一年里,我一直在玩一些解决方案,但我在Karma(以前的testacular)的球场上找不到任何东西。试一试

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

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

#5


0  

You may be able to use rhino, the headless browser, to run your unit tests on your CI machine. 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...

您可以使用rhino(无头浏览器)在CI机器上运行单元测试。当然,这里的缺点是它不会发现特定于浏览器X的bug……但是它确实胜过在CI box上安装2-3个操作系统,以覆盖所有主要平台……

But yes, this kind of sucks... but it might work just well enough in a CI scenario.

但是,是的,这很糟糕……但是在CI场景中,它可能会工作得很好。

#6


0  

I have used maven and junit to call rhino. It is not elegant, but I use it to test basic services and utility code.

我使用了maven和junit来调用rhino。它并不优雅,但我使用它来测试基本的服务和实用程序代码。

It requires mocking unsupported classes, like XHR with Java libraries.

它需要模拟不受支持的类,比如使用Java库的XHR。

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. Or mocha w/ a junit reporter.

我想看看JsTestDriver是否能做到这一点。或者mocha w/ a junit报告者。

#7


0  

JS Test Runner is a pretty good solution. It uses PhantomJS and QUnit.

JS测试运行器是一个很好的解决方案。它使用幻影和QUnit。

#1


55  

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实例,这是一个虚拟的framebuffer
  • Using 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的测试结果
  • 使用JsTestDriver:将一个Firefox实例启动到virtual framebuffer(无头的)中,捕获Firefox实例并运行测试套件,生成与junit兼容的测试结果。xml
  • Use Bamboo to inspect the results file to pass or fail the build
  • 使用竹子检查结果文件,以通过或失败构建。

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:

在您的应用程序中构建:

  • Install JsTestDriver: http://code.google.com/p/js-test-driver/
    • add the QUnit adapters equiv.js and QUnitAdapter.js
    • 添加QUnit适配器equiv.js和QUnitAdapter.js。
    • configure JsTestDriver (jsTestDriver.conf):
    • 配置JsTestDriver(jsTestDriver.conf):
  • 安装JsTestDriver: http://code.google.com/p/js-test driver/添加QUnit适配器equiv.js和QUnitAdapter。js配置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

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

创建一个运行单元测试和生成测试结果的脚本文件(例如在Bash、run_js_test .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. Here the default "**/test-reports/*.xml" will do fine.

最后,告诉Bamboo构建计划调用测试目标并查找JUnit测试结果。这里默认“* * /测试报告/ *。xml”就会做的很好的。

#2


4  

For anyone interested in running their Jasmine BDD specs headlessly in maven, you might be interested in the jasmine-maven-plugin I maintain:

对于任何有兴趣在maven中无目的地运行茉莉花BDD规格的人来说,您可能会对我维护的jasminmail -plugin感兴趣:

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

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

#3


3  

As an alternative, you could also try TestSwarm. I've got it up and running using QUnit to run my JS tests.

作为另一种选择,您也可以尝试TestSwarm。我已经安装了它并使用QUnit运行我的JS测试。

#4


3  

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). Give it a try

在过去的一年里,我一直在玩一些解决方案,但我在Karma(以前的testacular)的球场上找不到任何东西。试一试

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

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

#5


0  

You may be able to use rhino, the headless browser, to run your unit tests on your CI machine. 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...

您可以使用rhino(无头浏览器)在CI机器上运行单元测试。当然,这里的缺点是它不会发现特定于浏览器X的bug……但是它确实胜过在CI box上安装2-3个操作系统,以覆盖所有主要平台……

But yes, this kind of sucks... but it might work just well enough in a CI scenario.

但是,是的,这很糟糕……但是在CI场景中,它可能会工作得很好。

#6


0  

I have used maven and junit to call rhino. It is not elegant, but I use it to test basic services and utility code.

我使用了maven和junit来调用rhino。它并不优雅,但我使用它来测试基本的服务和实用程序代码。

It requires mocking unsupported classes, like XHR with Java libraries.

它需要模拟不受支持的类,比如使用Java库的XHR。

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. Or mocha w/ a junit reporter.

我想看看JsTestDriver是否能做到这一点。或者mocha w/ a junit报告者。

#7


0  

JS Test Runner is a pretty good solution. It uses PhantomJS and QUnit.

JS测试运行器是一个很好的解决方案。它使用幻影和QUnit。