What if you could run dozens of tests with just one simple function?
Why @pytest.mark.parametrize decorator? - Purpose & Use Cases
Imagine you have a function that needs to be tested with many different inputs and expected results. You write separate test functions for each case, copying and changing code repeatedly.
This manual way is slow and boring. You might forget to update some tests or make mistakes copying code. Running many tests means a lot of repeated work and hard-to-read code.
The @pytest.mark.parametrize decorator lets you write one test function and run it multiple times with different inputs automatically. It keeps your code clean and saves time.
def test_add_1(): assert add(1, 2) == 3 def test_add_2(): assert add(3, 4) == 7
import pytest @pytest.mark.parametrize('a,b,expected', [(1,2,3), (3,4,7)]) def test_add(a, b, expected): assert add(a, b) == expected
You can easily test many input cases with less code and fewer mistakes, making your tests faster and clearer.
Testing a calculator app with many operations and numbers becomes simple and organized using @pytest.mark.parametrize.
Manual tests for many inputs are slow and error-prone.
@pytest.mark.parametrize runs one test with many inputs automatically.
This saves time, reduces mistakes, and keeps tests clean.
Practice
@pytest.mark.parametrize decorator in pytest?Solution
Step 1: Understand the decorator's role
@pytest.mark.parametrizeallows running one test function multiple times with different inputs.Step 2: Compare options with this role
Only To run the same test function multiple times with different input values describes running the same test with different inputs, matching the decorator's purpose.Final Answer:
To run the same test function multiple times with different input values -> Option BQuick Check:
Parametrize = multiple inputs [OK]
- Confusing parametrize with skipping tests
- Thinking it groups tests instead of repeating one
- Mixing it up with marking expected failures
@pytest.mark.parametrize for testing a function with inputs 1 and 2?Solution
Step 1: Check correct decorator name and syntax
The correct decorator is@pytest.mark.parametrizewith parameters as strings and list of values.Step 2: Validate each option
@pytest.mark.parametrize('input', [1, 2]) uses correct spelling, quotes around parameter name, and list syntax. Others have syntax errors or wrong names.Final Answer:
@pytest.mark.parametrize('input', [1, 2]) -> Option DQuick Check:
Correct spelling and syntax = @pytest.mark.parametrize('input', [1, 2]) [OK]
- Misspelling 'parametrize' as 'parametrized'
- Forgetting quotes around parameter name
- Using wrong brackets or missing @ symbol
import pytest
@pytest.mark.parametrize('x,y', [(1,2), (3,4)])
def test_sum(x, y):
assert x + y == 3Solution
Step 1: Analyze test cases and assertion
Test runs twice: (1,2) and (3,4). Assertion checks if sum equals 3.Step 2: Evaluate each case
1+2=3 passes; 3+4=7 fails assertion.Final Answer:
First test passes, second test fails -> Option AQuick Check:
Sum check true only for (1,2) [OK]
- Assuming all tests pass without checking values
- Confusing tuple unpacking in parametrize
- Ignoring assertion logic
@pytest.mark.parametrize:@pytest.mark.parametrize('num', [1, 2, 3])
def test_even():
assert num % 2 == 0Solution
Step 1: Check function parameters
Test function must accept the parameter named in parametrize ('num'), but test_even() has no parameters.Step 2: Confirm other parts
Decorator name and list syntax are correct; assertion logic is valid for even check.Final Answer:
Missing parameter in test function definition -> Option CQuick Check:
Function must accept parametrize variables [OK]
- Forgetting to add parameters to test function
- Thinking list must be tuple
- Confusing assertion logic with syntax error
is_valid_age(age) that returns True if age is between 18 and 65 inclusive. Which @pytest.mark.parametrize usage correctly tests ages 17, 18, 30, 65, and 70 with expected results?Solution
Step 1: Understand parametrize syntax for multiple parameters
When passing multiple parameters, use a string with comma-separated names or a list of strings as first argument, and a list of tuples as second.Step 2: Evaluate each option
A: @pytest.mark.parametrize(age,expected, [(17, False), (18, True), (30, True), (65, True), (70, False)]) misses quotes around parameter names, causing error. B has wrong syntax with multiple arguments. C uses a dict, which is invalid. D: @pytest.mark.parametrize(['age', 'expected'], [(17, False), (18, True), (30, True), (65, True), (70, False)]) uses list of strings correctly.Step 3: Choose best practice
@pytest.mark.parametrize(['age', 'expected'], [(17, False), (18, True), (30, True), (65, True), (70, False)]) is correct and clear syntax for multiple parameters.Final Answer:
@pytest.mark.parametrize(['age', 'expected'], [(17, False), (18, True), (30, True), (65, True), (70, False)]) -> Option AQuick Check:
List of param names + list of tuples = @pytest.mark.parametrize(['age', 'expected'], [(17, False), (18, True), (30, True), (65, True), (70, False)]) [OK]
- Passing multiple parameter lists separately
- Using dict instead of list of tuples
- Confusing string vs list for parameter names
Ready to prove it?
Pass the test and this pattern moves unknown
