0
0
PyTesttesting~3 mins

Why Conftest.py purpose in PyTest? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how one simple file can save you hours of repetitive test setup!

The Scenario

Imagine you have many test files, and each one needs the same setup steps like opening a browser or preparing test data.

You write these steps again and again inside every test file.

The Problem

This manual way is slow and boring.

You might forget to update one test file when the setup changes.

It causes mistakes and wastes time.

The Solution

Conftest.py lets you write setup code once and share it with all your tests automatically.

This keeps your tests clean and easy to maintain.

Before vs After
Before
def test_one():
    setup()
    assert something

def test_two():
    setup()
    assert something_else
After
import pytest

@pytest.fixture
def setup():
    # setup code
    pass

# tests just use the fixture without repeating setup
What It Enables

It enables writing DRY (Don't Repeat Yourself) tests that are easier to read and faster to update.

Real Life Example

When testing a website, you can open the browser once in conftest.py and all tests will use it without repeating the code.

Key Takeaways

Conftest.py centralizes shared test setup.

It reduces repeated code and errors.

It makes tests cleaner and easier to maintain.