Commit d7953073 d79530739470a58dfe546adb4ee27f80736da376 by Nicolas Perriault

renamed test result container classes, added tests for them

1 parent a9823c52
......@@ -82,7 +82,7 @@ var Tester = function Tester(casper, options) {
this.queue = [];
this.running = false;
this.started = false;
this.suites = new TestSuite();
this.suiteResults = new TestSuiteResult();
this.options = utils.mergeObjects({
failFast: false, // terminates a suite as soon as a test fails?
failText: "FAIL", // text to use for a succesful test
......@@ -729,7 +729,7 @@ Tester.prototype.begin = function begin(description, suiteFn) {
}
description = description || "Untitled suite in " + this.currentTestFile;
this.comment(description);
this.currentSuite = new TestSuiteResult({
this.currentSuite = new TestCaseResult({
name: description,
file: this.currentTestFile
});
......@@ -800,7 +800,7 @@ Tester.prototype.done = function done(planned) {
this.dubious(planned, this.executed);
}
if (this.currentSuite) {
this.suites.push(this.currentSuite);
this.suiteResults.push(this.currentSuite);
this.currentSuite = undefined;
this.executed = 0;
}
......@@ -947,7 +947,7 @@ Tester.prototype.pass = function pass(message) {
Tester.prototype.processAssertionResult = function processAssertionResult(result) {
"use strict";
if (!this.currentSuite) {
this.currentSuite = new TestSuiteResult({
this.currentSuite = new TestCaseResult({
name: "Untitled suite in " + this.currentTestFile,
file: this.currentTestFile
});
......@@ -976,7 +976,7 @@ Tester.prototype.processAssertionResult = function processAssertionResult(result
*/
Tester.prototype.renderFailureDetails = function renderFailureDetails() {
"use strict";
var failures = this.suites.getAllFailures();
var failures = this.suiteResults.getAllFailures();
if (failures.length === 0) {
return;
}
......@@ -1001,9 +1001,9 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) {
"use strict";
/*jshint maxstatements:20*/
save = save || this.options.save;
var failed = this.suites.countFailed(),
passed = this.suites.countPassed(),
total = this.suites.countTotal(),
var failed = this.suiteResults.countFailed(),
passed = this.suiteResults.countPassed(),
total = this.suiteResults.countTotal(),
statusText,
style,
result,
......@@ -1021,7 +1021,7 @@ Tester.prototype.renderResults = function renderResults(exit, status, save) {
style = 'GREEN_BAR';
}
result = f('%s %s tests executed in %ss, %d passed, %d failed.',
statusText, total, utils.ms2seconds(this.suites.calculateDuration()),
statusText, total, utils.ms2seconds(this.suiteResults.calculateDuration()),
passed, failed);
}
this.casper.echo(result, style, this.options.pad);
......@@ -1114,7 +1114,7 @@ Tester.prototype.runTest = function runTest(testFile) {
Tester.prototype.saveResults = function saveResults(filepath) {
"use strict";
var exporter = require('xunit').create();
exporter.setResults(this.suites);
exporter.setResults(this.suiteResults);
try {
fs.write(filepath, exporter.getXML(), 'w');
this.casper.echo(f('Result log stored in %s', filepath), 'INFO', 80);
......@@ -1164,16 +1164,16 @@ Tester.prototype.uncaughtError = function uncaughtError(error, file, line, backt
* Test suites array.
*
*/
function TestSuite() {}
TestSuite.prototype = [];
exports.TestSuite = TestSuite;
function TestSuiteResult() {}
TestSuiteResult.prototype = [];
exports.TestSuiteResult = TestSuiteResult;
/**
* Returns the number of tests.
*
* @return Number
*/
TestSuite.prototype.countTotal = function countTotal() {
TestSuiteResult.prototype.countTotal = function countTotal() {
"use strict";
return this.countPassed() + this.countFailed();
};
......@@ -1183,7 +1183,7 @@ TestSuite.prototype.countTotal = function countTotal() {
*
* @return Number
*/
TestSuite.prototype.countFailed = function countFailed() {
TestSuiteResult.prototype.countFailed = function countFailed() {
"use strict";
return this.map(function(result) {
return result.failed;
......@@ -1197,7 +1197,7 @@ TestSuite.prototype.countFailed = function countFailed() {
*
* @return Number
*/
TestSuite.prototype.countPassed = function countPassed() {
TestSuiteResult.prototype.countPassed = function countPassed() {
"use strict";
return this.map(function(result) {
return result.passed;
......@@ -1211,7 +1211,7 @@ TestSuite.prototype.countPassed = function countPassed() {
*
* @return Array
*/
TestSuite.prototype.getAllFailures = function getAllFailures() {
TestSuiteResult.prototype.getAllFailures = function getAllFailures() {
"use strict";
var failures = [];
this.forEach(function(result) {
......@@ -1225,7 +1225,7 @@ TestSuite.prototype.getAllFailures = function getAllFailures() {
*
* @return Array
*/
TestSuite.prototype.getAllPasses = function getAllPasses() {
TestSuiteResult.prototype.getAllPasses = function getAllPasses() {
"use strict";
var passes = [];
this.forEach(function(result) {
......@@ -1239,7 +1239,7 @@ TestSuite.prototype.getAllPasses = function getAllPasses() {
*
* @return Array
*/
TestSuite.prototype.getAllResults = function getAllResults() {
TestSuiteResult.prototype.getAllResults = function getAllResults() {
"use strict";
return this.getAllPasses().concat(this.getAllFailures());
};
......@@ -1250,7 +1250,7 @@ TestSuite.prototype.getAllResults = function getAllResults() {
*
* @return Number
*/
TestSuite.prototype.calculateDuration = function calculateDuration() {
TestSuiteResult.prototype.calculateDuration = function calculateDuration() {
"use strict";
return this.getAllResults().map(function(result) {
return result.time;
......@@ -1264,7 +1264,7 @@ TestSuite.prototype.calculateDuration = function calculateDuration() {
*
* @param Object options
*/
function TestSuiteResult(options) {
function TestCaseResult(options) {
"use strict";
this.name = options && options.name;
this.file = options && options.file;
......@@ -1274,7 +1274,7 @@ function TestSuiteResult(options) {
this.passes = [];
this.failures = [];
}
exports.TestSuiteResult = TestSuiteResult;
exports.TestCaseResult = TestCaseResult;
/**
* Adds a success record and its execution time to their associated stacks.
......@@ -1282,7 +1282,7 @@ exports.TestSuiteResult = TestSuiteResult;
* @param Object success
* @param Number time
*/
TestSuiteResult.prototype.addSuccess = function addSuccess(success, time) {
TestCaseResult.prototype.addSuccess = function addSuccess(success, time) {
"use strict";
success.suite = this.name;
success.time = time;
......@@ -1297,7 +1297,7 @@ TestSuiteResult.prototype.addSuccess = function addSuccess(success, time) {
* @param Object failure
* @param Number time
*/
TestSuiteResult.prototype.addFailure = function addFailure(failure, time) {
TestCaseResult.prototype.addFailure = function addFailure(failure, time) {
"use strict";
failure.suite = this.name;
failure.time = time;
......@@ -1311,7 +1311,7 @@ TestSuiteResult.prototype.addFailure = function addFailure(failure, time) {
*
* @return Number
*/
TestSuiteResult.prototype.calculateDuration = function calculateDuration() {
TestCaseResult.prototype.calculateDuration = function calculateDuration() {
"use strict";
function add(a, b) {
return a + b;
......
......@@ -32,6 +32,7 @@
var utils = require('utils');
var fs = require('fs');
var TestSuiteResult = require('tester').TestSuiteResult;
/**
* Generates a value for 'classname' attribute of the JUnit XML report.
......@@ -95,7 +96,7 @@ exports.XUnitExporter = XUnitExporter;
*/
XUnitExporter.prototype.getXML = function getXML() {
"use strict";
if (!(this.results instanceof require('tester').TestSuite)) {
if (!(this.results instanceof TestSuiteResult)) {
throw new CasperError('Results not set, cannot get XML.');
}
this.results.forEach(function(result) {
......@@ -147,7 +148,7 @@ XUnitExporter.prototype.getXML = function getXML() {
*/
XUnitExporter.prototype.setResults = function setResults(results) {
"use strict";
if (!(results instanceof require('tester').TestSuite)) {
if (!(results instanceof TestSuiteResult)) {
throw new CasperError('Invalid results type.');
}
return this.results = results;
......
......@@ -3,25 +3,7 @@
var fs = require('fs');
var t = casper.test;
casper.start();
t.comment('Tester.sortFiles()');
var testDirRoot = fs.pathJoin(phantom.casperPath, 'tests', 'testdir');
var files = t.findTestFiles(testDirRoot);
var expected = [
"01_a/abc.js",
"01_a/def.js",
"02_b/abc.js",
"03_a.js",
"03_b.js",
"04/01_init.js",
"04/02_do.js"
].map(function(entry) {
return fs.pathJoin.apply(fs, [testDirRoot].concat(entry.split('/')));
});
t.assertEquals(files, expected, 'findTestFiles() find test files and sort them');
casper.thenOpen('tests/site/index.html', function() {
casper.start('tests/site/index.html', function() {
t.comment('Tester.assertTextExists()');
t.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text');
......@@ -201,5 +183,5 @@ casper.reload(function() {
});
casper.run(function() {
t.done(55);
t.done(54);
});
......
/*jshint strict:false*/
/*global CasperError casper console phantom require*/
var fs = require('fs');
casper.test.begin('Tester.sortFiles()', function suite(test) {
var testDirRoot = fs.pathJoin(phantom.casperPath, 'tests', 'testdir');
var files = test.findTestFiles(testDirRoot);
var expected = [
"01_a/abc.js",
"01_a/def.js",
"02_b/abc.js",
"03_a.js",
"03_b.js",
"04/01_init.js",
"04/02_do.js"
].map(function(entry) {
return fs.pathJoin.apply(fs, [testDirRoot].concat(entry.split('/')));
});
test.assertEquals(files, expected, 'findTestFiles() find test files and sort them');
test.done(1);
});
/*jshint strict:false maxstatements:99 maxcomplexity:99 */
/*global CasperError casper console phantom require*/
var TestCaseResult = require('tester').TestCaseResult;
casper.test.begin('TestCaseResult.constructor() tests', function(test) {
var caseResult1 = new TestCaseResult();
test.assertType(caseResult1.name, "undefined", 'TestCaseResult.constructor() name is undefined by default');
test.assertType(caseResult1.file, "undefined", 'TestCaseResult.constructor() file is undefined by default');
var caseResult2 = new TestCaseResult({name: 'foo', file: '/tmp/foo'});
test.assertEquals(caseResult2.name, "foo", 'TestCaseResult.constructor() can set name');
test.assertEquals(caseResult2.file, "/tmp/foo", 'TestCaseResult.constructor() can set file');
test.done(4);
});
casper.test.begin('TestCaseResult.addSuccess() and TestCaseResult.addFailure() tests', function(test) {
var caseResult = new TestCaseResult({name: 'foo', file: '/tmp/foo'});
test.assertEquals(caseResult.assertions, 0, 'test case result counts no assertion by default');
test.assertEquals(caseResult.passed, 0, 'test case result counts no success by default');
test.assertEquals(caseResult.failed, 0, 'test case result counts no failure by default');
test.assertEquals(caseResult.calculateDuration(), 0,
'TestCaseResult.calculateDuration() computes initial tests duration');
var success = {};
caseResult.addSuccess(success, 1337);
test.assertEquals(caseResult.assertions, 1, 'test case result counts one assertion');
test.assertEquals(caseResult.passed, 1, 'test case result counts one success');
test.assertEquals(caseResult.failed, 0, 'test case result counts no failure');
test.assertEquals(caseResult.passes[0], success, 'TestCaseResult.addSuccess() added a success to the stack');
test.assertEquals(caseResult.passes[0].time, 1337, 'TestCaseResult.addSuccess() added test duration');
test.assertEquals(caseResult.passes[0].suite, 'foo', 'TestCaseResult.addSuccess() added suite name');
test.assertEquals(caseResult.calculateDuration(), 1337,
'TestCaseResult.calculateDuration() computes tests duration');
var failure = {};
caseResult.addFailure(failure, 42);
test.assertEquals(caseResult.assertions, 2, 'test case result counts two assertions');
test.assertEquals(caseResult.passed, 1, 'test case result counts one success');
test.assertEquals(caseResult.failed, 1, 'test case result counts no failure');
test.assertEquals(caseResult.failures[0], failure, 'TestCaseResult.addFailure() added a failure to the stack');
test.assertEquals(caseResult.failures[0].time, 42, 'TestCaseResult.addFailure() added test duration');
test.assertEquals(caseResult.failures[0].suite, 'foo', 'TestCaseResult.addFailure() added suite name');
test.assertEquals(caseResult.calculateDuration(), 1337 + 42,
'TestCaseResult.calculateDuration() computes new tests duration');
caseResult.addSuccess({}, 1000);
test.assertEquals(caseResult.assertions, 3, 'test case result counts three assertions');
test.assertEquals(caseResult.passed, 2, 'test case result counts two successes');
test.assertEquals(caseResult.failed, 1, 'test case result counts no failure');
test.assertEquals(caseResult.calculateDuration(), 1337 + 42 + 1000,
'TestCaseResult.calculateDuration() computes new tests duration');
test.done(22);
});
/*jshint strict:false*/
/*global CasperError casper console phantom require*/
var TestCaseResult = require('tester').TestCaseResult,
TestSuiteResult = require('tester').TestSuiteResult;
function generateCaseResult(options) {
var i,
nPasses = options && ~~options.nPasses,
nFailures = options && ~~options.nFailures,
caseResult = new TestCaseResult(options);
for (i = 0; i < nFailures; i++) {
caseResult.addFailure({}, i * 1000);
}
for (i = 0; i < nPasses; i++) {
caseResult.addSuccess({}, i * 1000);
}
return caseResult;
}
casper.test.begin('TestSuiteResult() basic tests', function(test) {
var suiteResult = new TestSuiteResult();
test.assertEquals(suiteResult.constructor.name, 'Array', 'TestSuiteResult() is derived from Array');
test.assertEquals(suiteResult.countTotal(), 0);
test.assertEquals(suiteResult.countFailed(), 0);
test.assertEquals(suiteResult.countPassed(), 0);
test.assertEquals(suiteResult.getAllFailures(), []);
test.assertEquals(suiteResult.getAllPasses(), []);
test.assertEquals(suiteResult.getAllResults(), []);
test.assertEquals(suiteResult.calculateDuration(), 0);
test.done();
});
casper.test.begin('TestSuiteResult() accumulation tests', function(test) {
var suiteResult = new TestSuiteResult();
suiteResult.push(generateCaseResult({
name: 'foo',
file: '/tmp/foo',
nPasses: 4,
nFailures: 1
}));
suiteResult.push(generateCaseResult({
name: 'bar',
file: '/tmp/bar',
nPasses: 3,
nFailures: 0
}));
test.assertEquals(suiteResult.countTotal(), 8);
test.assertEquals(suiteResult.countFailed(), 1);
test.assertEquals(suiteResult.countPassed(), 7);
test.assertEquals(suiteResult.getAllFailures().length, 1);
test.assertEquals(suiteResult.getAllPasses().length, 7);
test.assertEquals(suiteResult.getAllResults().length, 8);
test.assertEquals(suiteResult.calculateDuration(), 9000);
test.done();
});
......@@ -5,7 +5,7 @@ var testpage = require('webpage').create();
casper.test.begin('XUnitReporter() initialization', function suite() {
var xunit = require('xunit').create();
var results = new tester.TestSuite();
var results = new tester.TestSuiteResult();
xunit.setResults(results);
this.assertTruthy(xunit.getXML());
this.done(1);
......@@ -13,13 +13,13 @@ casper.test.begin('XUnitReporter() initialization', function suite() {
casper.test.begin('XUnitReporter() can hold test suites', function suite() {
var xunit = require('xunit').create();
var results = new tester.TestSuite();
var suite1 = new tester.TestSuiteResult({
var results = new tester.TestSuiteResult();
var suite1 = new tester.TestCaseResult({
name: 'foo',
file: '/foo'
});
results.push(suite1);
var suite2 = new tester.TestSuiteResult({
var suite2 = new tester.TestCaseResult({
name: 'bar',
file: '/bar'
});
......@@ -37,8 +37,8 @@ casper.test.begin('XUnitReporter() can hold test suites', function suite() {
casper.test.begin('XUnitReporter() can hold a suite with a succesful test', function suite() {
var xunit = require('xunit').create();
var results = new tester.TestSuite();
var suite1 = new tester.TestSuiteResult({
var results = new tester.TestSuiteResult();
var suite1 = new tester.TestCaseResult({
name: 'foo',
file: '/foo'
});
......@@ -57,8 +57,8 @@ casper.test.begin('XUnitReporter() can hold a suite with a succesful test', func
casper.test.begin('XUnitReporter() can handle a failed test', function suite() {
var xunit = require('xunit').create();
var results = new tester.TestSuite();
var suite1 = new tester.TestSuiteResult({
var results = new tester.TestSuiteResult();
var suite1 = new tester.TestCaseResult({
name: 'foo',
file: '/foo'
});
......