filmov
tv
How To Run Pytest Tests In Parallel (2 Min) | Python Unit Testing

Показать описание
In this tutorial, you'll learn how to run Python Pytest Tests in parallel.
—
—
Video Transcript:
—
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn how to run Python tests in parallel using PI tet. Let's start by clicking on the Python packages tab and ensure that the PI test and PI test dash x disk packages are installed in the tests folder.
I have two test files containing two tests each. In the first test file I'm testing the cash sales report and asserting whether the keyword select appears in the first query and the keyword XYZ appears in the second query or not. Similarly in the second test file, I'm testing the lead report and I'm asserting wether the select and XYZ keywords appear in the first query and the second query respectively.
To run these tests sequentially right click on the tests folder then hit run the PI test. Since we have four tests in total and every test will sleep for two seconds, the total execution time should be around 8 seconds. To run these tests in parallel click on the PI test dropdown and then hit edit configuration.
In the additional argument, the section enters dash n4. Here 4 represents the number of CPU cores you want to test to spread across. Click ok to exit then hit the run button to run these tests in parallel. As you can see the execution time for all four tests was only two seconds.
If you run the test again it should only take around 2 seconds. If I reduce the number of CPU cores to 2 then it should take around 4 seconds. To export, the test results click on the export test results button.
One thing to note is that if you run tests in parallel then the ordering of tests will be off as you can imagine. There you have it. Make sure you like, subscribe, and turn on the notification bell.
Until next time.
[pytest]
addopts = -n2
—
—
Video Transcript:
—
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn how to run Python tests in parallel using PI tet. Let's start by clicking on the Python packages tab and ensure that the PI test and PI test dash x disk packages are installed in the tests folder.
I have two test files containing two tests each. In the first test file I'm testing the cash sales report and asserting whether the keyword select appears in the first query and the keyword XYZ appears in the second query or not. Similarly in the second test file, I'm testing the lead report and I'm asserting wether the select and XYZ keywords appear in the first query and the second query respectively.
To run these tests sequentially right click on the tests folder then hit run the PI test. Since we have four tests in total and every test will sleep for two seconds, the total execution time should be around 8 seconds. To run these tests in parallel click on the PI test dropdown and then hit edit configuration.
In the additional argument, the section enters dash n4. Here 4 represents the number of CPU cores you want to test to spread across. Click ok to exit then hit the run button to run these tests in parallel. As you can see the execution time for all four tests was only two seconds.
If you run the test again it should only take around 2 seconds. If I reduce the number of CPU cores to 2 then it should take around 4 seconds. To export, the test results click on the export test results button.
One thing to note is that if you run tests in parallel then the ordering of tests will be off as you can imagine. There you have it. Make sure you like, subscribe, and turn on the notification bell.
Until next time.
[pytest]
addopts = -n2