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

时间:2014-07-11 18:19:47

标签: javascript ruby-on-rails ember.js continuous-integration qunit

我最近开始研究一个Rails应用程序,它已经有大量的QUnit测试用于测试ember。我被指控使用CI设置应用程序(我决定使用CodeShip)。我目前面临的问题是,运行qunit测试的唯一方法是转到http://localhost:3000/qunit。我需要设置一种从命令行运行测试的方法。我做了大量的研究,尝试了至少10种不同的解决方案,但没有成功。

目前我正在尝试使用teaspoon,但我还没有设法让它发挥作用。任何帮助将非常感激。如果我需要发布有关设置的更多信息,请告诉我。

4 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:1)

您可以使用Grunt(任务运行程序)。您还需要安装这两个软件包:grunt-contrib-qunitgrunt-contrib-connect

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

欢迎您自行分享并亲自试用。

答案 2 :(得分:1)

QUnit now has its own 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了解更多使用信息。

答案 3 :(得分: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));
});

在浏览器中运行

这与问题没有直接关系,只是想在这里做参考。 只需将脚本和测试都放入tests.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没有公开任何内容,则将无法访问任何内容。因此,需要公开要显式测试的功能(有关exports的更多信息)。将此添加到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

我不想为我的简单(或没有)项目处理节点

这是一个悬而未决的问题,我无法回答。但是无论如何,您都需要 some 运行程序来运行QUnit测试。因此,让package.json"qunit": "^2.6.1"这样的devDependency并不是最糟糕的选择。第三方竞赛者有:grunt-qunitPhantomJS runnnerember-qunit-cli,另请参见官方QUnit Plugins page

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

在JS中,所有东西都是一个函数,对吧:)?所以没问题,只需更改脚本导出并相应地测试导入

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

请参阅示例性小入门here

相关问题