0
0
PyTesttesting~10 mins

Conftest fixtures (shared across files) in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses a shared fixture defined in conftest.py to provide a sample user dictionary. It verifies that the test function receives the correct data from the fixture.

Test Code - PyTest
PyTest
import pytest

# conftest.py
@pytest.fixture

def sample_user():
    return {"name": "Alice", "age": 30}

# test_user.py

def test_user_name(sample_user):
    assert sample_user["name"] == "Alice"
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1PyTest discovers the fixture 'sample_user' in conftest.pyPyTest test runner ready to run tests in test_user.py-PASS
2PyTest starts test 'test_user_name' and injects 'sample_user' fixtureTest function receives dictionary {"name": "Alice", "age": 30}-PASS
3Test asserts that sample_user["name"] equals "Alice"sample_user["name"] is "Alice"assert sample_user["name"] == "Alice"PASS
4Test completes successfullyTest runner marks test as passed-PASS
Failure Scenario
Failing Condition: The fixture returns incorrect data or the test asserts wrong expected value
Execution Trace Quiz - 3 Questions
Test your understanding
Where is the fixture 'sample_user' defined so it can be shared across multiple test files?
AIn a separate test file
BInside the test function
CIn conftest.py
DIn the main application code
Key Result
Using conftest.py to define fixtures allows sharing setup code across multiple test files, making tests cleaner and easier to maintain.