Permalink
Browse files

Output custom logging during automated tests.

  • Loading branch information...
1 parent 2dbe714 commit e6c9a870787ca796cae639fc9307f47623b49b60 @Mossop committed Nov 30, 2012
Showing with 117 additions and 38 deletions.
  1. +15 −11 lib/sdk/deprecated/unit-test.js
  2. +89 −23 lib/sdk/test/harness.js
  3. +13 −4 test/test-unit-test.js
View
26 lib/sdk/deprecated/unit-test.js
@@ -50,35 +50,37 @@ TestRunner.prototype = {
if (!(why in this.test.errors))
this.test.errors[why] = 0;
this.test.errors[why]++;
- if (!this.testFailureLogged) {
- this.console.error("TEST FAILED: " + this.test.name + " (" + why + ")");
- this.testFailureLogged = true;
- }
},
pass: function pass(message) {
if(!this.expectFailure) {
- this.console.info("pass:", message);
+ this.console.info("TEST-PASS", "|", this.test.name, "|", message);
this.passed++;
this.test.passed++;
}
else {
this.expectFailure = false;
- this.fail('Failure Expected: ' + message);
+ this._logTestFailed("failure");
+ this.console.error("TEST-UNEXPECTED-PASS", "|", this.test.name, "|", message);
+ this.console.trace();
+ this.failed++;
+ this.test.failed++;
}
},
fail: function fail(message) {
if(!this.expectFailure) {
this._logTestFailed("failure");
- this.console.error("fail:", message);
+ this.console.error("TEST-UNEXPECTED-FAIL", "|", this.test.name, "|", message);
this.console.trace();
this.failed++;
this.test.failed++;
}
else {
this.expectFailure = false;
- this.pass(message);
+ this.console.info("TEST-KNOWN-FAIL", "|", this.test.name, "|", message);
+ this.passed++;
+ this.test.passed++;
}
},
@@ -431,12 +433,14 @@ TestRunner.prototype = {
this.test.errors = {};
this.isDone = false;
- this.onDone = options.onDone;
+ this.onDone = function(self) {
+ self.console.info("TEST-END", "|", self.test.name);
+ options.onDone(self);
+ }
this.waitTimeout = null;
- this.testFailureLogged = false;
try {
- this.console.info("executing '" + this.test.name + "'");
+ this.console.info("TEST-START", "|", this.test.name);
if(this.test.setup) {
this.test.setup(this);
View
112 lib/sdk/test/harness.js
@@ -256,7 +256,7 @@ var POINTLESS_ERRORS = [
'Invalid chrome URI:',
'OpenGL LayerManager Initialized Succesfully.',
'[JavaScript Error: "TelemetryStopwatch:',
- '[JavaScript Warning: "ReferenceErrorL reference to undefined property',
+ '[JavaScript Warning: "ReferenceError: reference to undefined property',
'[Javascript Warning: "Error: Failed to preserve wrapper of wrapped ' +
'native weak map key',
'[JavaScript Warning: "Duplicate resource declaration for'
@@ -272,32 +272,98 @@ var consoleListener = {
var pointless = [err for each (err in POINTLESS_ERRORS)
if (message.indexOf(err) == 0)];
if (pointless.length == 0 && message)
- print("console: " + message + "\n");
+ print("\nTEST-INFO | " + message + "\n");
}
};
-function TestRunnerConsole(base, options) {
- this.__proto__ = {
- errorsLogged: 0,
- warn: function warn() {
- this.errorsLogged++;
- base.warn.apply(base, arguments);
- },
- error: function error() {
- this.errorsLogged++;
- base.error.apply(base, arguments);
- },
- info: function info(first) {
- if (options.verbose)
- base.info.apply(base, arguments);
- else
- if (first == "pass:")
- print(".");
- },
- __proto__: base
- };
+function stringify(arg) {
+ try {
+ return String(arg);
+ }
+ catch(ex) {
+ return "<toString() error>";
+ }
+}
+
+function stringifyArgs(args) {
+ return Array.map(args, stringify).join(" ");
+}
+
+function message(print, level, args) {
+ if (args[0].substring(0, 5) !== "TEST-")
+ print(level + " | ");
+ print(stringifyArgs(args) + "\n");
}
+function TestRunnerConsole(options) {
+ this.print = options.print;
+ this.verbose = options.verbose;
+ this.errorsLogged = 0;
+
+ // Binding all the public methods to an instance so that they can be used
+ // as callback / listener functions straightaway.
+ this.log = this.log.bind(this);
+ this.info = this.info.bind(this);
+ this.warn = this.warn.bind(this);
+ this.error = this.error.bind(this);
+ this.debug = this.debug.bind(this);
+ this.exception = this.exception.bind(this);
+ this.trace = this.trace.bind(this);
+};
+
+TestRunnerConsole.prototype = {
+ log: function log() {
+ if (!this.verbose)
+ this.print("\n");
+ message(this.print, "TEST-INFO", arguments);
+ },
+
+ info: function info(first) {
+ if (!this.verbose) {
+ if (first === "TEST-PASS" || first === "TEST-KNOWN-FAIL")
+ this.print(".");
+ return;
+ }
+
+ message(this.print, "TEST-INFO", arguments);
+ },
+
+ warn: function warn() {
+ this.errorsLogged++;
+ if (!this.verbose)
+ this.print("\n");
+ message(this.print, "TEST-UNEXPECTED-FAIL", arguments);
+ },
+
+ error: function error() {
+ this.errorsLogged++;
+ if (!this.verbose)
+ this.print("\n");
+ message(this.print, "TEST-UNEXPECTED-FAIL", arguments);
+ },
+
+ debug: function debug() {
+ if (!this.verbose)
+ this.print("\n");
+ message(this.print, "TEST-INFO", arguments);
+ },
+
+ exception: function exception(e) {
+ var fullString = ("An exception occurred.\n" +
+ require("../console/traceback").format(e) + "\n" + e);
+ this.error(fullString);
+ },
+
+ trace: function trace() {
+ var traceback = require("../console/traceback");
+ var stack = traceback.get();
+ stack.splice(-1, 1);
+ if (!this.verbose)
+ this.print("\n");
+ message(this.print, "TEST-INFO", [traceback.format(stack)]);
+ }
+};
+
var runTests = exports.runTests = function runTests(options) {
iterationsLeft = options.iterations;
profileMemory = options.profileMemory;
@@ -315,7 +381,7 @@ var runTests = exports.runTests = function runTests(options) {
loader = Loader(module, {
- console: new TestRunnerConsole(new PlainTextConsole(print), options)
+ console: new TestRunnerConsole(options)
});
nextIteration();
View
17 test/test-unit-test.js
@@ -121,9 +121,7 @@ exports.testWaitUntilTimeoutInCallback = function(test) {
calls: 0,
error: function(msg) {
this.calls++;
- if (this.calls == 1)
- test.assertEqual(arguments[0], "TEST FAILED: wait4ever (timed out)");
- else if (this.calls == 2) {
+ if (this.calls == 2) {
test.assertEqual(arguments[0], "test assertion never became true:\n");
test.assertEqual(arguments[1], "assertion failed, value is false\n");
// We could additionally check that arguments[1] contains the correct
@@ -140,7 +138,18 @@ exports.testWaitUntilTimeoutInCallback = function(test) {
}
},
info: function (msg) {
- test.assertEqual(msg, "executing 'wait4ever'");
+ this.calls++;
+ if (this.calls == 1) {
+ test.assertEqual(arguments[0], "TEST-START");
+ test.assertEqual(arguments[2], "wait4ever");
+ }
+ else if (this.calls == 3) {
+ test.assertEqual(arguments[0], "TEST-END");
+ test.assertEqual(arguments[2], "wait4ever");
+ }
+ else {
+ test.fail("We got unexpected console.info() calls: " + msg);
+ }
},
trace: function () {}
}

0 comments on commit e6c9a87

Please sign in to comment.