Servoy has built-in support for Unit Testing. The integration adds the following to the Servoy environment:

Creating testcases is as straightforward as creating a function with a name that is prefixed by 'test_':

 

function test_thisShouldPass() {
 jsunit.assertEquals('This test should pass', true, 5 < 10);
}

function test_thisShouldFail() {
 jsunit.assertEquals('This test should fail', true, 10 < 5);
}

The JSUnit node in the Solution Explorer provides easy access to the different supported assert functions

Testcases can be added on application level in the global scope of on form level in the form scope.

setUp & tearDown

Each scope can contain a setUp and/or tearDown function. The setUp function is called BEFORE running each testcase in the scope and the tearDown function is called AFTER running each test in the scope. The setUp and tearDown function allow the developer to create the right circumstances for testcases to run and cleanup afterwards. Note that the setUp() and tearDown() methods are called before and after EACH test methods, as each single test is supposed to be independant.

 

function setUp() {
  //Code here to setup the environment for the testcases in this scope
}
function tearDown() {
  //Code here to cleanup the environment after running the testcases in this scope
}

 

assertEquals vs. assertSame

The jsunit object supports 2 methods that seem similar, but have a distinct difference: assertEquals and assertSame. The difference between these two function is the JavaScript compare operator they use:

The difference between these two operators is that the equal operator does type conversion if the objects on both sides of the operator are of different types, while the strict equal operator does not.

jsunit.assertEqual(1,'1') //true
jsunit.assertSame(1,'1') //false