Test JS

About Test JS

This is a test class for JavaScript with simple use. You insert the clsTest.js files into your html file, where you want to run the tests.

Create a new instance of the clsTest class and call the Init() function to initialize the test cases. All your test cases are automatically identified in your project by their function name prefix. So make sure to use a unique prefix for your test cases to avoid conflicts.

The default prefix is testCases_ but you can change it to any other prefix you want. The test cases can be anywhere in your project but must be defined at a global scope. Best practice is to have them in a separate JavaScript file typically named testCases_*.js.

Run() the tests.

ShowTestResult() to show the results in the console or in a separate popup window. The default is 'console' but you can also use 'popup' to show the results in a popup window.

Special Feature for ShowTestResult('popup'):
You can add your custom css,html and script to the popup window by setting the customCSS, customHTML_BeforeTable, customHTML_AfterTable, and customScript properties of the clsTest.popup instance.

DownloadTestResult() to save the results to a file with a different file name.

Unit Tests

Unit tests are a way to test individual functions or methods in your code. Unit test are for functions that:

For example for the followingfunctions:

you can create the following unit tests. The reuqimrents must be met:

  • the test case name has to start with your defined prefix (here 'testCase_')
  • the test case function must take the test instance as its only parameter (here: 'myTest')
  • test methods must be called with the following parameters:
    • actual value: you can directly pass here the function incl. parameters you want to test
    • expected value: what the expected result should be
    • function name: the name of the function being tested
    • test case name: the name of the test or a comment for the test. This is different from the test case name, which is the name of the whole test function (here 'testCase_addition')