Using a single parameter in pytest helps you run the same test with different inputs easily. It saves time and avoids repeating code.
Single parameter in PyTest
Introduction
Syntax
PyTest
@pytest.mark.parametrize('param', [value1, value2, value3]) def test_example(param): assert some_function(param) == expected_result
The string 'param' is the name of the parameter your test function will receive.
The list contains all the values you want to test with.
Examples
PyTest
@pytest.mark.parametrize('number', [1, 2, 3]) def test_is_positive(number): assert number > 0
PyTest
@pytest.mark.parametrize('word', ['cat', 'dog', 'bird']) def test_word_length(word): assert len(word) > 0
Sample Program
This test runs three times with num = 0, 5, and 10. It checks that each number is zero or positive.
PyTest
import pytest @pytest.mark.parametrize('num', [0, 5, 10]) def test_is_non_negative(num): assert num >= 0
Important Notes
Use simple and clear parameter names to understand what each test value means.
Each value in the list will run the test separately, so you get detailed results for each case.
Summary
Single parameter lets you test one input with many values easily.
It helps avoid repeating similar test code.
Each test run shows if a specific input passed or failed.
Practice
1. What is the purpose of using a single parameter in a pytest test function?
easy
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]
Hint: Single parameter means testing with different inputs [OK]
Common Mistakes:
- Thinking single parameter defines multiple tests
- Confusing parameter with skipping tests
- Assuming single parameter runs test once only
2. Which of the following is the correct syntax to use a single parameter with pytest's @pytest.mark.parametrize decorator?
easy
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]
Hint: Parameter name in quotes, values in list brackets [OK]
Common Mistakes:
- Not quoting the parameter name
- Passing values without list brackets
- Using wrong argument structure
3. What will be the output when running this pytest code?
import pytest
@pytest.mark.parametrize('x', [2, 4])
def test_double(x):
assert x * 2 == 4medium
Solution
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]
Hint: Check each input separately for assertion [OK]
Common Mistakes:
- Assuming all tests pass without checking values
- Mixing up which input causes failure
- Ignoring assertion logic
4. Identify the error in this pytest code using a single parameter:
import pytest
@pytest.mark.parametrize('val', 1, 2, 3)
def test_positive(val):
assert val > 0medium
Solution
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]
Hint: Values must be in a list or tuple [OK]
Common Mistakes:
- Passing values without brackets
- Removing quotes from parameter name
- Wrong assertion operator
5. Given this test function, how can you modify it to test multiple string inputs using a single parameter?
def test_is_uppercase(s):
assert s.isupper()hard
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]
Hint: Use @pytest.mark.parametrize with list of inputs [OK]
Common Mistakes:
- Trying to pass multiple parameters instead of one
- Using loops inside test instead of parametrize
- Calling test manually instead of using pytest features
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
