Recall & Review
beginner
What is the purpose of using a single parameter in pytest test functions?
A single parameter allows you to pass different values to the test function, enabling the same test to run multiple times with different inputs.
Click to reveal answer
beginner
How do you mark a test function to run multiple times with different values for a single parameter in pytest?
Use the @pytest.mark.parametrize decorator with the parameter name and a list of values to test.
Click to reveal answer
beginner
Example: What does this pytest code do?
@pytest.mark.parametrize('x', [1, 2, 3])
def test_example(x):
assert x > 0It runs test_example three times with x equal to 1, 2, and 3. Each time it checks that x is greater than 0.
Click to reveal answer
beginner
Why is using a single parameter with @pytest.mark.parametrize helpful in testing?
It saves time by avoiding writing multiple similar test functions and helps check how code behaves with different inputs.
Click to reveal answer
beginner
What happens if one of the parameter values causes the assertion to fail in a single parameter pytest test?
Only the test run with that failing value will fail; other runs with different values will still pass if their assertions hold.
Click to reveal answer
Which pytest decorator is used to run a test multiple times with different values for a single parameter?
✗ Incorrect
The @pytest.mark.parametrize decorator allows you to specify parameter names and values to run the test multiple times.
How do you specify the parameter name in @pytest.mark.parametrize for a single parameter?
✗ Incorrect
You provide the parameter name as a string, like 'x', followed by a list of values to test.
What will happen if you run this test?
@pytest.mark.parametrize('num', [0, 1, 2])
def test_positive(num):
assert num > 0
✗ Incorrect
The assertion num > 0 fails when num is 0, so that run fails; others pass.
Why might you prefer using a single parameter with @pytest.mark.parametrize instead of writing multiple test functions?
✗ Incorrect
Using parameterization avoids repeating similar test code and helps keep tests clean.
In pytest, what type of values can you pass to a single parameter in @pytest.mark.parametrize?
✗ Incorrect
You can pass any iterable of values to test different inputs.
Explain how to use a single parameter with @pytest.mark.parametrize to run a test multiple times with different inputs.
Think about how to tell pytest to repeat a test with different values.
You got /5 concepts.
Describe what happens when one of the parameter values causes an assertion failure in a pytest test using a single parameter.
Consider how pytest handles multiple test runs with different inputs.
You got /4 concepts.