0
0
PyTesttesting~10 mins

Parametrize with IDs in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses pytest to run the same test function with different inputs. It uses ids to give each test case a clear name. The test checks if the input number is even.

Test Code - pytest
PyTest
import pytest

@pytest.mark.parametrize(
    "number,expected",
    [
        (2, True),
        (3, False),
        (10, True),
        (7, False)
    ],
    ids=["two", "three", "ten", "seven"]
)
def test_is_even(number, expected):
    assert (number % 2 == 0) == expected
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts with pytest runnerpytest ready to run tests-PASS
2pytest collects test_is_even with 4 parameter sets and IDsTest cases named: test_is_even[two], test_is_even[three], test_is_even[ten], test_is_even[seven]-PASS
3pytest runs test_is_even with number=2, expected=TrueRunning test_is_even[two]Check if 2 % 2 == 0 equals TruePASS
4pytest runs test_is_even with number=3, expected=FalseRunning test_is_even[three]Check if 3 % 2 == 0 equals FalsePASS
5pytest runs test_is_even with number=10, expected=TrueRunning test_is_even[ten]Check if 10 % 2 == 0 equals TruePASS
6pytest runs test_is_even with number=7, expected=FalseRunning test_is_even[seven]Check if 7 % 2 == 0 equals FalsePASS
7All parameterized tests completeAll tests passed with clear IDs-PASS
Failure Scenario
Failing Condition: If the assertion fails for any input, e.g., expecting True but got False
Execution Trace Quiz - 3 Questions
Test your understanding
What does the 'ids' parameter do in pytest parametrize?
AGives each test case a readable name
BChanges the test function name
CSkips some test cases
DRuns tests in parallel
Key Result
Using 'ids' in pytest parametrize helps identify each test case clearly in reports, making debugging easier.