Tester Quick Reference
Unit testing framework for OpenForum with fluent-style interface
Key Features
- Fluent-style test API (given/when/then pattern)
- Server-side JavaScript unit testing
- Test result tracking and reporting
- Interactive test runner client
- Test assertions and expectations
- Detailed failure messages
- Test suite organization
- Command-line and web-based test execution
Test Pattern
Tests follow a three-stage pattern:
- given - Set up test data
- when - Execute the action/method under test
- then - Assert expected output
Server-Side Usage:
// Import the test framework
var tester = js.getObject("/OpenForum/AddOn/Tester","Test.sjs");
// Create a class to test
var MyTestClass = function() {
this.increment = function(value) {
return value 1;
};
this.decrement = function(value) {
return value-1;
};
};
// Wrap the function under test
function testMyClass(fn) {
return function (input) {
return MyTestClass[fn](input);
};
}
// Log message about the test
tester.log("Running tests for MyTestClass");
// Create test instance
tester.unitTest("Can add one to a number").
given(122).
when( testMyClass("increment") ).
thenOutputEquals(123).
run();
// Another test
tester.unitTest("Can subtract one from a number").
given(123).
when( testMyClass("decrement") ).
thenOutputEquals(122).
run();
// Get results
var results = tester.getResults();
tester.log("Tests:" results.tests " Passed:" results.passed " Failed:" results.failed);
Test Methods
- unitTest(name) - Create new test with description
- given(data) - Provide input data
- when(function) - Specify function to test
- thenOutputEquals(expected) - Assert expected output
- run() - Execute the test
- log(message) - Log a message
- getResults() - Get test results summary
Running Tests
Via Tester Client:
/OpenForum/AddOn/Tester?pageName=/MyPage&testFileName=test.sjs
Via URL Action:
/OpenForum/AddOn/Tester?action=runTest&pageName=/MyPage&testScript=test.sjs
Test Results Format
{
tests: 2,
passed: 1,
failed: 1
}
Example Test Output
Test Results
Starting
MESSAGE: Started Tester v0.002 The Jester
MESSAGE: Running tests for MyTestClass
PASSED: Can add one to a number
FAILED: Can subtract one from a number.
Input (123) Expected (122) but found (124)
MESSAGE: Tests:2 Passed:1 Failed:1
Full Reference