OpenForum Tester - A Quick Reference

The OpenForum tester is based around a simple idea. There are three stages to a test:

  1. given some set of data
  2. when some event happens
  3. then some output is expected

The testing frame work is based on a fluent style interface. Methods are chained together to create meaningful flows of actions.

To create a simple test on the server, first import the test framework:

var tester = js.getObject("/OpenForum/AddOn/Tester","Test.sjs"); Then create or load something to test: var MyTestClass = function() { this.increment = function(value) { return value+1; }; this.decrement = function(value) { return value+1; }; }; Currently you have to wrap the function under test in a method that returns the method under test when requsted. function testMyClass(fn) { return function (input) { return MyTestClass[fn](input); }; } Optionally log a mesage about the test tester.log("Running tests for MyTestClass"); Then create a test instance: tester.unitTest("Can add one to a number"). given(122). when( testMyClass("increment") ). thenOutputEquals(123). run(); Then create another test instance: tester.unitTest("Can subtract one from a number"). given(123). when( testMyClass("decrement") ). thenOutputEquals(122). run(); You can then retrieve the results and display them. var results = tester.getResults(); tester.log("Completed example tests Tests:"+results.tests+" Passed:"+results.passed+" Failed:"+results.failed);

You can run the test using the Tester Client by supplying the pageName and test fileName then pressing run tests. Here is a link to fill in the fields for you. /OpenForum/AddOn/Tester?pageName=/OpenForum/AddOn/Tester/QuickReference&testFileName=example.test.sjs

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: Completed example tests Tests:2 Passed:1 Failed:1 As you can see, the second test failed as it is incrementing rather than decrementing.

Alternatively you can call the Tester get method: https://open-forum.onestonesoup.org/OpenForum/AddOn/Tester?action=runTest&pageName=/OpenForum/AddOn/Tester/QuickReference&testScript=example.test.sjs or run the test on the server using the Server Console editor plugin

Full Reference