$ qunit TAP version 13 ok 1 hello test not ok 2 hello test --- message: "failed!" severity: failed actual: 1 expected: 2 stack: at Object.<anonymous> (E:\release10.0\fine-js-test\test\use-qunit-test.js:10:12) at runTest (E:\release10.0\fine-js-test\node_modules\qunit\qunit\qunit.js:1530:30) at Test.run (E:\release10.0\fine-js-test\node_modules\qunit\qunit\qunit.js:1516:6) at E:\release10.0\fine-js-test\node_modules\qunit\qunit\qunit.js:1737:12 at advanceTaskQueue (E:\release10.0\fine-js-test\node_modules\qunit\qunit\qunit.js:1129:6) at advance (E:\release10.0\fine-js-test\node_modules\qunit\qunit\qunit.js:1110:4) ... 1..2 # pass 1 # skip 0 # todo 0 # fail 1
beforeEach(function () { console.log('Array before for each') });
afterEach(function () { console.log('Array after for each') });
describe('#indexOf()', function () { describe('when not present', function () { it('should not throw an error', function () { (function () { [1, 2, 3].indexOf(4); }).should.not.throw(); }); it('should return -1', function () { [1, 2, 3].indexOf(4).should.equal(-1); }); }); describe('when present', function () { it('should return the index where the element first appears in the array', function () { [1, 2, 3].indexOf(3).should.equal(2); }); }); }); });
相应的执行结果:
1 2 3 4 5 6 7 8
Array before Array before for each Array after for each Array before for each Array after for each Array before for each Array after for each Array after
Array Array before #indexOf() when not present Array before for each √ should not throw an error Array after for each Array before for each √ should return -1 Array after for each when present Array before for each √ should return the index where the element first appears in the array Array after for each Array after
Array suite set up #indexOf() √ should return -1 when not present suite tear down
var testCase = require('mocha').describe; var pre = require('mocha').before; var assertions = require('mocha').it; var assert = require('chai').assert;
var chai = require('chai'), assert = chai.assert, should = chai.should();
describe('async test', function () { before(function (done) { console.log('async test before'); sucCallback = function () { // some success method console.log('test before save success'); done(); }; var d = newDemo().save(sucCallback); });
afterEach(function () { console.log('async test after each') });
it('async test #save', function (done) { sucCallback = function () { // some success method console.log('save success'); done(); }; var d = newDemo().save(sucCallback); }) });
借助 nodejs 模仿在浏览器运行的代码
casperjs is a navigation scripting & testing utility for the PhantomJS (WebKit) and SlimerJS (Gecko) headless browsers, written in Javascript.
casperjs 需要先安装 PhantomJS 或者 SlimerJS。通过 get start 一步一步就可以。
demo:
1 2 3 4 5 6 7 8 9 10 11 12 13
var casper = require('casper').create(); casper.start('http://casperjs.org/');
yaohw@yaohwu MINGW64 /e/release10.0/fine-js-test (master) $ First Page: CasperJS, a navigation scripting and testing utility for PhantomJS and SlimerJS Second Page: PhantomJS - Scriptable Headless Browser