Commit 26ee5cf5 26ee5cf51aecf8961528fadd48ef91b2bf94686b by Nicolas Perriault

added a way to set planned tests

In order to check that every planned test has actually
been executed, a new optional `planned` parameter has been
added to `Tester.done()`:

```js
casper.test.assert(true);
casper.test.assert(true);
casper.test.assert(true);
casper.test.done(4);
```

Will trigger a failure:

```
fail: 4 tests planned, 3 tests executed.
```
1 parent 0cb73a8a
......@@ -6,6 +6,8 @@ XXXX-XX-XX, v1.0.0
### Important Changes & Caveats
#### Casper.evaluate() signature compatibility with PhantomJS
`Casper.evaluate()` method signature is now compatible with PhantomJS' one, so you can now write:
```js
......@@ -22,6 +24,27 @@ casper.evaluate(function(a, b) {
}, {a: "foo", b: "bar"}); // true
```
#### Specification of planned tests ####
In order to check that every planned test has actuall been executed, a new optional `planned` parameter has been added to `Tester.done()`:
```js
casper.test.assert(true);
casper.test.assert(true);
casper.test.assert(true);
casper.test.done(4);
```
Will trigger a failure:
```
fail: 4 tests planned, 3 tests executed.
```
That's especially useful in case a given test script is abruptly interrupted leaving you with no obvious way to know it and an erroneous success status.
The whole [CapserJS test suite](https://github.com/n1k0/casperjs/tree/master/tests/) has been migrated to use this new feature.
### Bugfixes & enhancements
- fixed [#281](https://github.com/n1k0/casperjs/issues/281) - `Casper.evaluate()` should take an array as context not object
......
Subproject commit c3a7cd1633371805cdf0ab622cf68c4d0b8eaa2a
Subproject commit 9ddb0c6eb79d0c6b176d7bf41187e06b2e8fc708
......
......@@ -48,6 +48,7 @@ exports.create = function create(casper, options) {
*/
var Tester = function Tester(casper, options) {
"use strict";
/*jshint maxstatements:20*/
if (!utils.isCasperObject(casper)) {
throw new CasperError("Tester needs a Casper instance");
......@@ -57,6 +58,7 @@ var Tester = function Tester(casper, options) {
this.SKIP_MESSAGE = '__termination__';
this.executed = 0;
this.currentTestFile = null;
this.currentSuiteNum = 0;
this.exporter = require('xunit').create();
......@@ -141,6 +143,7 @@ exports.Tester = Tester;
*/
Tester.prototype.assert = Tester.prototype.assertTrue = function assert(subject, message, context) {
"use strict";
this.executed++;
return this.processAssertionResult(utils.mergeObjects({
success: subject === true,
type: "assert",
......@@ -691,9 +694,14 @@ Tester.prototype.configure = function configure() {
/**
* Declares the current test suite done.
*
* @param Number planned Number of planned tests
*/
Tester.prototype.done = function done() {
Tester.prototype.done = function done(planned) {
"use strict";
if (planned > 0 && planned !== this.executed) {
this.fail(f('%s: %d tests planned, %d tests executed',
this.currentTestFile, planned, this.executed));
}
this.emit('test.done');
this.running = false;
};
......@@ -981,6 +989,7 @@ Tester.prototype.runTest = function runTest(testFile) {
"use strict";
this.bar(f('Test file: %s', testFile), 'INFO_BAR');
this.running = true; // this.running is set back to false with done()
this.executed = 0;
this.exec(testFile);
};
......
......@@ -22,5 +22,5 @@ casper.thenOpen('tests/site/index.html');
casper.run(function() {
this.removeListener('resource.requested', fetchUA);
this.test.done();
this.test.done(3);
});
......
......@@ -9,5 +9,6 @@ casper.on('remote.alert', function(message) {
casper.start('tests/site/alert.html').run(function() {
this.test.assert(ok, 'alert event has been intercepted');
this.test.done();
this.removeAllListeners('remote.alert');
this.test.done(1);
});
......
......@@ -20,5 +20,5 @@ casper.test.assertEquals(casper.page.settings.userName, 'niko');
casper.test.assertEquals(casper.page.settings.password, 'plop');
casper.run(function() {
this.test.done();
this.test.done(8);
});
......
......@@ -13,8 +13,7 @@ casper.start('tests/site/index.html', function() {
this.test.assert(fs.isFile(testFile), 'Casper.capture() captured a screenshot');
});
if (phantom.version.major === 1 && phantom.version.minor >= 6) {
casper.thenOpen('tests/site/index.html', function() {
casper.thenOpen('tests/site/index.html', function() {
this.test.comment('Casper.captureBase64()');
this.test.assert(this.captureBase64('png').length > 0,
'Casper.captureBase64() rendered a page capture as base64');
......@@ -22,12 +21,11 @@ if (phantom.version.major === 1 && phantom.version.minor >= 6) {
'Casper.captureBase64() rendered a capture from a selector as base64');
this.test.assert(this.captureBase64('png', {top: 0, left: 0, width: 30, height: 30}).length > 0,
'Casper.captureBase64() rendered a capture from a clipRect as base64');
});
}
});
casper.run(function() {
try {
fs.remove(testFile);
} catch(e) {}
this.test.done();
this.test.done(4);
});
......
......@@ -59,5 +59,5 @@ casper.then(function() {
});
casper.run(function() {
this.test.done();
this.test.done(21);
});
......
......@@ -13,5 +13,5 @@ casper.start('tests/site/confirm.html', function() {
casper.run(function() {
this.test.assertEquals(received, 'are you sure?', 'confirmation message is ok');
this.test.done();
this.test.done(2);
});
......
......@@ -6,5 +6,5 @@ casper.start('tests/site/index.html', function() {
});
casper.run(function() {
casper.test.done();
casper.test.done(2);
});
......
......@@ -6,5 +6,5 @@ casper.start('tests/site/elementattribute.html', function() {
});
casper.run(function() {
this.test.done();
this.test.done(1);
});
......
......@@ -20,5 +20,5 @@ casper.start('file://' + phantom.casperPath + '/tests/site/index.html', function
});
casper.run(function() {
this.test.done();
this.test.done(2);
});
......
......@@ -70,4 +70,4 @@ casper.test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, "foo", "bar", "baz"), ["foo", "bar", "baz"], 'Casper.evaluate() accepts natural arguments context');
casper.test.done();
casper.test.done(11);
......
......@@ -37,4 +37,4 @@ casper.test.assertEquals(casper.foo, 42, "filter() applies the correct context")
delete casper.foo;
casper.test.done();
casper.test.done(5);
......
......@@ -7,5 +7,5 @@ casper.start('tests/site/index.html', function() {
});
casper.run(function() {
this.test.done();
this.test.done(1);
});
......
......@@ -7,5 +7,5 @@ casper.start('tests/site/index.html', function() {
});
casper.run(function() {
this.test.done();
this.test.done(1);
});
......
......@@ -35,4 +35,4 @@ casper.then ->
casper.then ->
@test.assertEquals ++step, 13, "last step"
casper.run(-> @test.done())
casper.run(-> @test.done(13))
......
......@@ -100,5 +100,5 @@ casper.thenOpen('tests/site/field-array.html', function() {
});
casper.run(function() {
this.test.done();
this.test.done(19);
});
......
......@@ -18,5 +18,5 @@ casper.start('tests/site/frames.html', function() {
casper.run(function() {
this.page.switchToMainFrame();
this.test.done();
this.test.done(8);
});
......
......@@ -7,5 +7,5 @@ casper.start('tests/site/global.html', function() {
});
casper.run(function() {
this.test.done();
this.test.done(2);
});
......
......@@ -37,5 +37,5 @@ casper.thenOpen('http://localhost:8090/', function thenLocalhost(response) {
casper.run(function() {
server.close();
this.test.done();
this.test.done(4);
});
......
......@@ -19,5 +19,5 @@ casper.then(function() {
casper.run(function() {
this.test.assert(this.history.length > 0, 'Casper.history contains urls');
this.test.assertMatch(this.history[0], /tests\/site\/page1\.html$/, 'Casper.history has the correct first url');
this.test.done();
this.test.done(4);
});
......
......@@ -39,5 +39,5 @@ casper.then(function() {
casper.run(function() {
this.options.onAlert = null;
this.test.done();
this.test.done(5);
});
......
......@@ -36,5 +36,5 @@ casper.then(function() {
casper.run(function() {
this.test.assertEquals(this.result.log.length, 3, 'Casper.log() logged messages');
this.test.done();
this.test.done(4);
});
......
......@@ -25,5 +25,5 @@ casper.then(function() {
});
casper.run(function() {
this.test.done();
this.test.done(16);
});
......
......@@ -17,5 +17,5 @@ casper.thenOpen('tests/site/error.html', function() {
});
casper.run(function() {
this.test.done();
this.test.done(2);
});
......
......@@ -131,5 +131,5 @@ casper.thenOpen('tests/site/index.html', {
casper.run(function() {
this.removeAllListeners('open');
t.done();
t.done(16);
});
......
......@@ -9,5 +9,5 @@ casper.start('tests/site/prompt.html', function() {
});
casper.run(function() {
this.test.done();
this.test.done(1);
});
......
......@@ -32,5 +32,5 @@ casper.thenOpen('tests/site/index.html');
casper.run(function() {
this.removeAllListeners('page.resource.requested');
t.done();
t.done(3);
});
......
......@@ -21,4 +21,4 @@ casper.start "tests/site/resources.html", ->
onTimeout = -> @test.fail "waitForResource timeout occured"
@waitForResource "dummy.js", onTime, onTimeout
casper.run(-> @test.done())
casper.run(-> @test.done(5))
......
......@@ -28,5 +28,5 @@ casper.thenOpen('tests/site/form.html', function() {
casper.run(function() {
this.options.remoteScripts = [];
this.test.done();
this.test.done(6);
});
......
......@@ -13,5 +13,5 @@ casper.start('tests/site/index.html', function() {
casper.test.assert(casper.started, 'Casper.start() started');
casper.run(function() {
this.test.done();
this.test.done(4);
});
......
......@@ -30,5 +30,5 @@ casper.each([1, 2, 3], function(self, item, i) {
});
casper.run(function() {
this.test.done();
this.test.done(8);
});
......
......@@ -17,6 +17,5 @@ casper.then(function() {
casper.run(function() {
this.test.assertHttpStatus(200);
this.test.assertUrlMatches('Forlì', 'Casper.getCurrentUrl() retrieves a decoded URL');
this.test.done();
this.test.done(6);
});
......
......@@ -10,4 +10,4 @@ casper.test.assertEquals(casper.page.viewportSize.width, 1337, 'Casper.viewport(
casper.test.assertEquals(casper.page.viewportSize.height, 999, 'Casper.viewport() can change the height of page viewport');
casper.test.assertRaises(casper.viewport, ['a', 'b'], 'Casper.viewport() validates viewport size data');
casper.test.done();
casper.test.done(3);
......
......@@ -15,5 +15,5 @@ casper.start('tests/site/visible.html', function() {
});
casper.run(function() {
this.test.done();
this.test.done(4);
});
......
......@@ -9,8 +9,9 @@ casper.start('tests/site/index.html', function() {
casper.wait(1000, function() {
this.test.comment('Casper.wait()');
this.test.assert(new Date().getTime() - waitStart > 1000, 'Casper.wait() can wait for a given amount of time');
// Casper.waitFor()
casper.thenOpen('tests/site/waitFor.html', function() {
});
casper.thenOpen('tests/site/waitFor.html', function() {
this.test.comment('Casper.waitFor()');
this.waitFor(function() {
return this.evaluate(function() {
......@@ -21,7 +22,6 @@ casper.wait(1000, function() {
}, function() {
this.test.fail('Casper.waitFor() can wait for something to happen');
});
});
});
casper.thenOpen('tests/site/waitFor.html').waitForText('<li>four</li>', function() {
......@@ -33,5 +33,5 @@ casper.thenOpen('tests/site/waitFor.html').waitForText('<li>four</li>', function
});
casper.run(function() {
this.test.done();
this.test.done(3);
});
......
......@@ -30,5 +30,5 @@ casper.thenClick(x('/html/body/a[2]'), function() {
});
casper.run(function() {
this.test.done();
this.test.done(6);
});
......
......@@ -124,4 +124,4 @@ t.comment('parse(), get(), has()');
}, 'drop() did not affect other raw options');
})(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false']));
t.done();
t.done(76);
......
......@@ -109,5 +109,5 @@ function fakeDocument(html) {
})(casper);
casper.run(function() {
this.test.done();
this.test.done(28);
});
......
......@@ -16,4 +16,4 @@ casper.then ->
casper.run ->
@test.assertEquals steps, 3, "Casper.options.onStepComplete() is called on step complete"
@options.onStepComplete = null
@test.done()
@test.done(4)
......
......@@ -35,4 +35,4 @@ var fs = require('fs'), t = casper.test;
}
})();
t.done();
t.done(14);
......
......@@ -40,5 +40,5 @@ casper.each(codes, function(self, code) {
casper.run(function() {
server.close();
this.test.done();
this.test.done(109);
});
......
......@@ -63,4 +63,4 @@ t.assertEquals(processed(), 3, 'FunctionArgsInjector.process() processed the fun
var fnIssue129 = createInjector(issue129).process({term: 'fixed'});
t.assertEquals(fnIssue129('fixed'), 'fixed', 'FunctionArgsInjector.process() has issue #129 fixed');
t.done();
t.done(12);
......
......@@ -20,4 +20,4 @@ try {
casper.test.fail('require() patched version can load a coffeescript module');
}
casper.test.done();
casper.test.done(2);
......
......@@ -205,5 +205,5 @@ casper.then(function() {
});
casper.run(function() {
t.done();
t.done(56);
});
......
......@@ -306,4 +306,4 @@ t.comment('unique()');
});
})();
t.done();
t.done(112);
......
......@@ -15,4 +15,4 @@ casper.test.assertMatch(xunit.getXML(), /<testcase classname="(.*)plop" name="It
xunit.addSuccess(require('fs').workingDirectory + '/plip.js', 'Failure');
casper.test.assertMatch(xunit.getXML(), /<testcase classname="(.*)plip" name="Failure"/, 'XUnitExporter.addFailure() handles class name');
casper.test.done();
casper.test.done(4);
......