ServiceNow Unit Testing util for server side scripts
Read this for a usage explanation.
- SNUnit
- Assertions
- SNValidator
- InvalidArgumentException
- InvalidSysIDException
- RecordNotFoundException
Initializes the test runner object
@param {Function} setup A setup function
@param {Function} teardown A teardown function
var unit = new SNUnit();The parameters are optional, as you can set them later.
Sets the setup function
@param {Function} setup A setup function
unit.setSetup(functionName);Sets the teardown function
@param {Function} teardown A teardown function
unit.setTeardown(functionName);Runs a test, first calling setup, then running the passed in function, finally running the teardown
@param {string} name A description of the test
@param {Function} callback A function containing tests
unit.test("This test does nothing", function(){
//See, I don't do anything!
});Creates an assertion object
@param {string} tableName A table name for what is being tested
@param {string} functionName The name of the function or class being tested
@param {string} testName The name of the test
var assert = new Assertions();The parameters are optional, as you can set them later.
Sets the function that is being tested
@param {string} functionName The name of the function or class being tested
assert.setFunctionName('myFunction');Sets the name of the table being tested
@param {string} tableName A table name for what is being tested
assert.setTableName('incident');Sets the name of the test being run
@param {string} testName The name of the test
assert.setTestName('This is a sample test');Asserts that a value is equal to what is expected (==)
@param {[type]} expected A value that is expected
@param {[type]} actual The actual value
@param {string} message A message describing the test
assert.assertEqual('Hello', 'Hello', "Hello should equal Hello!");Asserts that a value is equal to what is expected (===)
@param {[type]} expected A value that is expected
@param {[type]} actual The actual value
@param {string} message A message describing the test
assert.assertDeepEqual('Hello', 'Hello', "Hello should equal Hello!");Asserts that a value is not equal to what is expected (!=)
@param {[type]} expected A value that is expected
@param {[type]} actual The actual value
@param {string} message A message describing the test
assert.assertEqual('Hello', 'World', "Hello should not equal World!");Asserts that a value is not equal to what is expected (!==)
@param {[type]} expected A value that is expected
@param {[type]} actual The actual value
@param {string} message A message describing the test
assert.assertEqual('Hello', 'World', "Hello should equal World!");Asserts that an exception is thrown by a function
@param {Function} block A block of code to execute
@param {string} message A message describing the test
assert.assertThrows(function(){ throw new Error();}, "My anonymous function throws an error!");Asserts that a value is true
@param {booleanExpression} value A boolean expression
@param {string} message A message describing the test
assert.assertTrue(1 == 1, "One equals One");Asserts that a value is false
@param {booleanExpression} value A boolean expression
@param {string} message A message describing the test
assert.assertFalse(1 == 2, "One is not equal to two");Handles the passing of a test
@param {string} message A message describing the test
Handles the failing of a test
@param {string} message A message describing the test
Cleans up the message; returns '' if nothing was passed in
@param {string} message A message describing the test
@return {string} A cleaned up message
Logs the result to a table in ServiceNow
@param {string} message A message describing the test
@param {boolean} flag Whether or not the test passed
Creates a new validator object
var validator = new SNValidator();Validates that an argument is, in fact, a string
@param {string} string A string to test
@return {boolean} true if an object
validator.checkValidStringArgument('what'); //true
validator.checkValidStringArgument({}); //falseValidates that an argument is, in fact, an object
@param {Object} object An object to test
@return {Boolean} true if an object
validator.checkValidObjectArgument({}); //true
validator.checkValidObjectArgument(2); //falseValidates than an argument is, in fact, a valid number, but uses == instead of ===
@param {number} number A number to test
@return {boolean} true if the value is a number
validator.checkValidNumberArgumentLoose(1); //true
validator.checkValidNumberArgumentLoose('1'); //true
validator.checkValidNumberArgumentLoose('Potatoes'); //falseValidates than an argument is, in fact, a valid number, but uses === instead of ==
@param {number} number A number to test
@return {boolean} true if the value is a number
validator.checkValidNumberArgumentStrict(1); //true
validator.checkValidNumberArgumentStrict('1'); //falseValidates that an argument is, in fact, a sys_id
@param {string} sys_id A string to test
@return {Boolean} true if a sys_id
validator.checkValidSysIDArgument('153ebc53a4377100764f456eb40d41f1'); //true
validator.checkValidSysIDArgument('peasAndCarrots!'); //falseValidates that an argument is, in fact, a function to callback
@param {Function} callback A function to test
@return {boolean} true if a valid function (or object)
validator.checkValidCallbackArgument(function(){}); //true
validator.checkValidCallbackArgument({}); //falsethrow new InvalidArgumentException('potatoes', 'myFunction');throw new InvalidSysIDException('bananas', 'myOtherFunction');throw new RecordNotFoundException('incident', 'myIncidentFinder');
