Squish tip of the week: Ensuring that functions are always called pairwise

Squish tip of the week: Ensuring that functions are always called pairwise

Some functions in Squish need to be called in pairs to work correctly. For example fixateResultContext and restoreResultContext should go always together just like startsection and endSection. This is crucial in order to prevent any malfunctioning script behavior and misleading test results.

In order to ensure this will never happen even when script exceptions are raised or the control flow bypasses one of the statements, it is advisable to wrap the calls to the function pairs into helper functions which ensure that they always go together.

Here is one Javascript examples for how to ensure that fixateResultContext and restoreResultContext always go together. The same can be applied to startSection and endSection:

function withSection(f)
{
    test.fixateResultContext(1);
    try {
        return f();
    } finally {
    test.restoreResultContext();
    }
}

function main()
{
    withSection(function() {
        test.compare("Apples", "Apples");
    });

    withSection(function() {
        test.compare("Oranges", "Apples");
    });
}

Animated result example (click image to enlarge):

Ensuring that functions are always called pairwise

Any thoughts on this topic? Please let us know in the comments below.


Related topics