0
0
PyTesttesting~10 mins

Fixture factories in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses a fixture factory to create user objects with different names. It verifies that the created user has the expected name.

Test Code - pytest
PyTest
import pytest

@pytest.fixture
def user_factory():
    def create_user(name):
        return {"name": name}
    return create_user

def test_user_creation(user_factory):
    user = user_factory("Alice")
    assert user["name"] == "Alice"

    user2 = user_factory("Bob")
    assert user2["name"] == "Bob"
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startspytest test runner initialized-PASS
2Fixture user_factory is called to get the factory functionuser_factory fixture returns create_user function-PASS
3test_user_creation calls user_factory("Alice") to create useruser dictionary {'name': 'Alice'} createdassert user["name"] == "Alice"PASS
4test_user_creation calls user_factory("Bob") to create another useruser dictionary {'name': 'Bob'} createdassert user2["name"] == "Bob"PASS
5Test ends successfullyAll assertions passed-PASS
Failure Scenario
Failing Condition: The user_factory returns a user with a wrong name or the assertion compares to a wrong expected name
Execution Trace Quiz - 3 Questions
Test your understanding
What does the fixture factory user_factory return?
AA string with the user name
BA user dictionary with a fixed name
CA function that creates user dictionaries with a given name
DA pytest test function
Key Result
Using fixture factories allows creating flexible test data with different parameters, improving test reusability and clarity.