How to run QUnit tests from command line?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-22 13:55:25

问题


I recently started working on a Rails app that has a large amount of QUnit tests already in place for testing ember. I have been charged with the task of setting the app with a CI (I decided to use CodeShip). The issue I currently face is that the only way for me to run the qunit tests is to go to http://localhost:3000/qunit. I need to setup a way to run the tests from the command line. I have done a large amount of research, and have tried at least 10 different solutions but non have managed to work.

Currently I am attempting to use teaspoon but it I have not managed to get it to work. Any help would be much appreciated. Please let me know if I need to post more information about the setup.


回答1:


node-qunit-phantomjs gets the job done easy enough and is standalone, not a Grunt-, Gulp-, whatever-plugin:

$ 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



回答2:


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

Run qunit --help for more usage information.




回答3:


You can use Grunt (task runner) for this. You would also need to install these two packages: grunt-contrib-qunit and grunt-contrib-connect

I did just recently set up a GitHub repository when trying to figure out how to run QUnit on Travis CI: https://github.com/stebru/travis-qunit-test

You're welcome to fork it and try it out for yourself.




回答4:


TL;DR

Use out-of-the-box qunit command (do npm install -g qunit beforehand), so you don't need additional dependencies.


Extending a bit Arthur's answer because he mentioned only simplest case which works only for simplest projects.

As mentioned on QUnit page, there is built-in possibility to run tests from command line. There is no need to install additional weird frameworks on top of QUnit!

npm install -g qunit
qunit # Will search for tests in "test" directory

This works for artificial tests as on their website, but in real project you probably will have your logic in some other .js file.

Having following structure:

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

And let's imagine that in your index.js you have following:

function doSomething(arg) {
  // do smth
  return arg;
}

And the test code in tests.js (not that it can be the whole content of the file - you don't need anything else to work):

QUnit.test( "test something", function( assert ) {
  assert.ok(doSomething(true));
});

Running in browser

This is not related directly to the question, but just want to make a reference here. Just put both your script and tests to tests.html and open the page in browser:

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

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

Running from command line

With the setup described below you can try to run qunit, but it will not work because it cannot find your function doSomething. To make it accessible you need to add two things to the scripts.

First is to explicitly "import" your script from tests. Since JS doesn't have sunch a functionality out-of-the box, we'll need to use require coming from NPM. And to keep our tests working from HTML (when you run it from browser, require is undefined) add simple check:

// 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;
}

But if index.js does not expose anything, nothing will be accessible. So it's required to expose functions you want to test explicitly (read more about exports). Add this to index.js:

//This goes to the very bottom of index.js
if (typeof module !== 'undefined' && module.exports) {
  exports.doSomething = doSomething; 
}

When it's done, first check tests.html still working and not raising any errors (testing test infrastructure, yeah) and, finaly, try

qunit

And the output should be like

TAP version 13
ok 1 Testing index.js > returnTrue returns true
1..1
# pass 1
# skip 0
# todo 0
# fail 0

I don't want to deal with node for my simple (or not) project

This is an open question and I cannot answer this. But you'll need some runner to run QUnit tests anyway. So maybe having package.json with one devDependency like "qunit": "^2.6.1" is not the worst option here. There are several 3rd-party runners: grunt-qunit, PhantomJS runnner, ember-qunit-cli, also see more on official QUnit Plugins page

What if I have class, not function?

In JS everything is a function, right :)? So no problem, just change your script exports and tests import acordingly

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

See example a.k.a. small getting started here.



来源:https://stackoverflow.com/questions/24704043/how-to-run-qunit-tests-from-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!