0
0
PyTesttesting~10 mins

Parametrize with indirect fixtures in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses pytest to run the same test function multiple times with different inputs. It uses an indirect fixture to prepare the input before the test runs. The test checks if the input string is converted to uppercase correctly.

Test Code - pytest
PyTest
import pytest

@pytest.fixture
def input_data(request):
    # Prepare the input by converting to uppercase
    return request.param.upper()

@pytest.mark.parametrize('input_data', ['hello', 'world'], indirect=True)
def test_uppercase(input_data):
    assert input_data.isupper(), f"Expected uppercase but got {input_data}"
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startspytest test runner initialized-PASS
2pytest collects test_uppercase with parametrize inputs ['hello', 'world'] and indirect=TrueTest function identified with two parameter sets-PASS
3For first parameter 'hello', pytest calls fixture input_data(request) with request.param='hello'Fixture input_data prepares 'HELLO'-PASS
4test_uppercase runs with input_data='HELLO'Test function receives 'HELLO'Check if 'HELLO'.isupper() is TruePASS
5For second parameter 'world', pytest calls fixture input_data(request) with request.param='world'Fixture input_data prepares 'WORLD'-PASS
6test_uppercase runs with input_data='WORLD'Test function receives 'WORLD'Check if 'WORLD'.isupper() is TruePASS
7Test ends with all parameter sets passedTest report shows 2 passed tests-PASS
Failure Scenario
Failing Condition: Fixture input_data does not convert string to uppercase correctly
Execution Trace Quiz - 3 Questions
Test your understanding
What does the 'indirect=True' parameter do in pytest.mark.parametrize?
AIt skips the test for that parameter
BIt runs the test only once ignoring parameters
CIt tells pytest to pass the parameter value to a fixture instead of directly to the test function
DIt marks the test as expected to fail
Key Result
Using indirect fixtures with parametrize allows you to preprocess or prepare test inputs dynamically before the test runs, making tests more flexible and reusable.