froglogic / Blog / Global initialization and cleanup functions

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 for your init() function in the Test Suite Resources.
  2. Implement the init() function in the script file.
  3. Source the in your test cases.

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 in the Global Scripts
  2. Implement the init(is_global=True) function in the

    Please note that in lines 7 and 8 we try to source the 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 in the Test Suite Resources.
  4. Implement the init(is_global=False) function in the

And now you can source either the or in your test case :

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

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.

1 Comment

[…] note that you can use a global cleanup() and init() to implement this […]

Leave a Reply

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

Copy link
Powered by Social Snap