diff options
author | kelvinp@chromium.org <kelvinp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-05 00:24:47 +0000 |
---|---|---|
committer | kelvinp@chromium.org <kelvinp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-05 00:24:47 +0000 |
commit | ce8777901f74d4a9aebef5dc77016ee5e7d963be (patch) | |
tree | badf59afe565393a03ef3596bd3a5b3fc76ccdb3 /third_party/qunit | |
parent | 8847487c1ce73b282e34b205b0ed816db3aafc04 (diff) | |
download | chromium_src-ce8777901f74d4a9aebef5dc77016ee5e7d963be.zip chromium_src-ce8777901f74d4a9aebef5dc77016ee5e7d963be.tar.gz chromium_src-ce8777901f74d4a9aebef5dc77016ee5e7d963be.tar.bz2 |
JavaScript unit test framework - Part II
This change enables running the JavaScript unit test on try bots using the browser test infrastructure.
To run a QUnit test suite as a browser test.
1. include third_party/qunit/src/browser_test_harness.js in your qunit test suite.
2. Derive your browser test from QUnitBrowserTestRunner, and call RunTest with the URL of the test suite.
3. modify chrome_tests.gypi to add the c++ source of your browser test and add your test suite as a run time dependency of the browser_tests executable
Summary of changes:
1. QUnit_browser_test_runner.h serves as a base class for a QUnit browser test. It navigates the browser to the QUnit test suite, calls browserTestHarness.run() and listens for the test results.
2. browser_test_harness.js serves as a test took on the QUnit test suite. Using the window.domAutomationController, browser_test_harness.js detects whether it is running under the browser test infrastructure to auto-enable itself and report results back to the browser test.
Review URL: https://codereview.chromium.org/351933003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@281451 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party/qunit')
-rw-r--r-- | third_party/qunit/README.chromium | 4 | ||||
-rw-r--r-- | third_party/qunit/src/browser_test_harness.js | 97 |
2 files changed, 100 insertions, 1 deletions
diff --git a/third_party/qunit/README.chromium b/third_party/qunit/README.chromium index ef2df607..2239331 100644 --- a/third_party/qunit/README.chromium +++ b/third_party/qunit/README.chromium @@ -29,4 +29,6 @@ It is currently used by the chrome remote desktop team for JavaScript unit testing. Local Modifications: -None +No Modifications is made to src/qunit.js. +src/browser_test_harness.js is added to enable running the QUnit test suite on +try bots using the chromium browser test infrastructure. diff --git a/third_party/qunit/src/browser_test_harness.js b/third_party/qunit/src/browser_test_harness.js new file mode 100644 index 0000000..4acfe62 --- /dev/null +++ b/third_party/qunit/src/browser_test_harness.js @@ -0,0 +1,97 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/** + * @fileoverview + * Integration module for QUnit tests running in browser tests. + * Specifically it: + * - Sets QUnit.autostart to false, so that the browser test can hook the test + * results callback before the test starts. + * - Implements a text-based test reporter to report test results back to the + * browser test. + */ + +(function(QUnit, automationController, exports) { + +'use strict'; + +var TestReporter = function() { + this.errorMessage_ = ''; + this.failedTestsCount_ = 0; + this.failedAssertions_ = []; +}; + +TestReporter.prototype.init = function(qunit) { + qunit.testDone(this.onTestDone_.bind(this)); + qunit.log(this.onAssertion_.bind(this)); +}; + +TestReporter.prototype.onTestDone_ = function(details) { + if (this.failedAssertions_.length > 0) { + this.errorMessage_ += ' ' + details.module + '.' + details.name + '\n'; + this.errorMessage_ += this.failedAssertions_.map( + function(assertion, index){ + return ' ' + (index + 1) + '. ' + assertion.message + '\n' + + ' ' + assertion.source; + }).join('\n'); + this.failedAssertions_ = []; + this.failedTestsCount_++; + } +}; + +TestReporter.prototype.onAssertion_ = function(details) { + if (!details.result) { + this.failedAssertions_.push(details); + } +}; + +TestReporter.prototype.getErrorMessage = function(){ + var errorMessage = ''; + if (this.failedTestsCount_ > 0) { + var test = (this.failedTestsCount_ > 1) ? 'tests' : 'test'; + errorMessage = this.failedTestsCount_ + ' ' + test + ' failed:\n'; + errorMessage += this.errorMessage_; + } + return errorMessage; +}; + +var BrowserTestHarness = function(qunit, domAutomationController, reporter) { + this.qunit_ = qunit; + this.automationController_ = domAutomationController; + this.reporter_ = reporter; +}; + +BrowserTestHarness.prototype.init = function() { + this.qunit_.config.autostart = false; +}; + +BrowserTestHarness.prototype.run = function() { + this.reporter_.init(this.qunit_); + this.qunit_.start(); + this.qunit_.done(function(details){ + this.automationController_.send(JSON.stringify({ + passed: details.passed == details.total, + errorMessage: this.reporter_.getErrorMessage() + })); + }.bind(this)); +}; + +// The browser test runs chrome with the flag --dom-automation, which creates +// the window.domAutomationController object. This allows the test suite to +// JS-encoded data back to the browser test. +if (automationController) { + if (!QUnit) { + console.error('browser_test_harness.js must be included after QUnit.js.'); + return; + } + + var testHarness = new BrowserTestHarness( + QUnit, + automationController, + new TestReporter()); + testHarness.init(); + exports.browserTestHarness = testHarness; +} + +})(window.QUnit, window.domAutomationController, window);
\ No newline at end of file |