如何从命令行运行QUnit测试

How to run QUnit tests from command line?

本文关键字:QUnit 测试 运行 命令行      更新时间:2023-09-26

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