0
0
PytestHow-ToBeginner ยท 3 min read

How to Parametrize Tests in pytest for Efficient Testing

In pytest, you can parametrize tests using the @pytest.mark.parametrize decorator to run the same test function with different sets of inputs. This helps you test multiple cases without writing separate test functions.
๐Ÿ“

Syntax

The @pytest.mark.parametrize decorator takes two main arguments: a string of comma-separated parameter names, and a list of tuples with values for those parameters. Each tuple represents one test case.

Example: @pytest.mark.parametrize('input,expected', [(1,2), (3,4)]) runs the test twice with input=1, expected=2 and input=3, expected=4.

python
@pytest.mark.parametrize('param1,param2', [(value1, value2), (value3, value4)])
def test_example(param1, param2):
    # test code using param1 and param2
    pass
๐Ÿ’ป

Example

This example shows a test for a simple function that adds 1 to a number. The test runs with multiple inputs and checks the expected output.

python
import pytest

def add_one(x):
    return x + 1

@pytest.mark.parametrize('input,expected', [
    (1, 2),
    (5, 6),
    (0, 1),
    (-1, 0)
])
def test_add_one(input, expected):
    assert add_one(input) == expected
Output
==== test session starts ==== collected 4 items test_sample.py .... [100%] ==== 4 passed in 0.01s ====
โš ๏ธ

Common Pitfalls

  • Forgetting to separate parameter names by commas inside the string (e.g., use 'input,expected' not 'input expected').
  • Passing a list of values instead of a list of tuples when multiple parameters are used.
  • Mismatch between the number of parameters and the tuple length.
  • Using mutable default arguments inside test functions can cause unexpected behavior.
python
import pytest

# Wrong: parameter names missing comma
# @pytest.mark.parametrize('input expected', [(1,2), (3,4)])

# Wrong: values not tuples for multiple params
# @pytest.mark.parametrize('input,expected', [1, 2, 3])

# Correct usage:
@pytest.mark.parametrize('input,expected', [(1,2), (3,4)])
def test_correct(input, expected):
    assert input < expected
๐Ÿ“Š

Quick Reference

Use @pytest.mark.parametrize to run tests with different inputs easily. Always match parameter names and tuple values. Use tuples for multiple parameters. This reduces code duplication and improves test coverage.

FeatureUsageExample
Decorator@pytest.mark.parametrize@pytest.mark.parametrize('x,y', [(1,2), (3,4)])
Parameter namesComma-separated string'input,expected'
ValuesList of tuples[(1,2), (3,4)]
Test function argsMatch parameter namesdef test_func(input, expected):
Multiple testsRuns once per tuple4 tuples = 4 test runs
โœ…

Key Takeaways

Use @pytest.mark.parametrize to run one test with many input sets.
Parameter names must be a comma-separated string matching test function arguments.
Provide a list of tuples with values for each test case.
Ensure tuple length matches the number of parameters exactly.
Parametrization reduces code duplication and improves test clarity.