Parallel Test Execution and Tauk

Ranvir Chhina
Tauk Blog
Published in
5 min readJul 13, 2022

For large test suites, it is necessary to utilize parallel execution to minimize total execution time. Although unittest framework in Python does not contain a built-in method for parallel execution of test cases, it is possible to conduct parallel execution using packages like multiprocessing.

In a previous blog post, I went over how to do this by running unittest test cases in a separate process and communicating with them before and after the run. In this blog post, I will go further and explain how to integrate Tauk into your multiprocess runs using environment variables.

Writing Base Test Case

Let us utilize the test cases from the previous blog post for the sake of continuity and modify them according to our needs.

We will have a base class. This will be a unittest.TestCase with setUp and tearDown defined.

In this base class, we have made a couple of additions:

  • We are importing TaukConfig and Tauk objects from the tauk package. These will be used to initialize Tauk in your test and provide the necessary contextual and authentication details to generate a test report in the Tauk Web UI.
  • The Tauk object is initialized with a TaukConfig. It has three parameters: the Tauk API token for authentication, project ID for contextualizing the project in which the test report will be stored, and the multiprocess_run boolean switch to declare if test cases are running in a multi-process environment.
  • Finally, we add a line to register the driver for the test case. This allows Tauk to save screenshots and other test artifacts. It also allows Tauk to attach to the unittest lifecycle hooks.

Writing Another Module which Extends the Base Case

Next up, let us write a simple Selenium test case. It will open the Google home page and check for the existence of the Gmail hyperlink.

Again, this test case has very few changes. To recap,

  • We extend the GoogleTestCase class and write a test method. The test method finds the Gmail hyperlink and asserts test success conditions.
  • This method is decorated with a @tauk.observe() decorator. This decorator accepts two parameters.
  • custom_test_name allows you to declare what the Tauk test report should display as the test name for this case in the Tauk Web UI.
  • excluded allows you to set a boolean which will determine if Tauk generates a report for this test case.

Tauk Credentials & API Access

Make sure you substitute the API Token and Project ID in the base class. You can get them by doing the following:

  • API Token: The Tauk API Token can be accessed from the User Settings page. This page has a section called API Access where you can show an existing token or generate a new one.
  • Project ID: On the Tauk Home page, you can see cards for each project showing their project details, health scores, and project ID. You can copy the project ID of your card.

Running Test Case

To run the test case above, you can us the unittest command line interface.

When you execute the above command, you should look out for the following lines in the command output:

These commands are logged by the Tauk package and are indicators that the Tauk initialization worked without any hitches.

Adding Environment Variables (Optional)

Using environment variables to store Tauk credentials has a few advantages.

  • You can manage your credentials in one environment file.
  • You can avoid hard-coding sensitive details in code.
  • You can dynamically update Project ID or API Token when running tests in a CI/CD environment.

To add the environment variables, let us create a new file called .env.

You can load this file in your code by importing the dotenv package from the base test. This can be installed by running the installation command.

After installing the package, you can reference it from the base test.

You can observe the following changes:

  • TaukConfig is no longer necessary as all configuration for Tauk is stored in the environment.
  • load_dotenv() method is run before Tauk initialization. This method takes in the path of the .env file. You can read more about this package at PyPi website.

You can run the test using the unittest command line interface again and verify that the credentials are now supplied from the environment.

Parallel Testing

Let us write another test case so we can demonstrate the parallel execution of the two tests. We will extend the same base class called GoogleTestCase.

This test is mostly similar to the previous test case. The only difference is that we are checking for another hyperlink.

For running these tests in parallel, we will create another module which manages the processes and runs them simultaneously. This module will be called run_with_process.py and is the same as in the previous blog post.

This has two sections

  • run() method: In this section, we create a function that receives a parameter named test. It creates a new unittest.TestSuite(), adds the test case represented by test, and runs this suite.
  • __main__ method: In this section, we create a pool of two workers. The map() function maps the imported unittest.TestCase to the run function and runs them in parallel.

To trigger this multiprocess run, you can simply run this module and it will run both test cases in parallel.

However, before you do this, you need to do one final bit of Tauk environment setup. This will allow Tauk to manage multiple running tests and also allocate the same run ID for them.

Using TaukConfig

In your base test case module, set the multiprocess_run switch to true.

Using Environment Variables

To do this, add a new variable TAUK_MULTI_PROCESS to your .env file and set it to true.

Running Tests in Parallel

You are now good to go in terms of the Tauk setup. To run the above module, you can either use your IDE or execute the following in your terminal.

This should allow you to run both the tests in parallel. If you see the following warning generated in the command output, it means that Tauk is not aware that you are trying to run tests in parallel. If this was the case, I suggest you follow the steps in the previous section again and verify that there are no errors.

Conclusion

In a few minutes of your reading time, this guide teaches you

  • How to integrate Tauk with your python unittest
  • How to run those unittest cases in parallel, and
  • How to make Tauk aware that you are trying to run tests in parallel.

I would like to end here with a caveat. Although parallel execution can decrease test execution time, it often involves a lot of pitfalls (especially when you try to access shared objects). It is usually worth the effort only if your test suite is large enough.

And as always, if you have any questions about this blog post, you can reach out to us by joining our Discord server:

--

--