我有一个在持续集成系统 (Atlassian Bamboo 2.5) 上运行的 webapp 构建计划。我需要加入 QUnit将基于 JavaScript 的单元测试纳入构建计划,以便在每次构建时运行 Javascript 测试,而 Bamboo 将解释测试结果。
我希望能够使构建过程“独立”进行,这样就不需要连接到外部服务器。关于如何实现这一点的好主意?运行构建过程的 CI 系统位于 Ubuntu Linux 服务器上。
最佳答案
由于我自己想出了一个解决方案,所以我认为分享它是个好主意。该方法可能并非完美无缺,但它是第一个似乎有效的方法。请随时发布改进和建议。
简而言之,我做了什么:
- 启动一个 Xvfb 实例,一个虚拟帧缓冲区
- 使用JsTestDriver :
- 将 Firefox 实例启动到虚拟帧缓冲区( headless )
- 捕获 Firefox 实例并运行测试套件
- 生成符合 JUnit 的测试结果 .XML
- 使用 Bamboo 检查结果文件以通过或失败构建
接下来我将介绍更详细的阶段。这就是我的目录结构最终的样子:
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
) - Install 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
andQUnitAdapter.js
- configure JsTestDriver (
jsTestDriver.conf
):
- add the QUnit adapters
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
):
#!/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 目标:
<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
目标并查找 JUnit 测试结果。这里默认的 "**/test-reports/*.xml"
就可以了。
关于javascript - 在持续集成构建中 headless 地运行 JavaScript 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2070499/