1
0
Fork 0

[#25] Added suite runner

- Getting closer to nice clean testing
closure/master
Mike Gerwitz 2011-11-03 21:23:40 -04:00
parent 4e2af2333d
commit 705e228842
3 changed files with 136 additions and 9 deletions

View File

@ -2,16 +2,22 @@
tests := $(shell find . \ tests := $(shell find . \
-name 'test-*' \ -name 'test-*' \
-a ! -name 'test-combine*.js'\ -a ! -name 'test-combine*.js'\
-o -name '*Test.*' \ )
cases := $(shell find . \
-name '*Test.*' \
) )
tests_combine := test-combine*.js tests_combine := test-combine*.js
.PHONY: FORCE test test-combine .PHONY: FORCE test test-combine suite
default: $(tests) combine default: $(tests) suite combine
combine: $(tests_combine) combine: $(tests_combine)
suite:
@echo "ease.js Test Suite"
@echo
@NODE_PATH=".:$(NODE_PATH)" node --stack_trace_limit=20 runner.js ${cases}
%.js: FORCE %.js: FORCE
NODE_PATH=".:$(NODE_PATH)" node --stack_trace_limit=20 "$@" NODE_PATH=".:$(NODE_PATH)" node --stack_trace_limit=20 "$@"
test-%: FORCE test-%: FORCE

View File

@ -1,9 +1,35 @@
/**
* Simple X-Unit-style test cases
*
* Copyright (C) 2010 Mike Gerwitz
*
* This file is part of ease.js.
*
* ease.js is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Mike Gerwitz
* @package test
*/
var assert = require( 'assert' ), var assert = require( 'assert' ),
assert_wrapped = {}, assert_wrapped = {},
acount = 0, acount = 0,
// when set to true, final statistics will be buffered until suite ends
suite = false,
failures = [],
common_require = require( __dirname + '/common' ).require common_require = require( __dirname + '/common' ).require
; ;
@ -57,13 +83,14 @@ function incAssertCount()
*/ */
module.exports = function( test_case ) module.exports = function( test_case )
{ {
var failures = [], var context = prepareCaseContext(),
scount = 0,
context = prepareCaseContext(),
setUp = test_case.setUp; setUp = test_case.setUp;
// reset assertion count for this case // if we're not running a suite, clear out the failures
acount = 0; if ( !( suite ) )
{
init();
}
// perform case-wide setup // perform case-wide setup
test_case.caseSetUp && test_case.caseSetUp.call( context ); test_case.caseSetUp && test_case.caseSetUp.call( context );
@ -95,6 +122,31 @@ module.exports = function( test_case )
} }
} }
// only output statistics if we're not running a suite (otherwise they'll be
// output at the end of the suite)
if ( !( suite ) )
{
endStats();
}
};
/**
* Reset counters
*/
function init()
{
failures = [];
scount = 0;
acount = 0;
}
/**
* Display end stats (failures, counts)
*/
function endStats()
{
testPrint( "\n\n" ); testPrint( "\n\n" );
if ( failures.length ) if ( failures.length )
@ -114,6 +166,26 @@ module.exports = function( test_case )
failures.length failures.length
&& typeof process !== 'undefined' && typeof process !== 'undefined'
&& process.exit( 1 ); && process.exit( 1 );
}
/**
* Start test suite, deferring summary stats until call to endSuite()
*/
module.exports.startSuite = function()
{
init();
suite = true;
};
/**
* Ens test suite, display stats buffered since startSuite()
*/
module.exports.endSuite = function()
{
suite = false;
endStats();
}; };

49
test/runner.js 100644
View File

@ -0,0 +1,49 @@
/**
* Runs test suite
*
* This script must be fed the tests to be run as arguments, one test case per
* argument. All test cases should use common.testCase().
*
* Copyright (C) 2010 Mike Gerwitz
*
* This file is part of ease.js.
*
* ease.js is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Mike Gerwitz
* @package test
*/
var common = require( 'common' );
// start the test suite to defer statistics and failure output until the end of
// all test cases
common.testCase.startSuite();
// run each file provided to us
process.argv.forEach( function( val, i )
{
// first arg is 'node', second is 'runner.js'
if ( i < 2 )
{
return;
}
// the tests will run themselves; we need only require 'em
require( __dirname + '/' + val );
} );
// output statistics
common.testCase.endSuite();