Global initialization and cleanup functions

Squish offers a great way to separate the main body of your test case, initialization and cleanup parts. It can be useful in many cases, simplifies complexity and improves test case readability. In combination with some scripting it can reduce one of the worst enemies of maintainability – code duplication.

The init() and cleanup() functions have a huge advantage over standard functions calls. They are independent of the main() function so even if the code inside the main() throws an error, those two functions are executed.

For more information about the init() and cleanup() please refer to our documentation.

init() and cleanup() for a single test suite

Putting some code into init() and cleanup() functions is often a first step to increase readability and maintainability of your test case. If the body of those functions is the same across different test case it might be a good idea to move it from test case to the test suite level.

code duplication

In this example we will focus on the init() function but you can apply the following steps to the cleanup() as well:

  1. Create script suite_init.py for your init() function in the Test Suite Resources.
  2. Implement the init() function in the suite_init.py script file.
    suite_init
  3. Source the suite_init.py in your test cases.
    source_suite_init

With this approach you can share the init() and cleanup() functions across one test suite. You don’t need to call them as Squish automatically detects if those functions are available and calls them when expected.

init() and cleanup() for a set of test suites

Let’s take it to an even higher level and share our functions between multiple test suites.

If your test suites test different parts of the same application but require the same environment setup this solution might be what you are looking for.

In addition we will make possible to have a custom initialization per test suite or even test case. This is important when only part of your tests requires certain setup actions but you don’t want to move those tests to a separate test suite.

  1. Create global_init.py in the Global Scripts
  2. Implement the init(is_global=True) function in the global_init.py
    global_init

    Please note that in lines 7 and 8 we try to source the suite_init.py file and call the init() function implemented inside. I case of lack of this function a parameter “is_global” protects us from infinite recursion. An except block checks if the file is available at all. Is so, it adds a proper log entry. If the exception reason is different it’s propagated further.
  3. Create suite_init.py in the Test Suite Resources.
  4. Implement the init(is_global=False) function in the suite_init.py.
    suite_init_for_global_approach

And now you can source either the suite_init.py or global_init.py in your test case :

  1. Sourcing the global_init.py will execute initialization method implemented in the Global Scripts. In addition it will execute the initialization method implemented in the suite_init.py.
    source_global_init
    In case of lack of the suite_init.py or the init() method inside, the proper log entry will be added.
    source_global_init_missing_suite_init
  2. Sourcing the suite_init.py will execute only initialization method implemented in the Test Suite Resources.
    source_suite_init_global_approach

The following ZIP package contains an example implementation of the global init() and cleanup() functions. The example is prepared for the Addressbook application which is delivered within Squish packages.
ExampleImplementation.zip

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*