How to Speed Up Your Tests With Parallelization
Intro
Compared to Unit and Integration Tests, Selenium is slow. A single test can easily take minutes to run. And when you have a lot of them, it can quickly compound your suite to take hours to complete (just for a single browser) -- hampering your ability to get fast feedback.
A Solution
Rather than run your tests in series (which is what you get out of the box with Selenium) you can dramatically speed things up by running them in parallel. Couple this with the ability to run your tests in any browser, and you're on to some solid footing with your test automation.
But keep in mind that doing this requires some consideration into how you structure your tests, but the pay off is well worth the effort.
A brief primer on Good Test Writing
In order to write tests that work well in parallel there are some simple guidelines to follow:
- Write atomic and autonomous tests
- Group like tests together in small batches
- Be descriptive
- Use a Test Runner
Atomic & Autonomous Tests
Each test needs to be concise (e.g. testing a single feature rather than multiple features) and be able to be run independently (e.g. sets up it's own data rather than relying on a previous test to do it). Doing this may require a mental shift, discipline, and more up front effort. But it will make a dramatic impact on the quality, effectiveness, and maintainability of your tests.
Grouping Tests
As your test suite grows you should have multiple test files, each containing a small grouping of tests broken out by functionality that they're exercising. This will go a long way towards organization and maintenance as your test suite grows -- as well as faster execution times (depending on your approach to parallelization).
Being Descriptive
Each test file should be named appropriately, and each test within it should have an informative name (even if it may be a bit verbose). Also, each test (or grouping of tests) should be tagged for flexible execution later (e.g. on a Continuous Integration server).
This way you can run parts of your test suite as needed, and the results will be informative thanks to helpful naming.
Test Runners
At the heart of every testing framework is a test runner that does a lot of the heavy lifting (e.g. test group execution, easy global configuration for setup and teardown, reporting, etc.).
So rather than reinvent the wheel, you can use one of the many that already exists (there's more than one for every language). And with it, you can bolt on third party libraries to extend it's functionality if there's something missing -- like parallelization.
Example
For this example we are using the test code from a previous tip on working with tables. Don't get too distracted by what the test steps are doing, the organization and naming of things is more important.
Most test runners run tests in parallel at the file (or class) level, so we break our tests up into two files: one for the table with attributes, and one for the table without them. And we move our setup and teardown into a central place, so each test file only has its tests in it.
Each test gets its own browser, so the tests don't share any state and can safely run at the same time. Then we tell the test runner to run them in parallel (2 at a time in these examples).
Expected Behavior
- Two browsers open in parallel
- Each test runs in a different browser
- All browsers close
If you combine this with running your tests in Sauce Labs, you can run many more tests at the same time, and the test results (along with a video recording, screenshots, and other debugging information) are available on the test results dashboard.
Summary
Hopefully this tip has helped steer you on a path towards better test writing and parallelization.
Happy Testing!