XulUnit
| resources: | Home Mailing List Installation Source Code Members Bugs Cookbook Screenshots |
|---|
Cookbook
The Metaphor
Remember this:
- Tests are assertions made on objects (I.e. variables, functions, values... or even proper objects). Assertions could be things like "check this variable is not null" or "check this value equals this one".
- Test Cases are functions (starting with "test_") that, among any other commands, include some tests (assertions).
- Test Suites are collection of Test Cases.
- Test Runner is the Graphical User Interface which can execute some Test Suites and show their results.
Easy! Is'nt it?
Some Examples
This is a test:
assertEquals("A simple test", 36, myAge);
Tests should be included in Test Cases to be run. This is a Test Case:
function test_MyTest(){
var myAge = 35;
myAge++;
assertEquals("A simple test", 36, myAge);
assertNotNull("Another stupid test", "notNullValue");
}
But, as Test Cases need some more things that just a function (with assertions) we need to instatiate a TestCase object. This will add result attributes and other jingles to our test function:
myTestCase = new TestCase("This is my Test Case", test_MyTest);
Test Cases should be added to Test Suites in order to be run. This is done by:
var myTestSuite = new TestSuite("TestSuite Name");
myTestSuite.add(myTestCase);
And, in order to run the Suites (and therefore the tests), we need to create a TestRunner object and add our Test Suites:
var testRunner = new TestRunner(); testRunner.add(myTestSuite);
A complete example of test construction can be found in myTests.js file (included in the XulUnit Package).
How to Run Test Runner
After creating your tests, test cases, test suites and a test runner object, you simply need to create a xul page, load any js file with your tests and classes to be tested, and call TestRunner launch method (with no arguments).
This will open a new window, run the tests and show the results. Thats all.
For example
Example at XulUnit Package myTest.xul includes the following code:
<script src="xulunit.js" /> <script src="myTests.js" /> <!-- Any other js files with classes -->
This loads XulUnit classes and the user defined tests.
To run then, you can use a simple button...
<button label="Run My Tests" onclick="testRunner.launch()"/>
You can also call launch method on the onload attribute of your window.
Assert Functions
These are the assert functions included in XulUnit:
function assert(name, toEval) Asserts toEval expression equals true
function assertTrue(name, toEval) Asserts toEval expression equals true.
function assertFalse(name, toEval) Asserts toEval expression equals false.
function assertEquals(name, value, toEval) Asserts toEval expression equals value.
function assertNotEquals(name, value, toEval) Asserts toEval expression does not equals value.
function assertNull(name, toEval) Asserts toEval expression is null.
function assertNotNull(name, toEval) Asserts toEval expression is not null.
function assertUndefined(name, toEval) Asserts toEval expression is undefined.
function assertNotUndefined(name, toEval) Asserts toEval expression is not undefined.
function assertNaN(name, toEval) Asserts toEval expression is Nan (Not a Number).
function assertNotNaN(name, toEval) Asserts toEval expression is not Nan (Not a Number). I.e. it is a number.
function assertRegExp(name, regExp, toEval) To be done.
function assertTypeOf(name, type, toEval) To be done.
Understanding Test Results
Tests can evaluate to any of four results:
- PASS The assertion is correct.
- FAIL The assertion is false.
- BREAK The test did not complete.
- UNKNOWN The assertion was done in the test.
Results follow up to cases and suites with the rule: If all passes, I pass. This means that a case PASSES only if all its tests passes. And same aplies to Suites.
If any of the tests in a case fails, the case will throw a FAIL result (eventhough you can see the results for each individual test).
If any of the tests breaks... the case and the suite will break.
Todo
I know you can't believe it. But there are still features to be added to XulUnit (on which I am working hard). The main left are:
- Fixtures: So you can do some preparation before Suite execution and some cleanning afterwards.
- Anidated Suites: For the moment, Suites can only include TestCases. In a future release, you will be able to add Suites to other Suites making your test organization more flexible.