0
0
PyTesttesting~10 mins

Conftest.py purpose in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks that a fixture defined in conftest.py is properly shared and used in a test file. It verifies that the fixture runs and provides the expected value to the test.

Test Code - pytest
PyTest
import pytest

# conftest.py
@pytest.fixture
def sample_fixture():
    return "hello from fixture"

# test_sample.py

def test_using_fixture(sample_fixture):
    assert sample_fixture == "hello from fixture"
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1pytest starts test discovery and finds test_using_fixture in test_sample.pypytest environment ready, conftest.py fixture available-PASS
2pytest loads conftest.py and registers sample_fixture fixturefixture sample_fixture is ready to be injected-PASS
3pytest runs test_using_fixture and injects sample_fixturetest_using_fixture receives 'hello from fixture' as sample_fixture valueassert sample_fixture == 'hello from fixture'PASS
4pytest completes test run and reports resultstest_using_fixture passed successfully-PASS
Failure Scenario
Failing Condition: conftest.py is missing or sample_fixture fixture is not defined correctly
Execution Trace Quiz - 3 Questions
Test your understanding
What is the main purpose of conftest.py in pytest?
ATo store test data files
BTo configure the pytest command line options
CTo define fixtures shared across multiple test files
DTo write the actual test functions
Key Result
Using conftest.py to define fixtures allows sharing setup code across tests without importing, making tests cleaner and easier to maintain.