Consider a test function that uses @pytest.mark.parametrize to run multiple input values. Why does this approach multiply test coverage?
Think about how running a test with different inputs affects the number of scenarios checked.
Parametrization runs the same test logic multiple times with different inputs. This means more scenarios are tested, increasing coverage.
Given the following pytest test code, how many tests will be executed?
import pytest @pytest.mark.parametrize('x', [1, 2]) @pytest.mark.parametrize('y', [3, 4, 5]) def test_sum(x, y): assert x + y > 0
Multiply the number of values in each parametrize decorator.
There are 2 values for x and 3 for y. The total tests run is 2 * 3 = 6.
Which assertion correctly verifies that a parametrized test ran all expected cases?
import pytest @pytest.mark.parametrize('val', [10, 20, 30]) def test_positive(val): assert val > 0
Count how many times the test function name appears in verbose output.
Since the test is parametrized with 3 values, it runs 3 times. The count should be 3.
Why does this parametrized test not increase coverage as expected?
import pytest @pytest.mark.parametrize('a', [1, 2]) def test_example(a): assert a != 0 @pytest.mark.parametrize('b', [3, 4]) def test_example(b): assert b != 0
Check if function names are unique in the test file.
Both functions have the same name, so the second overwrites the first. Only 2 tests run, not 4.
Which mechanism does pytest use internally to multiply test coverage when using @pytest.mark.parametrize?
Think about when pytest creates separate test cases for each parameter.
Pytest creates multiple test calls with different arguments during test collection, so each runs independently.