Recall & Review
beginner
What is the purpose of the
@pytest.mark.parametrize decorator in pytest?It allows you to run the same test function multiple times with different input values, making tests more efficient and easier to maintain.
Click to reveal answer
beginner
How do you specify multiple sets of input values using
@pytest.mark.parametrize?You provide a string of comma-separated parameter names and a list of tuples with values for each test run.
Click to reveal answer
beginner
Example: What will happen when this test runs?<br><pre>import pytest
@pytest.mark.parametrize('x, y, expected', [(1, 2, 3), (4, 5, 9)])
def test_add(x, y, expected):
assert x + y == expected</pre>The test
test_add will run twice: once with (1, 2, 3) and once with (4, 5, 9). Both runs check if x + y equals expected.Click to reveal answer
intermediate
Can
@pytest.mark.parametrize be used to test edge cases and why is this useful?Yes, it helps test many edge cases easily by running the same test with different inputs, improving test coverage without writing many separate tests.
Click to reveal answer
intermediate
What happens if one of the parameter sets in
@pytest.mark.parametrize causes the test to fail?Only that specific test run fails, while others continue. This helps identify which input caused the failure clearly.
Click to reveal answer
What does
@pytest.mark.parametrize do?✗ Incorrect
The decorator runs the same test function multiple times with different sets of parameters.
How do you pass parameters to
@pytest.mark.parametrize?✗ Incorrect
You specify parameter names as a string and provide a list of tuples with values for each test run.
If a test with
@pytest.mark.parametrize fails for one input set, what happens?✗ Incorrect
Each parameter set runs as a separate test case; failure in one does not stop others.
Which of these is a correct use of
@pytest.mark.parametrize?✗ Incorrect
Option C correctly specifies parameter names and a list of tuples with values.
Why is
@pytest.mark.parametrize helpful for testing?✗ Incorrect
It helps write fewer tests by reusing the same test logic with different data.
Explain how to use
@pytest.mark.parametrize to test a function with multiple input sets.Think about how you tell pytest which inputs to use for each test run.
You got /5 concepts.
Describe the benefits of using
@pytest.mark.parametrize in your test suite.Consider how it helps when you want to test many input values without writing many tests.
You got /5 concepts.