Aborting, stopping test script execution

Last edited on

Aborting test scripts

It is possible to abort/stop test scripts, but the actual mechanism depends on the scripting language being used.

For Python and JavaScript the best way is to use their exception handling mechanisms.

In both cases the mechanism used to abort the test script results in an exception being thrown — and this exception can be caught in the main() function. Furthermore it is also possible to tell this exception apart from regular exceptions that can occur at runtime.

The following examples demonstrate how to do this.

Python

from exceptions import RuntimeError

def main():
    # Do this
    # Do that

    # Example: Abort execution without error
    # if we get an error in the code below
    try:
        # !!! Do something that might throw an
        # error here !!!
        x
    except:
        raise RuntimeError("Test case aborted: ...")

    # Example: Abort if a condition is not as we like
    if xyz != "123":
        raise RuntimeError("Test case aborted: xyz != \"123\"")

    # ...

JavaScript

function main()
{
    // Do this
    // Do that

    // Example: Abort execution without error
    // if we get an error in the code below
    try {
        // !!! Do something that might throw an
        // error here !!!
        x;
    } catch (e) {
        throw "Test case aborted: ...";
    }

    // Example: Abort if a condition is not as we like
    if (xyz != "123") {
        throw "Test case aborted: xyz != \"123\"";
    }

    // ...
}