Zac*_*ack 10 javascript continuous-integration ruby-on-rails qunit ember.js
我最近开始研究一个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
Run Code Online (Sandbox Code Playgroud)
TL; 博士
使用开箱即用的qunit命令(npm install -g qunit预先执行),因此您不需要额外的依赖项。
扩展一点 Arthur 的答案,因为他只提到了仅适用于最简单项目的最简单案例。
正如在QUnit页面上提到的,从命令行运行测试是内置的。无需在 QUnit 之上安装额外的奇怪框架!
npm install -g qunit
qunit # Will search for tests in "test" directory
Run Code Online (Sandbox Code Playgroud)
这适用于他们网站上的人工测试,但在实际项目中,您可能会将您的逻辑放在其他一些 .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
Run Code Online (Sandbox Code Playgroud)
让我们想象一下,index.js你有以下内容:
function doSomething(arg) {
// do smth
return arg;
}
Run Code Online (Sandbox Code Playgroud)
和测试代码tests.js(不是它可以是文件的全部内容 - 你不需要其他任何东西来工作):
QUnit.test( "test something", function( assert ) {
assert.ok(doSomething(true));
});
Run Code Online (Sandbox Code Playgroud)
在浏览器中运行
这与问题没有直接关系,只是想在这里做一个参考。只需将您的脚本和测试都放入 tests.html 并在浏览器中打开页面:
<script type="text/javascript" src="../index.js"></script>
<script src="tests.js"></script>
从命令行运行
使用下面描述的设置,您可以尝试运行qunit,但它无法运行,因为它找不到您的函数doSomething。要使其可访问,您需要向脚本添加两件事。
首先是从测试中明确“导入”您的脚本。由于 JS 没有开箱即用的功能,我们需要使用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;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果index.js不公开任何内容,则将无法访问任何内容。因此,需要公开要显式测试的函数(阅读更多有关导出的信息)。将此添加到 index.js:
//This goes to the very bottom of index.js
if (typeof module !== 'undefined' && module.exports) {
exports.doSomething = doSomething;
}
Run Code Online (Sandbox Code Playgroud)
完成后,首先检查 tests.html 是否仍然有效并且没有引发任何错误(测试测试基础设施,是的),最后,尝试
qunit
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的
TAP version 13
ok 1 Testing index.js > returnTrue returns true
1..1
# pass 1
# skip 0
# todo 0
# fail 0
Run Code Online (Sandbox Code Playgroud)
我不想为我的简单(或不)项目处理节点
这是一个悬而未决的问题,我无法回答。但是无论如何您都需要一些运行器来运行 QUnit 测试。所以也许package.json拥有一个 devDependency"qunit": "^2.6.1"并不是这里最糟糕的选择。有几个 3rd-party runner:grunt-qunit,PhantomJS runner,ember-qunit-cli,也可以在官方QUnit 插件页面上查看更多信息
如果我有课程,没有功能怎么办?
在 JS 中,一切都是函数,对吧:)?所以没问题,只需更改您的脚本导出和测试导入即可
exports.exportedMyClass = MyClass; // in index.js
MyClass= require('../index.js').exportedMyClass ; // in tests.js
Run Code Online (Sandbox Code Playgroud)
请参阅此处的示例又名小入门。
小智 1
为此,您可以使用Grunt (任务运行程序)。您还需要安装这两个软件包:grunt-contrib-qunit和grunt-contrib-connect
我最近在尝试弄清楚如何在 Travis CI 上运行 QUnit 时刚刚设置了一个 GitHub 存储库: https: //github.com/stebru/travis-qunit-test
欢迎您 fork 它并亲自尝试一下。
| 归档时间: |
|
| 查看次数: |
5302 次 |
| 最近记录: |