繁体   English   中英

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

[英]How to run QUnit tests from command line?

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

我最近刚刚设置了一个GitHub存储库,试图弄清楚如何在Travis CI上运行QUnit: 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并在浏览器中打开页面:

<script type="text/javascript" src="../index.js"></script>

<script src="tests.js"></script>

从命令行运行

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

首先是从测试中明确“导入”您的脚本。 由于JS没有sunch一个功能外的箱子,我们就需要使用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;
}

但是如果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-qunitPhantomJS runnnerember-qunit-cli ,还可以在官方的QUnit插件页面上看到更多

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

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

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

请参阅示例aka small get here here here

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM