Skip to content

Instantly share code, notes, and snippets.

@kwhinnery
Created August 24, 2010 18:49
Show Gist options
  • Save kwhinnery/548085 to your computer and use it in GitHub Desktop.
Save kwhinnery/548085 to your computer and use it in GitHub Desktop.
//Run test suite (flip to false to disable)
if (true) {
// Add additional unit tests to this include statement, after runner is included
Ti.include('runner.js','my_tests.js');
unitTest.run(); //optionally pass in a suite of tests - unitTest.run('suiteOne')
}

Unit Testing Framework

A super simple testing framework for quickly validating that certain functions within the application are working properly. In app.js, you would:

Ti.include('runner.js');

Which would add a variable unitTest to the current context. To create a test, you would:

unitTest.add({
	test: function() {
		//assert a boolean that should be true on a successful test,
		//plus a description of the assertion
		unitTest.assert(true,"This should always be true.");
			
		unitTest.done();  //need to call this when test is done - All tests are assumed to be asynch
	},
	suite: 'mySuite' // this is optional - by default, all tests run
});

As a shorthand, if you don't want to associate your test with a specific suite, you can also do:

unitTest.add(function() {
	unitTest.assert(true,"This should always be true.");
	unitTest.done();
});

But since you'll probably have a bunch of tests, this is probably a bad idea - put it in a suite so you can run tests in groups. In app.js, you'll see the call to unitTest.run(), which optionally takes a test suite as an argument.

API Doc

Check assignments to the api variable in runner.js

(function() {
//alias unit test methods to save keystrokes
var assert = unitTest.assert;
var done = unitTest.done;
// A simple test
unitTest.add({
test: function() {
unitTest.warn('Register a warning');
assert(true,'This should always be true');
done();
},
suite: 'suiteOne'
});
})();
//Public test namespace
var unitTest = (function() {
var api = {}; //public test API
var unitTests = []; //our unit tests
var testsExecuted = 0;
var assertions = 0;
var successes = 0;
var failures = 0;
var warnings = 0;
var executing = false;
//Add a unit test
/*
{
suite: string associating a test with a specific suite,
test: a function that executes a test, makes assertions,
etc. - must invoke done() to continue with the tests
}
*/
api.add = function(test) {
//Is this a function? If so, add it to a test object
if (!!(test && test.constructor && test.call && test.apply)) {
unitTests.push({test:test});
}
//Otherwise we assume it is a properly formatted test object
else {
unitTests.push(test);
}
};
//Insert a warning
api.warn = function(description) {
warnings++;
Ti.API.warn('[UNIT TEST WARNING]: '+description);
};
//Assert that a given condition is true
api.assert = function(value,description) {
assertions++;
if (value) {
successes++;
Ti.API.debug('[UNIT TEST PASSED]: '+description);
}
else {
failures++;
Ti.API.error('[UNIT TEST FAILED]: '+description);
}
};
//Indicate that the current test is done running
api.done = function() {
executing = false;
};
//Run the given unit test suite
api.run = function(suite) {
Ti.API.info('********* Unit Tests ***********');
Ti.API.info('Begin hardcore testing action...');
var timer = setInterval(function() {
if (unitTests.length == 0 && !executing) {
clearInterval(timer);
Ti.API.info('Test run finished. So, how did we do?');
Ti.API.info('Total assertions: '+assertions);
Ti.API.info('Successes: '+successes);
Ti.API.info('Warnings: '+warnings);
Ti.API.info('Failures: '+failures);
if (failures > 0) {
Ti.API.error('Dude, some of your tests failed - check the log for details.');
}
Ti.API.info('********* Unit Tests ***********');
}
if (!executing && unitTests.length > 0) {
executing = true;
var tst = unitTests.shift();
if (suite == null || tst.suite == suite) {
tst.test.call(this);
testsExecuted++;
}
else {
executing = false;
}
}
},10);
};
return api;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment