[英]How to run QUnit tests from command line?
我最近開始研究一個Rails應用程序,它已經有大量的QUnit測試用於測試ember。 我被指控使用CI設置應用程序(我決定使用CodeShip)。 我目前面臨的問題是,運行qunit測試的唯一方法是轉到http://localhost:3000/qunit
。 我需要設置一種從命令行運行測試的方法。 我做了大量的研究,嘗試了至少10種不同的解決方案,但沒有成功。
目前我正在嘗試使用茶匙,但我沒有設法讓它工作。 任何幫助將非常感激。 如果我需要發布有關設置的更多信息,請告訴我。
node-qunit-phantomjs可以很容易地完成工作並且是獨立的,而不是Grunt-,Gulp-,無論什么插件:
$ npm install -g node-qunit-phantomjs
$ node-qunit-phantomjs tests.html
Testing tests.html
Took 8 ms to run 1 tests. 0 passed, 1 failed.
...
$ echo $?
1
你可以使用Grunt (任務運行器)。 您還需要安裝這兩個軟件包: grunt-contrib-qunit和grunt-contrib-connect
我最近剛剛設置了一個GitHub存儲庫,試圖弄清楚如何在Travis CI上運行QUnit: https : //github.com/stebru/travis-qunit-test
歡迎您自行分享並嘗試一下。
$ npm install -g qunit
$ qunit 'tests/*-test.js'
TAP version 13
ok 1 Module > Test #1
ok 2 Module > Test #2
1..2
# pass 2
# skip 0
# todo 0
# fail 0
運行qunit --help
以獲取更多用法信息。
TL; DR
使用開箱即用的qunit
命令(事先執行npm install -g qunit
),這樣就不需要額外的依賴項了。
延伸了一點亞瑟的答案,因為他提到的最簡單的案例只適用於最簡單的項目。
如QUnit頁面所述,內置的可能性是從命令行運行測試。 沒有必要在QUnit上安裝額外的奇怪框架!
npm install -g qunit
qunit # Will search for tests in "test" directory
這適用於他們網站上的人工測試,但在實際項目中,您可能會在其他.js文件中使用您的邏輯。
有以下結構:
project
│ index.js <--- Your script with logic
│ package.json <--- most probably you'll have npm file since qunit executable is installed via npm
└───test
tests.html <--- QUnit tests included in standard HTML page for "running" locally
tests.js <--- QUnit test code
讓我們假設您在index.js
有以下內容:
function doSomething(arg) {
// do smth
return arg;
}
tests.js
的測試代碼(不是它可以是文件的整個內容 - 你不需要任何其他工作):
QUnit.test( "test something", function( assert ) {
assert.ok(doSomething(true));
});
在瀏覽器中運行
這與問題沒有直接關系,只是想在這里提一下。 只需將腳本和測試放到tests.html並在瀏覽器中打開頁面:
<script type="text/javascript" src="../index.js"></script>
<script src="tests.js"></script>
從命令行運行
使用下面描述的設置,您可以嘗試運行qunit
,但它無法工作,因為它無法找到您的函數doSomething
。 要使其可訪問,您需要向腳本添加兩個內容。
首先是從測試中明確“導入”您的腳本。 由於JS沒有sunch一個功能外的箱子,我們就需要使用require
來自NPM來。 並保持我們的測試從HTML工作(當您從瀏覽器運行它時, require
是未定義的)添加簡單的檢查:
// Add this in the beginning of tests.js
// Use "require" only if run from command line
if (typeof(require) !== 'undefined') {
// It's important to define it with the very same name in order to have both browser and CLI runs working with the same test code
doSomething = require('../index.js').doSomething;
}
但是如果index.js
沒有暴露任何東西,那么什么都不可訪問。 因此,需要公開要顯式測試的函數(閱讀有關導出的更多信息)。 將其添加到index.js:
//This goes to the very bottom of index.js
if (typeof module !== 'undefined' && module.exports) {
exports.doSomething = doSomething;
}
完成后,首先檢查tests.html是否仍在工作,不會產生任何錯誤(測試測試基礎設施,是的),最后,嘗試
qunit
輸出應該是這樣的
TAP version 13
ok 1 Testing index.js > returnTrue returns true
1..1
# pass 1
# skip 0
# todo 0
# fail 0
我不想為我的簡單(或不是)項目處理節點
這是一個懸而未決的問題,我無法回答這個問題。 但是無論如何你都需要一些跑步者來運行QUnit測試。 所以也許讓package.json
有一個devDependency就像"qunit": "^2.6.1"
不是這里最糟糕的選擇。 有幾個第三方跑步者: grunt-qunit , PhantomJS runnner , ember-qunit-cli ,還可以在官方的QUnit插件頁面上看到更多
如果我上課,而不是功能怎么辦?
在JS中,一切都是函數,對吧:)? 所以沒問題,只需改變你的腳本導出和測試導入
exports.exportedMyClass = MyClass; // in index.js
MyClass= require('../index.js').exportedMyClass ; // in tests.js
請參閱示例aka small get here here here 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.