0
0
PyTesttesting~15 mins

Multiple parameters in PyTest - Build an Automation Script

Choose your learning style9 modes available
Test addition function with multiple input pairs
Preconditions (1)
Step 1: Call add(1, 2) and verify the result is 3
Step 2: Call add(0, 0) and verify the result is 0
Step 3: Call add(-1, 1) and verify the result is 0
Step 4: Call add(100, 200) and verify the result is 300
✅ Expected Result: The add function returns the correct sum for each input pair
Automation Requirements - pytest
Assertions Needed:
Assert that add(a, b) returns the expected sum for each input pair
Best Practices:
Use @pytest.mark.parametrize to run the test with multiple input sets
Write clear assertion messages
Keep test function simple and focused
Automated Solution
PyTest
import pytest

# Function to test
def add(a, b):
    return a + b

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

This test uses @pytest.mark.parametrize to run the test_add_multiple_parameters function multiple times with different inputs.

Each time, it calls the add function with parameters a and b, then checks if the result matches the expected sum.

The assertion includes a message to help understand failures clearly.

Common Mistakes - 3 Pitfalls
Not using @pytest.mark.parametrize and writing separate test functions for each input
Missing assertion messages
Using print statements instead of assertions
Bonus Challenge

Now add data-driven testing with 3 additional input pairs including floats and zeros

Show Hint