logo

NJP

Unlearn Series - Unit Test Your Script Include Using ATF

Import · May 14, 2020 · article

Enough back story but how all of this is done in ATF?

Lets take a look at a simple script include - DoStuff. This has a function defaultParam, which checks if arguments are passed to it. If yes, it returns it back and if not, it will default the param to ‘test’. So now to test this function, we would need two tests, one to test function with argument and one without.

var DoStuff = Class.create();
DoStuff.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    defaultParam: function() {
        var param = (arguments.length > 0 && arguments[0] !== undefined) ? arguments[0] : 'test';
        return param;
    },
    type: "DoStuff"
});

Create a ATF test and add 'Run Server Side Script' step. Writing our Jasmine tests for the script might look like.

(function (outputs, steps, stepResult, assertEqual) {
    describe('Unit test for DoStuff', function () {

        var script;

        //Set up things which are common for all tests
        beforeAll(function () {
            script = new DoStuff();
        });

        it('defaultParam returns test', function () {
            expect(script.defaultParam()).toEqual('test');
        });

        it('defaultParam returns param', function () {
            expect(script.defaultParam('param')).toEqual('param');
        });

    });

})(outputs, steps, stepResult, assertEqual);

jasmine.getEnv().execute();

The first 'it' block (test) tests the function if no parameter is passed. "We expect the return value to be Equal to 'test'" is the literal translation of this test image Similarly the second, tests for the function if argument is passed.

Now open your ATF test and run it to see the results. If you look over the test steps results, you might find something like this

----- Tests Complete -----
Spec :: Unit test for DoStuff defaultParam returns param :: passed
Spec :: Unit test for DoStuff defaultParam returns test :: passed
Test results :: (2/2) :: passed

As you can see, both of our tests have passed and we are good to go image

Remember, using ATF as shown here will make your development process that much more fun and stress free. Now anytime you make an update to this script, all you have to do is run your ATF. You will be able to quickly determine whether you broke something which was unexpected or whether your test needs an update. And, if you really want to follow test driven development, writing such an ATF is all you need to get started.

Following the process like above, add your unit tests for individual functions as needed using Jasmine tests and chain together tests to form the test suite for the entire script.

I am adding the updateset to the post if someone wishes to try it on their instance. I hope this helps someone.

If you liked the content, please share, click helpful, bookmark or leave your valuable comments.

Labels:

View original source

https://www.servicenow.com/community/in-other-news/unlearn-series-unit-test-your-script-include-using-atf/ba-p/2285261