What are Testerum Hooks?

Testerum supports hooks, which are special functions that run before or after you execute an automated test or a suite of tests. Testerum Hooks allow us to better manage the test automation process and prevent us from creating duplicates.

Why Testerum Hooks?

In the test automation circle, you may have encountered some situations where you had to execute prerequisite steps before any test. Some of these prerequisite steps can be:

In the same manner, there can be also added after steps like:

These are some of the actions we can do before and after any execution, in order to set up our test environment in a certain way. When you cope with these situations, Testerum hooks are a good solution. Testerum supports 4 different hooks:

@BeforeAllTests
will execute once before any tests are executed
@BeforeEachTest
will execute before each test
@AfterEachTest
will execute after each test
@AfterAllTests
will execute once after all the tests have been executed

Let us have a look on some examples.

Before all hook

@BeforeAllTests
This hook will execute a specified action only once before all tests. In this example, a step which does a log in into the application under test is executed once before all the tests available inside the "owners" feature.
Before all hook

Before each hook

@BeforeEachTest
This hook will execute a specified action before each test. In this example, the database is initialized and an SQL script is executed in order to clean all the records and insert only the data that we need. This way we are sure our tests are executed in isolation, they are not dependent upon each other and the data won't be modified outside the test.
Before each hook

After each hook

@AfterEachTest
This hook allows us to leave the environment in a certain a certain state after each test execution. In this exampe, after each test, we clean the database.
After each test

After all hook

@AfterAllTests
This hook will execute only one action after all the tests. It comes in handy for example after all the tests have been executed and you want to send the the reports to an internal system.
After all hook

Tips

In some situations we need to define some variables before any test. It's very useful to define the variables inside a hook and the tests can interact with the variables.
But when it comes down to overriding the variables inside a test we have the following situations: Here is an example:
    Define a variable inside a hook
  1. We start by creating a step "Given I have defined a variable "userName" with the value <<admin>>" which defines a variable. This hook is being executed only once before all the tests inside the feature Hooks example.

  2. Defined variable used
  3. Now we create a test "Example 1" which will use the variable defined by the hook. It will be replaced at runtime with the value "admin".

  4. Overriding a variable
  5. In the next test "Example 2" we override the variable's value "admin" with "wrongUserName". A change inside a test, will not reflect in other tests.

  6. Example 3 where
  7. As we can see in "Example 3", we use again the variable defined inside the hook. This time the variable will have the default value "admin" because the changes in Example 2 test are not reflected in here.