Adding global and local init and cleanup to test cases

Last edited on

For sake of easier maintenance/more flexibility one should consider introducing an init/cleanup mechanism on a global level for the test scripts.

For this a small change to any test script is require, but this can be done en masse by using the search & replace functionality of text editor (or any other tool of your choice) to add the required from .. import * command to all your scripts at once.

Test script example:

# -*- coding: utf-8 -*-


def main():
    test.log("")
    test.log("main()")
    test.log("")


def init():
    test.log("Test case init()")


def cleanup():
    test.log("Test case cleanup()")


# Must be source'd at the end of the test script!
source(findFile("scripts", "global_init_cleanup.py"))
test.py

Shared test suite resource or global script example:

# -*- coding: utf-8 -*-

init_of_testcase = None
try:
    init_of_testcase = init
except:
    pass


cleanup_of_testcase = None
try:
    cleanup_of_testcase = cleanup
except:
    pass


def init():
    test.log("Global/shared init() before test case init()")

    if init_of_testcase is not None:
        init_of_testcase()

    test.log("Global/shared init() after test case init()")


def cleanup():
    test.log("Global/shared cleanup() before test case cleanup()")

    if cleanup_of_testcase is not None:
        cleanup_of_testcase()

    test.log("Global/shared cleanup() after test case cleanup()")
global_init_cleanup.py

Test Results:

Log  Global/shared init() before test case init()
Log  Test case init()
Log  Global/shared init() after test case init()
Log  
Log  main()
Log  
Log  Global/shared cleanup() before test case cleanup()
Log  Test case cleanup()
Log  Global/shared cleanup() after test case cleanup()