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