如何从命令行运行QUnit测试
How to run QUnit tests from command line?
我最近开始开发一个Rails应用程序,该应用程序已经为测试ember准备了大量的QUnit测试。我负责用CI设置应用程序的任务(我决定使用CodeShip)。我目前面临的问题是,我运行qunit测试的唯一方法是转到http://localhost:3000/qunit
。我需要设置一种从命令行运行测试的方法。我做了大量的研究,并尝试了至少10种不同的解决方案,但都没有成功。
目前我正试图使用茶匙,但我没有设法让它工作。任何帮助都将非常感激。请让我知道,如果我需要张贴有关设置的更多信息。
node- unit-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
TL;DR
使用开箱即用的qunit
命令(预先执行npm install -g qunit
),因此您不需要额外的依赖项。
稍微扩展一下Arthur的答案,因为他只提到了最简单的情况,只适用于最简单的项目。
正如在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));
});
在浏览器中运行
这与问题没有直接关系,只是想在这里做一个参考。只需将脚本和测试都放到test .html中,然后在浏览器中打开页面:
<script type="text/javascript" src="../index.js"></script>
<script src="tests.js"></script>
从命令行运行
使用下面描述的设置,您可以尝试运行qunit
,但它将无法工作,因为它无法找到您的函数doSomething
。要使其可访问,您需要在脚本中添加两件事。
首先是显式地从测试中"导入"您的脚本。由于JS没有这样的功能,我们需要使用来自NPM的require
。为了让我们的测试在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运行器,ember- quunit -cli,也可以在官方QUnit插件页面查看更多信息
如果我有类而不是函数怎么办?
在JS中一切都是一个函数,对吧:)?所以没有问题,只需相应地更改脚本导出和测试导入
exports.exportedMyClass = MyClass; // in index.js
MyClass= require('../index.js').exportedMyClass ; // in tests.js
QUnit现在有自己的命令行:
$ 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
获取更多使用信息
您可以使用Grunt(任务运行器)。您还需要安装这两个包:grunt-contrib-qunit和grunt-contrib-connect
我最近刚刚建立了一个GitHub仓库,试图找出如何在Travis CI上运行QUnit: https://github.com/stebru/travis-qunit-test
- QUnit 测试错误与 IE(Internet Explorer)
- 是否可以模拟 qUnit 测试的窗口位置对象
- 使用QUnit测试项是否具有某个类
- 在 Qunit 测试中使用 Zend 部分视图帮助程序
- 不能在 QUnit 测试中使用从 http url 引用的 jQuery
- 在烧瓶下运行 Qunit 测试的最佳方法
- 使用 qunit 测试我的代码时出错
- 如何使用 qunit 测试 html 元素的内容
- jQuery 焦点事件似乎在 qUnit 测试中不起作用
- 为什么这个 QUnit 测试失败
- 如何使用 QUnit 测试 JavaScript 错误恢复
- 使用 phantomjs 运行函数 qUnit 测试
- Qunit测试属性存在于对象中
- grunt没有在phantom上运行QUnit测试
- 在PhantomJS中从Ant运行异步QUnit测试
- Firefox 23中的QUnit测试:;类型错误:document.setUserData不是函数;
- 可变范围和qUnit测试
- QUnit测试间歇性失败
- 任何使用qunit测试异步jquery插件的方法,避免嵌入"start()"并保持异步
- 使用qunit测试javascript函数