0
0
PyTesttesting~10 mins

Why parametrize multiplies test coverage in PyTest - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test uses pytest's parametrize feature to run the same test logic with different inputs. It verifies that the function correctly identifies even numbers for multiple cases, increasing test coverage efficiently.

Test Code - pytest
PyTest
import pytest

def is_even(num):
    return num % 2 == 0

@pytest.mark.parametrize("input_num,expected", [
    (2, True),
    (3, False),
    (0, True),
    (-4, True),
    (7, False)
])
def test_is_even(input_num, expected):
    assert is_even(input_num) == expected
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts with input_num=2 and expected=TrueTest environment ready, function is_even loadedCheck if is_even(2) == TruePASS
2Test runs with input_num=3 and expected=FalseSame test function reused with new parametersCheck if is_even(3) == FalsePASS
3Test runs with input_num=0 and expected=TrueTest function called again with zero inputCheck if is_even(0) == TruePASS
4Test runs with input_num=-4 and expected=TrueNegative even number testedCheck if is_even(-4) == TruePASS
5Test runs with input_num=7 and expected=FalseOdd number tested lastCheck if is_even(7) == FalsePASS
Failure Scenario
Failing Condition: If is_even returns wrong result for any input
Execution Trace Quiz - 3 Questions
Test your understanding
What does pytest parametrize do in this test?
ASkips tests that fail
BRuns different test functions automatically
CRuns the same test multiple times with different inputs
DRuns tests only once
Key Result
Using parametrize in pytest lets you run one test with many inputs, multiplying coverage without writing many test functions.