如何从命令行运行QUnit测试?

我最近开始研究一个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

我最近在尝试弄清楚如何在Travis CI上运行QUnit时建立了一个GitHub存储库: https : //github.com/stebru/travis-qunit-test

欢迎您自行分享并尝试一下。

QUnit现在有自己的CLI :

 $ 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并在浏览器中打开页面:

从命令行运行

使用下面描述的设置,您可以尝试运行qunit ,但它无法工作,因为它无法找到您的函数doSomething 。 要使其可访问,您需要向脚本添加两个内容。

首先是从测试中明确“导入”您的脚本。 由于JS没有开箱即用的function,我们需要使用来自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插件页面

如果我上课,而不是function怎么办?

在JS中,一切都是函数,对吧:)? 所以没问题,只需改变你的脚本导出和测试导入

 exports.exportedMyClass = MyClass; // in index.js MyClass= require('../index.js').exportedMyClass ; // in tests.js 

请参阅示例aka small get here here 。