Share fixtures among test files: conftest.py



examples/pytest/autouse/conftest.py
import pytest

@pytest.fixture(autouse = True, scope="session")
def fix_session():
    print("\nSession setup")
    yield
    print("\nSession teardown")


@pytest.fixture(autouse = True, scope="module")
def fix_module():
    print("\n  Module setup")
    yield
    print("\n  Module teardown")


@pytest.fixture(autouse = True, scope="function")
def fix_function():
    print("\n    Function setup")
    yield
    print("\n    Function teardown")

examples/pytest/autouse/test_blue.py
def test_one():
    print("      Test Blue one")
    assert True


def test_two():
    print("      Test Blue two")
    assert False

examples/pytest/autouse/test_green.py
def test_three():
    print("      Test Green Three")
    assert True

pytest -qs



Session setup

  Module setup
    Function setup
      Test Blue one
    Function teardown

    Function setup
      Test Blue two
    Function teardown
  Module teardown

  Module setup
    Function setup
      Test Green Three
    Function teardown
  Module teardown

Session teardown