0
0
PyTesttesting~15 mins

Why parametrize multiplies test coverage in PyTest - Automation Benefits in Action

Choose your learning style9 modes available
Verify addition function with multiple input sets
Preconditions (2)
Step 1: Open the test file containing the addition function test
Step 2: Run the test with input values (1, 2) expecting 3
Step 3: Run the test with input values (0, 0) expecting 0
Step 4: Run the test with input values (-1, 1) expecting 0
✅ Expected Result: All tests pass for each input set, confirming the addition function works correctly for multiple cases
Automation Requirements - pytest
Assertions Needed:
Assert the addition function returns the correct sum for each input pair
Best Practices:
Use @pytest.mark.parametrize to run the same test with different inputs
Keep tests small and focused
Use descriptive test names
Automated Solution
PyTest
import pytest

def add(a, b):
    return a + b

@pytest.mark.parametrize("a,b,expected", [
    (1, 2, 3),
    (0, 0, 0),
    (-1, 1, 0)
])
def test_add(a, b, expected):
    result = add(a, b)
    assert result == expected, f"Expected {expected} but got {result}"

This test uses @pytest.mark.parametrize to run test_add three times with different inputs.

Each time, it checks if the add function returns the expected sum.

This multiplies test coverage by testing multiple cases with one test function, making tests concise and clear.

Common Mistakes - 3 Pitfalls
Writing separate test functions for each input set
Not asserting the expected result properly
Using complex logic inside the test instead of simple inputs
Bonus Challenge

Now add data-driven testing with 3 different input sets including floats and large numbers

Show Hint