mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
Unit testing framework for client code.
This work is based on QUnit and tests are runnable in-browser without any external dependencies or via command-line using grunt and PhantomJS. See README.txt in `test/qunit` directory for more information on how to run tests. A test case is defined by an HTML and JavaScript source file. The file `test-common.js` will bootstrap most of the minimal HTML needed for qunit and configure require so nearly all testing logic is isolated into test js file. An example test is included that demonstrates how to build tests using requirejs's `define` and target backbone components. Tests can target non-backbone code as well. Sinon is included for stubbing out server interactions, etc... when testing backbone components. This seems to be a popular fallback for backbone projects not using Jasmine.
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
module.exports = function(grunt) {
|
||||
grunt.initConfig({
|
||||
pkg: grunt.file.readJSON('package.json'),
|
||||
|
||||
qunit: {
|
||||
all: ['tests/**/*.html'],
|
||||
options: {
|
||||
}
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
grunt.loadNpmTasks('grunt-contrib-qunit');
|
||||
// register one or more task lists (you should ALWAYS have a "default" task list)
|
||||
grunt.registerTask('default', ['qunit']);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
Running Tests in qunit Directory
|
||||
--------------------------------
|
||||
|
||||
From Command-line (with Grunt and Phantom):
|
||||
|
||||
% npm install
|
||||
% grunt
|
||||
|
||||
From Web Browser (no additional dependencies):
|
||||
|
||||
Just open test HTML file in Web Browser.
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "galaxy-qunit-tests",
|
||||
"devDependencies": {
|
||||
"grunt": "^0.4.4",
|
||||
"grunt-contrib-qunit": "^0.4.0"
|
||||
}
|
||||
}
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../static/scripts
|
||||
@@ -0,0 +1,132 @@
|
||||
var pathname = location.pathname;
|
||||
var qunit_absolute_directory = pathname.substring( 0, pathname.lastIndexOf( "qunit/" ) + 6 );
|
||||
var filename = pathname.substr( pathname.lastIndexOf( "/" ) + 1 );
|
||||
|
||||
// Configure require.js for unit testing.
|
||||
require.config({
|
||||
baseUrl: qunit_absolute_directory + "scripts",
|
||||
paths: {
|
||||
// Custom paths for Galaxy dependencies...
|
||||
"jquery": "libs/jquery/jquery",
|
||||
"backbone": "libs/backbone/backbone",
|
||||
"underscore": "libs/underscore",
|
||||
// Custom paths for qunit testing dependencies...
|
||||
"QUnit": qunit_absolute_directory + "test-libs/qunit-1.14.0", // .. because baseUrl is scripts to match Galaxy.
|
||||
"sinon": qunit_absolute_directory + "test-libs/sinon-1.9.1",
|
||||
"sinon-qunit": qunit_absolute_directory + "test-libs/sinon-qunit-1.0.0",
|
||||
},
|
||||
shim: {
|
||||
// Ensure correct Qunit order in light of requirejs loading...
|
||||
// https://gist.github.com/drewwells/920405
|
||||
"QUnit": {
|
||||
exports: "QUnit",
|
||||
init: function() {
|
||||
QUnit.config.autostart = false;
|
||||
bridge_phantomjs( QUnit );
|
||||
}
|
||||
},
|
||||
"sinon": {
|
||||
exports: "sinon"
|
||||
},
|
||||
"sinon-qunit": {
|
||||
deps: [ 'sinon', "QUnit" ],
|
||||
exports: "sinon" // Odd but seems to work
|
||||
},
|
||||
"underscore": {
|
||||
exports: "_"
|
||||
},
|
||||
"backbone": {
|
||||
deps: [ 'underscore', 'jquery' ],
|
||||
exports: "Backbone"
|
||||
}
|
||||
}
|
||||
} );
|
||||
|
||||
// Mock out Galaxy globals.
|
||||
var galaxy_config =
|
||||
{
|
||||
root: '/'
|
||||
};
|
||||
|
||||
|
||||
require( [ "jquery", "QUnit" ], function( $, QUnit ) {
|
||||
// Bootstrap HTML for displaying Qunit results.
|
||||
$('head').append( $('<link rel="stylesheet" type="text/css" />').attr( "href", qunit_absolute_directory + "test-libs/qunit-1.14.0.css") );
|
||||
$('body').append( $('<div id="qunit">') );
|
||||
$('body').append( $('<div id="qunit-fixture">') );
|
||||
|
||||
var test_module_path = "./" + filename.replace( ".html", ".js" );
|
||||
|
||||
// underscore + backbone loaded here because they are assumed globals by
|
||||
// much of the Galaxy client code.
|
||||
require( [ "underscore", "backbone" ], function( _, Backbone ) {
|
||||
|
||||
require( [ test_module_path ], function( ) {
|
||||
QUnit.load();
|
||||
QUnit.start();
|
||||
} );
|
||||
|
||||
} );
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
function bridge_phantomjs( QUnit ) {
|
||||
// Needed because the grunt task will attempt to inject this bridge assuming
|
||||
// QUnit is loaded directly - not using require.js.
|
||||
// https://github.com/gruntjs/grunt-contrib-qunit/blob/master/phantomjs/bridge.js
|
||||
var userAgent = navigator && navigator.userAgent;
|
||||
if( ! userAgent || userAgent.indexOf( "PhantomJS" ) < 0 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/*global QUnit:true, alert:true*/
|
||||
(function () {
|
||||
'use strict';
|
||||
console.log( );
|
||||
// Don't re-order tests.
|
||||
// QUnit.config.reorder = false;
|
||||
|
||||
// Send messages to the parent PhantomJS process via alert! Good times!!
|
||||
function sendMessage() {
|
||||
var args = [].slice.call(arguments);
|
||||
alert(JSON.stringify(args));
|
||||
}
|
||||
|
||||
// These methods connect QUnit to PhantomJS.
|
||||
QUnit.log(function(obj) {
|
||||
// What is this I don’t even
|
||||
if (obj.message === '[object Object], undefined:undefined') { return; }
|
||||
// Parse some stuff before sending it.
|
||||
var actual = QUnit.jsDump.parse(obj.actual);
|
||||
var expected = QUnit.jsDump.parse(obj.expected);
|
||||
// Send it.
|
||||
sendMessage('qunit.log', obj.result, actual, expected, obj.message, obj.source);
|
||||
});
|
||||
|
||||
QUnit.testStart(function(obj) {
|
||||
sendMessage('qunit.testStart', obj.name);
|
||||
});
|
||||
|
||||
QUnit.testDone(function(obj) {
|
||||
sendMessage('qunit.testDone', obj.name, obj.failed, obj.passed, obj.total);
|
||||
});
|
||||
|
||||
QUnit.moduleStart(function(obj) {
|
||||
sendMessage('qunit.moduleStart', obj.name);
|
||||
});
|
||||
|
||||
QUnit.moduleDone(function(obj) {
|
||||
sendMessage('qunit.moduleDone', obj.name, obj.failed, obj.passed, obj.total);
|
||||
});
|
||||
|
||||
QUnit.begin(function() {
|
||||
sendMessage('qunit.begin');
|
||||
});
|
||||
|
||||
QUnit.done(function(obj) {
|
||||
sendMessage('qunit.done', obj.failed, obj.passed, obj.total, obj.runtime);
|
||||
});
|
||||
}());
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
/*!
|
||||
* QUnit 1.14.0
|
||||
* http://qunitjs.com/
|
||||
*
|
||||
* Copyright 2013 jQuery Foundation and other contributors
|
||||
* Released under the MIT license
|
||||
* http://jquery.org/license
|
||||
*
|
||||
* Date: 2014-01-31T16:40Z
|
||||
*/
|
||||
|
||||
/** Font Family and Sizes */
|
||||
|
||||
#qunit-tests, #qunit-header, #qunit-banner, #qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult {
|
||||
font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
#qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult, #qunit-tests li { font-size: small; }
|
||||
#qunit-tests { font-size: smaller; }
|
||||
|
||||
|
||||
/** Resets */
|
||||
|
||||
#qunit-tests, #qunit-header, #qunit-banner, #qunit-userAgent, #qunit-testresult, #qunit-modulefilter {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
|
||||
/** Header */
|
||||
|
||||
#qunit-header {
|
||||
padding: 0.5em 0 0.5em 1em;
|
||||
|
||||
color: #8699A4;
|
||||
background-color: #0D3349;
|
||||
|
||||
font-size: 1.5em;
|
||||
line-height: 1em;
|
||||
font-weight: 400;
|
||||
|
||||
border-radius: 5px 5px 0 0;
|
||||
}
|
||||
|
||||
#qunit-header a {
|
||||
text-decoration: none;
|
||||
color: #C2CCD1;
|
||||
}
|
||||
|
||||
#qunit-header a:hover,
|
||||
#qunit-header a:focus {
|
||||
color: #FFF;
|
||||
}
|
||||
|
||||
#qunit-testrunner-toolbar label {
|
||||
display: inline-block;
|
||||
padding: 0 0.5em 0 0.1em;
|
||||
}
|
||||
|
||||
#qunit-banner {
|
||||
height: 5px;
|
||||
}
|
||||
|
||||
#qunit-testrunner-toolbar {
|
||||
padding: 0.5em 0 0.5em 2em;
|
||||
color: #5E740B;
|
||||
background-color: #EEE;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#qunit-userAgent {
|
||||
padding: 0.5em 0 0.5em 2.5em;
|
||||
background-color: #2B81AF;
|
||||
color: #FFF;
|
||||
text-shadow: rgba(0, 0, 0, 0.5) 2px 2px 1px;
|
||||
}
|
||||
|
||||
#qunit-modulefilter-container {
|
||||
float: right;
|
||||
}
|
||||
|
||||
/** Tests: Pass/Fail */
|
||||
|
||||
#qunit-tests {
|
||||
list-style-position: inside;
|
||||
}
|
||||
|
||||
#qunit-tests li {
|
||||
padding: 0.4em 0.5em 0.4em 2.5em;
|
||||
border-bottom: 1px solid #FFF;
|
||||
list-style-position: inside;
|
||||
}
|
||||
|
||||
#qunit-tests.hidepass li.pass, #qunit-tests.hidepass li.running {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#qunit-tests li strong {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#qunit-tests li a {
|
||||
padding: 0.5em;
|
||||
color: #C2CCD1;
|
||||
text-decoration: none;
|
||||
}
|
||||
#qunit-tests li a:hover,
|
||||
#qunit-tests li a:focus {
|
||||
color: #000;
|
||||
}
|
||||
|
||||
#qunit-tests li .runtime {
|
||||
float: right;
|
||||
font-size: smaller;
|
||||
}
|
||||
|
||||
.qunit-assert-list {
|
||||
margin-top: 0.5em;
|
||||
padding: 0.5em;
|
||||
|
||||
background-color: #FFF;
|
||||
|
||||
border-radius: 5px;
|
||||
}
|
||||
|
||||
.qunit-collapsed {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#qunit-tests table {
|
||||
border-collapse: collapse;
|
||||
margin-top: 0.2em;
|
||||
}
|
||||
|
||||
#qunit-tests th {
|
||||
text-align: right;
|
||||
vertical-align: top;
|
||||
padding: 0 0.5em 0 0;
|
||||
}
|
||||
|
||||
#qunit-tests td {
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
#qunit-tests pre {
|
||||
margin: 0;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
#qunit-tests del {
|
||||
background-color: #E0F2BE;
|
||||
color: #374E0C;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
#qunit-tests ins {
|
||||
background-color: #FFCACA;
|
||||
color: #500;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
/*** Test Counts */
|
||||
|
||||
#qunit-tests b.counts { color: #000; }
|
||||
#qunit-tests b.passed { color: #5E740B; }
|
||||
#qunit-tests b.failed { color: #710909; }
|
||||
|
||||
#qunit-tests li li {
|
||||
padding: 5px;
|
||||
background-color: #FFF;
|
||||
border-bottom: none;
|
||||
list-style-position: inside;
|
||||
}
|
||||
|
||||
/*** Passing Styles */
|
||||
|
||||
#qunit-tests li li.pass {
|
||||
color: #3C510C;
|
||||
background-color: #FFF;
|
||||
border-left: 10px solid #C6E746;
|
||||
}
|
||||
|
||||
#qunit-tests .pass { color: #528CE0; background-color: #D2E0E6; }
|
||||
#qunit-tests .pass .test-name { color: #366097; }
|
||||
|
||||
#qunit-tests .pass .test-actual,
|
||||
#qunit-tests .pass .test-expected { color: #999; }
|
||||
|
||||
#qunit-banner.qunit-pass { background-color: #C6E746; }
|
||||
|
||||
/*** Failing Styles */
|
||||
|
||||
#qunit-tests li li.fail {
|
||||
color: #710909;
|
||||
background-color: #FFF;
|
||||
border-left: 10px solid #EE5757;
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
#qunit-tests > li:last-child {
|
||||
border-radius: 0 0 5px 5px;
|
||||
}
|
||||
|
||||
#qunit-tests .fail { color: #000; background-color: #EE5757; }
|
||||
#qunit-tests .fail .test-name,
|
||||
#qunit-tests .fail .module-name { color: #000; }
|
||||
|
||||
#qunit-tests .fail .test-actual { color: #EE5757; }
|
||||
#qunit-tests .fail .test-expected { color: #008000; }
|
||||
|
||||
#qunit-banner.qunit-fail { background-color: #EE5757; }
|
||||
|
||||
|
||||
/** Result */
|
||||
|
||||
#qunit-testresult {
|
||||
padding: 0.5em 0.5em 0.5em 2.5em;
|
||||
|
||||
color: #2B81AF;
|
||||
background-color: #D2E0E6;
|
||||
|
||||
border-bottom: 1px solid #FFF;
|
||||
}
|
||||
#qunit-testresult .module-name {
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
/** Fixture */
|
||||
|
||||
#qunit-fixture {
|
||||
position: absolute;
|
||||
top: -10000px;
|
||||
left: -10000px;
|
||||
width: 1000px;
|
||||
height: 1000px;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* sinon-qunit 1.0.0, 2010/12/09
|
||||
*
|
||||
* @author Christian Johansen (christian@cjohansen.no)
|
||||
*
|
||||
* (The BSD License)
|
||||
*
|
||||
* Copyright (c) 2010-2011, Christian Johansen, christian@cjohansen.no
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* * Neither the name of Christian Johansen nor the names of his contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
/*global sinon, QUnit, test*/
|
||||
sinon.assert.fail = function (msg) {
|
||||
QUnit.ok(false, msg);
|
||||
};
|
||||
|
||||
sinon.assert.pass = function (assertion) {
|
||||
QUnit.ok(true, assertion);
|
||||
};
|
||||
|
||||
sinon.config = {
|
||||
injectIntoThis: true,
|
||||
injectInto: null,
|
||||
properties: ["spy", "stub", "mock", "clock", "sandbox"],
|
||||
useFakeTimers: true,
|
||||
useFakeServer: false
|
||||
};
|
||||
|
||||
(function (global) {
|
||||
var qTest = QUnit.test;
|
||||
|
||||
QUnit.test = global.test = function (testName, expected, callback, async) {
|
||||
if (arguments.length === 2) {
|
||||
callback = expected;
|
||||
expected = null;
|
||||
}
|
||||
|
||||
return qTest(testName, expected, sinon.test(callback), async);
|
||||
};
|
||||
}(this));
|
||||
@@ -0,0 +1,10 @@
|
||||
<!doctype html>
|
||||
<!-- Minimal outline test page for a requirejs+qunit testing setup,
|
||||
test environment is bootstrapped in test-common.js -->
|
||||
<html>
|
||||
<head>
|
||||
<script data-main="../test-common" src="../scripts/libs/require.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,46 @@
|
||||
// This file isn't really testing anything useful yet, it is just testing
|
||||
// (or demonstrating) qunit+backbone interactions.
|
||||
define([
|
||||
"mvc/dataset/hda-model",
|
||||
"jquery",
|
||||
"sinon-qunit"
|
||||
], function(
|
||||
hdaModel,
|
||||
$,
|
||||
sinon
|
||||
){
|
||||
module( "History Contents Model Tests" );
|
||||
|
||||
test( "HDA Constructions with Default Attributes", function() {
|
||||
var hda = new hdaModel.HistoryDatasetAssociation();
|
||||
expect( 2 );
|
||||
equal( hda.get( 'name' ), "(unnamed dataset)" );
|
||||
equal( hda.get( 'state' ), "new" );
|
||||
} );
|
||||
|
||||
test( "HDA Construction with Supplied Attributes", function() {
|
||||
var hda = new hdaModel.HistoryDatasetAssociation( {
|
||||
name: "my dataset",
|
||||
state: "ok",
|
||||
} );
|
||||
expect( 2 );
|
||||
|
||||
equal( hda.get( 'name' ), "my dataset" );
|
||||
equal( hda.get( 'state' ), "ok" );
|
||||
} );
|
||||
|
||||
test( "HDA Deletion", function() {
|
||||
var hda = new hdaModel.HistoryDatasetAssociation( {
|
||||
id: "hda1",
|
||||
history_id: "h1",
|
||||
deleted: false,
|
||||
} );
|
||||
|
||||
// Demonstrate sinon stubbing out of Ajax queries...
|
||||
equal( hda.get( 'deleted' ), false );
|
||||
sinon.stub( $, "ajax" );
|
||||
hda.delete();
|
||||
equal( hda.get( 'deleted' ), true );
|
||||
ok( $.ajax.calledWithMatch( { url: "/api/histories/h1/contents/hda1" } ) );
|
||||
} );
|
||||
} );
|
||||
Reference in New Issue
Block a user