問題描述
我有一個(gè)在持續(xù)集成系統(tǒng)上運(yùn)行的 webapp 構(gòu)建計(jì)劃(Atlassian Bamboo 2.5).我需要將基于 QUnit 的 JavaScript 單元測(cè)試合并到構(gòu)建計(jì)劃中,以便在每次構(gòu)建時(shí),Javascript 測(cè)試將運(yùn)行,Bamboo 將解釋測(cè)試結(jié)果.
最好我希望能夠使構(gòu)建過程獨(dú)立",這樣就不需要連接到外部服務(wù)器.關(guān)于如何實(shí)現(xiàn)這一點(diǎn)的好主意?運(yùn)行構(gòu)建過程的 CI 系統(tǒng)位于 Ubuntu Linux 服務(wù)器上.
我自己想出了一個(gè)解決方案,我認(rèn)為分享它是個(gè)好主意.這種方法可能并非完美無缺,但它似乎是第一個(gè)有效的方法.隨時(shí)發(fā)布改進(jìn)和建議.
簡(jiǎn)而言之我做了什么:
- 啟動(dòng)一個(gè) Xvfb 實(shí)例,一個(gè)虛擬幀緩沖區(qū)
- 使用 JsTestDriver:
- 在虛擬幀緩沖區(qū)中啟動(dòng)一個(gè) Firefox 實(shí)例(無頭)
- 捕獲 Firefox 實(shí)例并運(yùn)行測(cè)試套件
- 生成符合 JUnit 的測(cè)試結(jié)果 .XML
- 使用 Bamboo 檢查結(jié)果文件以通過或失敗構(gòu)建
接下來我將介紹更詳細(xì)的階段.這就是我的目錄結(jié)構(gòu)最終的樣子:
<上一頁>庫(kù)/JsTestDriver.jar測(cè)試/數(shù)量/equiv.jsQUnitAdapter.jsjsTestDriver.confrun_js_tests.sh測(cè)試.js測(cè)試報(bào)告/構(gòu)建.xml在構(gòu)建服務(wù)器上:
- 安裝 Xvfb (
apt-get install Xvfb
) - 安裝 Firefox (
apt-get install firefox
)
到您要構(gòu)建的應(yīng)用程序中:
- 安裝 JsTestDriver:http://code.google.com/p/js-測(cè)試驅(qū)動(dòng)/
- 添加 QUnit 適配器
equiv.js
和QUnitAdapter.js
- 配置 JsTestDriver (
jsTestDriver.conf
):
- 添加 QUnit 適配器
創(chuàng)建一個(gè)用于運(yùn)行單元測(cè)試和生成測(cè)試結(jié)果的腳本文件(例如在 Bash 中,run_js_tests.sh
):
#!/bin/bash# 寫入輸出 XML 的目錄(如果不存在,則不會(huì)生成結(jié)果!)OUTPUT_DIR="../test-reports"mkdir $OUTPUT_DIRXVFB=`哪個(gè) Xvfb`如果[$?"-eq 1];然后回顯未找到 Xvfb."1號(hào)出口菲火狐=`哪個(gè)火狐`如果[$?"-eq 1];然后回聲找不到火狐."1號(hào)出口菲$XVFB :99 -ac &# 將虛擬幀緩沖區(qū)啟動(dòng)到后臺(tái)PID_XVFB="$!"# 獲取進(jìn)程IDexport DISPLAY=:99 # 設(shè)置顯示使用 xvfb 的顯示# 運(yùn)行測(cè)試java -jar ../lib/JsTestDriver.jar --config jsTestDriver.conf --port 4224 --browser $FIREFOX --tests all --testOutput $OUTPUT_DIRkill $PID_XVFB # 關(guān)閉 xvfb (firefox 會(huì)被 JsTestDriver 徹底關(guān)閉)回聲完成".
創(chuàng)建一個(gè)調(diào)用腳本的 Ant 目標(biāo):
最后,告訴 Bamboo 構(gòu)建計(jì)劃調(diào)用 test
目標(biāo)并查找 JUnit 測(cè)試結(jié)果.這里默認(rèn)的 "**/test-reports/*.xml"
就可以了.
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.
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.
What I did in a nutshell:
- 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
- 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
) - 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."
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>
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.
這篇關(guān)于在持續(xù)集成構(gòu)建中無頭運(yùn)行 JavaScript 單元測(cè)試的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!