JsTestDriver與Sonar集成

JsTestDriver是一個JavaScript單元測試工具,易於與持續構建系統相集成並可以在多個瀏覽器上運行測試輕鬆實現TDD風格的開發。當在項目中配置好js-test-driver之後,如同junit測試java文件通常,js-test-driver能夠直接經過直接運行js文件,來對js文件單元測試。項目地址: http://code.google.com/p/js-test-driver/
php

JsTestDriver運行原理圖(好像是同事畫的,在此表示感謝): html


Sonar是一個開源平臺,用於管理源代碼的質量。咱們能夠把JsTestDriver的測試結果在Sonar上展現出來。項目地址:http://www.sonarsource.org/ java

  • JsTestDriver配置

1.代碼佈局 mysql

source folder,代碼路徑,一般狀況下就是src。
test folder,測試代碼路徑,例如src-test,unittest。
configuration file,配置文件。JsTestDriver配置文件,默認文件名爲jsTestDriver.conf。 sql

2.JsTestDriver文件 shell

依賴包:JsTestDriver.jar, coverage.jar 數據庫

配置文件:jsTestDriver.conf express

啓動腳本:shell 瀏覽器

3.JsTestDriver配置文件 oracle

配置文件是YAML格式,很容易看懂,路徑部分支持通配符,與junit徹底相似。

server: http://localhost:9876    # JsTestDriver的server地址

load:                            # 源代碼路徑
  - src/*.js
test:                            # 測試代碼路徑
  - unittest/*.js

plugin:                          # 這裏配置了一個插件,統計測試覆蓋率
 - name: "coverage"              # 插件名稱
   jar: "lib/coverage.jar"       # 插件jar包路徑
   module: "com.google.jstestdriver.coverage.CoverageModule「
若是須要生成HTML測試報告,須要安裝Icov,Ubuntu用戶:
sudo apt-get install lcov
其它平臺用戶能夠自行編譯,項目地址: http://ltp.sourceforge.net/coverage/lcov.php

更多細節能夠參考官方文檔:http://code.google.com/p/js-test-driver/wiki/ConfigurationFile

4.JsTestDriver Server配置

下載最新的JsTestDriver.jar,地址:http://code.google.com/p/js-test-driver/downloads/list

啓動server,端口9876:

java -jar JsTestDriver-1.3.5.jar --port 9876
瀏覽器打開: http://127.0.0.1:9876/ ,咱們在本地啓動,也就是localhost。


會看到兩行字:

Capture This Browser
Capture This Browser in strict mode

通常狀況下點第一個"Capture This Browser」,這裏瀏覽器就會執行單元測試的代碼,記得不要隨手關掉哦。如圖:

Capture以後的瀏覽器界面以下:

一些建議,可使用--browser來制定瀏覽器,這樣就不用手工去Capture了:

java -jar JsTestDriver-1.3.5.jar --port 9876 --browser firefox,chromium-browser
結合nohup,節約桌面空間:
nohup java -jar JsTestDriver-1.3.5.jar --port 9876 --browser firefox,chromium-browser &

也能夠查看幫助獲取更多參數:

java -jar JsTestDriver-1.3.5.jar help
 --browser VAR             : The path to the browser executable
 --browserTimeout VAR      : The ms before a browser is declared dead.
 --captureAddress VAL      : The address to capture the browser.
 --captureConsole          : Capture the console (if possible) from the browser
 --config VAL              : Loads the configuration file
 --dryRunFor VAR           : Outputs the number of tests that are going to be
                             run as well as their names for a set of
                             expressions or all to see all the tests
 --help                    : Help
 --port N                  : The port on which to start the JsTestDriver server
 --preloadFiles            : Preload the js files
 --raiseOnFailure VAL      : Whether jstd will throw an exception when a test
                             failure.
 --requiredBrowsers VAR    : Browsers that all actions must be run on.
 --reset                   : Resets the runner
 --server VAL              : The server to which to send the command
 --serverHandlerPrefix VAL : Whether the handlers will be prefixed with jstd
 --sslPort N               : The SSL port on which to start the JsTestDriver
                             server
 --testOutput VAL          : A directory to which serialize the results of the
                             tests as XML
 --tests VAR               : Run the tests specified in the form testCase.testNa
                             me
 --verbose                 : Displays more information during a run

 --plugins VAL[,VAL]       : Comma separated list of paths to plugin jars.
 --config VAL              : Path to configuration file.
 --basePath VAL[,VAL]      : Override the base path(s) in the configuration file. Defaults to the parent directory of the configuration file.
 --runnerMode VAL          : The configuration of the logging and frequency that the runner reports actions: DEBUG, DEBUG_NO_TRACE, DEBUG_OBSERVE, PROFILE, QUIET (default), INFO

官方文檔地址:http://code.google.com/p/js-test-driver/wiki/CommandLineFlags

5.運行JsTestDriver單元測試

在JsTestDriver server啓動而且Capture瀏覽器以後,就能夠執行測試了:

java -jar JsTestDriver-1.3.5.jar --config jsTestDriver.conf --tests all --testOutput ./testreport/
也能夠用--server 指定server地址:
java -jar JsTestDriver-1.3.5.jar --config jsTestDriver.conf --server http://localhost:9876 --tests all --testOutput ./testreport/

若是安裝了Icov的話,能夠生成HTML測試報告:

genhtml ./testreport/jsTestDriver.conf-coverage.dat -o ./testreport/output


  • Sonar Runner配置

(這裏比較亂,並無特定根據JsTestDriver來寫,其它語言的項目與此相似)

Sonar由3個部分組成:

Database:參數配置和代碼質量分析結果。

Web Server:展現分析結果和更改配置。

Client:運行代碼質量分析並將結果寫至Database。

Web Server和Database配置稍稍麻煩,將在下一篇blog詳細寫出。在這裏主要寫Client如何配置運行。

1.下載Sonar Runner

有幾種Client可供選擇,對外界環境依賴最少的莫過於Sonar Runner

首先下載Sonar Runner。下載地址:http://docs.codehaus.org/display/SONAR/Installing+and+Configuring+Sonar+Runner

2.環境變量配置

解壓到任何一個地址並配置環境變量$SONAR_RUNNER_HOME,將$SONAR_RUNNER_HOME/bin加入PATH。

3.檢查Sonar Runner是否配置成功

sonar-runner -h
環境變量配置正確會有輸出:

usage: sonar-runner [options]
 
Options:
 -h,--help             Display help information
 -X,--debug            Produce execution debug output
 -D,--define <arg>     Define property
可參考官方文檔: http://docs.codehaus.org/display/SONAR/Installing+and+Configuring+Sonar+Runner

4. 配置Sonar Server Databse地址

#----- Default Sonar server
#sonar.host.url=http://localhost:9000
 
#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#sonar.jdbc.driver=org.postgresql.Driver
 
#----- MySQL
#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8
#sonar.jdbc.driver=com.mysql.jdbc.Driver
 
#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
#sonar.jdbc.driver=oracle.jdbc.driver.OracleDriver
 
#----- Global database settings
#sonar.jdbc.username=sonar
#sonar.jdbc.password=sonar
 
#----- Default directory layout
#sonar.sources=src/main/java
#sonar.tests=src/test/java
#sonar.binaries=target/classes

數據庫密碼、地址,都懂。

以上Sonar配置看起來很複雜,但只須要配置一次。 環境變量也能夠不用配置,缺點就是每次跑的時候要從server下載不少jar包。
把配置OK的Sonar Runner check-in到代碼庫中讓你們共享,一勞永逸。

5.Sonar Runner項目配置

須要配置sonar-project.properties

# required metadata
sonar.projectKey=my:project                  # 項目key
sonar.projectName=My project                 # 項目顯示名稱 
sonar.projectVersion=1.0                     # 項目版本
 
# optional description
#sonar.projectDescription=Fake description   # 沒什麼用
 
# path to source directories (required)
sonar.sources=srcDir1,srcDir2                # 源代碼路徑,用英文逗號隔開
 
# path to test source directories (optional)
sonar.tests=testDir1,testDir2                # 測試代碼路徑,用英文逗號隔開
 
# path to project binaries (optional), for example directory of Java bytecode
# sonar.binaries=binDir                      # 沒什麼用

# optional comma-separated list of paths to libraries. Only path to JAR file and path to directory of classes are supported.
# sonar.libraries=path/to/library/*.jar,path/to/classes/dir,path/to/specific/library/myLibrary.jar,parent/*/*.jar
 
# The value of the property must be the key of the language.
sonar.language=js                            # 項目語言,重要
 
# Additional parameters
#sonar.my.property=value                     # 附加參數,依需求而定

官方文檔中有多模塊,多語言項目配置的介紹。地址: http://docs.codehaus.org/display/SONAR/Analyzing+with+Sonar+Runner

6.分析項目

直接執行:

sonar-runner
等執行完畢以後就能在Sonar上看到分析結果了。
相關文章
相關標籤/搜索