Python API test automation framework (Part 9) Running tests in parallel
This is the ninth and final 👋 post in our series on how to build an API framework using python.
You can read previous parts below:
- Python API test automation framework (Part 1) Introduction, Setup and Installation
- Python API test automation framework (Part 2) Making HTTP requests
- Python API test automation framework (Part 3) Writing Fluent assertions
- Python API test automation framework (Part 4) Working with XML using LXML
- Python API test automation framework (Part 5) Working with JSON and JsonPath
- Python API test automation framework (Part 6) API response schema validation
- Python API test automation framework (Part 7) Refactoring structure
- Python API test automation framework (Part 8) Adding reporting with report portal
This one is gonna be short and sweet 😉
As the size of your automated test suites grows from 10 to 100 to 1000 automated cases, the only way to scale your automated tests is to run them in parallel.
Often Test automation engineers start thinking about parallel runs too late in the game at which point modifying your automation is a very tedious task. Patterns that work for serial execution sometimes can be a blocker for parallel runs.
If you are completely new to this problem space and terms like concurrency, threads, processes and parallel testing sound like greek, don’t worry 😉, no one knows about it till they really encounter it and solve it for themselves. I promise to write a more detailed blog around this topic in the future. Keep watching this space out for it.
I recommend going through These amazing chapter in MIT Open courseware 6.031 course on Software Construction on:
Done reading? 😇
How can we set up our current framework to run our tests in parallel?
Pytest provides a wonderful plugin called pytest-xdist just for this purpose that makes it quite easy to achieve this
Install below modules via pipenv
# Install pytest-xdist plugin
pipenv install pytest-xdist
pipenv install pytest-xdist"[psutil]"
# After switching to example/09_running_tests_in_parallel branch
Running in parallel 🚀
Run below command to run your cases on multiple cores in your machine.
-n mentions the number of CPU's to send the tests to. You can either give a fixed no like 2, 3 or
auto in case you want pytest to determine the no of processes (as per no of CPU cores)
python -m pytest -n auto
gw0  / gw1  / gw2  / gw3  / gw4  / gw5  in run logs, these indicate different worker processes that pytest-xdist uses to run your tests.
rs/gauravsingh/self/course-api-framework-python, configfile: pytest.ini
plugins: reportportal-5.0.8, xdist-2.2.1, forked-1.3.0
gw0  / gw1  / gw2  / gw3  / gw4  / gw5 
1 failed, 7 passed in 1.63s
For a small test suite like this, the cost of multiprocessing results in longer runtime however this is easily amortized for a large enough test suite.
You can tell pytest to run all cases within a single module or all methods in a Test class within a single process by adding flag
python -m pytest -n auto ./tests --reportportal --dist loadscope
Pytest xdist plugin provides many useful features like:
- running these tests within a boxed subprocess,
- over different platforms,
- getting unique id for the test run and many more.
If you want to learn more, feel free to checkout their github readme as a starting point.
This was the final chapter in this long series, I hope you had as much fun 😏 as I had while putting this up.
Honestly, this helped me revisit a lot of concepts and clarified my understanding of things. Do not forget to checkout the full course on Test automation university (when it comes out).
I have discussed all these concepts with more depth in the videos.
If you found this post useful, Do share it with a friend or colleague and if you have thoughts, I’d be more than happy to chat over at twitter or comments. Until next time. Happy Testing and coding. 😄
Originally published at https://automationhacks.io on February 23, 2021.