0
0
Testing Fundamentalstesting~15 mins

Parallel test execution in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Run two simple tests in parallel and verify both pass
Preconditions (2)
Step 1: Create two test functions: test_addition and test_subtraction
Step 2: In test_addition, assert that 2 + 3 equals 5
Step 3: In test_subtraction, assert that 5 - 3 equals 2
Step 4: Run both tests in parallel using pytest with the -n 2 option
✅ Expected Result: Both tests run at the same time and pass successfully
Automation Requirements - pytest with pytest-xdist
Assertions Needed:
Assert 2 + 3 == 5 in test_addition
Assert 5 - 3 == 2 in test_subtraction
Best Practices:
Use pytest fixtures if needed for setup
Use pytest-xdist for parallel execution
Keep tests independent to avoid interference
Use clear and simple assertions
Automated Solution
Testing Fundamentals
import pytest


def test_addition():
    assert 2 + 3 == 5


def test_subtraction():
    assert 5 - 3 == 2

# To run these tests in parallel, use the command:
# pytest -n 2

This script defines two simple test functions using pytest. Each test checks a basic math operation with an assertion.

We use pytest -n 2 to run both tests at the same time in parallel using the pytest-xdist plugin.

This approach helps speed up test execution by running tests concurrently. The tests are independent, so they do not affect each other.

Common Mistakes - 3 Pitfalls
{'mistake': 'Running tests sequentially without using pytest-xdist', 'why_bad': 'Tests run one after another, so no speed benefit from parallel execution.', 'correct_approach': "Install pytest-xdist and run tests with 'pytest -n 2' to enable parallel execution."}
Writing tests that share state or data without isolation
Using print statements instead of assertions
Bonus Challenge

Now add a third test for multiplication and run all three tests in parallel

Show Hint