What if you could test many inputs with just one simple test function?
Why Single parameter in PyTest? - Purpose & Use Cases
Imagine you have a function that needs to be tested with many different input values. You write a separate test for each input by hand, repeating almost the same code again and again.
This manual way is slow and boring. You might forget to test some inputs or make mistakes copying code. It's hard to keep tests organized and update them when the function changes.
Using a single parameter with pytest's @pytest.mark.parametrize lets you write one test that runs many times with different inputs automatically. This saves time, reduces errors, and keeps tests clean.
def test_add(): assert add(1) == 2 assert add(2) == 3 assert add(3) == 4
import pytest @pytest.mark.parametrize('input', [1, 2, 3]) def test_add(input): assert add(input) == input + 1
You can easily test many input cases with one simple test, making your testing faster and more reliable.
Testing a calculator app's add function with many numbers without writing repetitive tests for each number.
Manual repetitive tests are slow and error-prone.
Single parameter testing runs one test many times with different inputs.
This makes tests easier to write, read, and maintain.
Practice
Solution
Step 1: Understand pytest parameterization
Pytest allows passing parameters to test functions to run the same test with different inputs.Step 2: Role of a single parameter
A single parameter lets you supply multiple values to the test, so it runs multiple times with each value.Final Answer:
To pass different values to the test function for repeated testing -> Option DQuick Check:
Single parameter = multiple test runs [OK]
- Thinking single parameter defines multiple tests
- Confusing parameter with skipping tests
- Assuming single parameter runs test once only
Solution
Step 1: Check parameter name format
The parameter name must be a string inside quotes, e.g., 'num'.Step 2: Check values format
The values must be inside a list or iterable, e.g., [1, 2, 3].Final Answer:
@pytest.mark.parametrize('num', [1, 2, 3]) -> Option CQuick Check:
Parametrize syntax = string + list [OK]
- Not quoting the parameter name
- Passing values without list brackets
- Using wrong argument structure
import pytest
@pytest.mark.parametrize('x', [2, 4])
def test_double(x):
assert x * 2 == 4Solution
Step 1: Evaluate first test with x=2
2 * 2 = 4, assertion is true, test passes.Step 2: Evaluate second test with x=4
4 * 2 = 8, assertion 8 == 4 is false, test fails.Final Answer:
First test passes, second test fails -> Option AQuick Check:
2*2=4 pass, 4*2=8 fail [OK]
- Assuming all tests pass without checking values
- Mixing up which input causes failure
- Ignoring assertion logic
import pytest
@pytest.mark.parametrize('val', 1, 2, 3)
def test_positive(val):
assert val > 0Solution
Step 1: Check parametrize values format
Values must be inside a list or iterable, e.g., [1, 2, 3].Step 2: Identify missing brackets
Code passes values as separate arguments instead of a single list, causing syntax error.Final Answer:
Missing list brackets around parameter values -> Option BQuick Check:
Values need list brackets [OK]
- Passing values without brackets
- Removing quotes from parameter name
- Wrong assertion operator
def test_is_uppercase(s):
assert s.isupper()Solution
Step 1: Understand parameterizing tests
Using @pytest.mark.parametrize allows running the test multiple times with different inputs.Step 2: Apply parametrize decorator with string list
Adding @pytest.mark.parametrize('s', ['HELLO', 'WORLD']) runs test_is_uppercase twice with each string.Final Answer:
Add @pytest.mark.parametrize('s', ['HELLO', 'WORLD']) above the function -> Option AQuick Check:
Parametrize decorator runs test with multiple inputs [OK]
- Trying to pass multiple parameters instead of one
- Using loops inside test instead of parametrize
- Calling test manually instead of using pytest features
Ready to prove it?
Pass the test and this pattern moves unknown
