Example Configuration
A standard professional setup includes configuration files such as .pytest.ini, conftest.py, and placeholder tests like test_example.py.
The .pytest.ini file dictates the overarching behavior of the testing framework across the entire repository. Rather than forcing you to memorize complex command-line arguments, this initialization file hardcodes best practices into the execution environment.
[pytest]
norecursedirs =
lab
*.egg
.eggs
dist
build
docs
.tox
.git
__pycache__
doctest_optionflags =
NUMBER
NORMALIZE_WHITESPACE
IGNORE_EXCEPTION_DETAIL
addopts =
--strict-markers
--tb=short
--doctest-modules
--doctest-continue-on-failure
--maxfail=2 -rf
testpaths = testsThe conftest.py file serves as a centralized hub for dynamic testing configurations and shared resources.
This file acts as a local plugin for the directory in which it resides.
Functions and data structures defined within conftest.py automatically become available to all test files within that directory and its subdirectories, eliminating the need for repetitive import statements.
import os
import pytest
from lab import enable_logging
TEST_DIR = os.path.dirname(__file__)
TMP_DIR = os.path.join(TEST_DIR, "tmp")
FILE_DIR = os.path.join(TEST_DIR, "files")
def pytest_sessionstart(session):
r"""Called after the Session object has been created and
before performing collection and entering the run test loop.
"""
os.makedirs(TMP_DIR, exist_ok=True)
@pytest.fixture
def test_dir():
return os.path.abspath(TEST_DIR)
@pytest.fixture
def tmp_dir():
return os.path.abspath(TMP_DIR)
@pytest.fixture
def file_dir():
return os.path.abspath(FILE_DIR)
@pytest.fixture(scope="session", autouse=True)
def turn_on_logging():
enable_logging(10)A well-architected conftest.py file abstracts away the boilerplate logic that often frustrates beginners.
For instance, the provided template imports the os and pytest modules to establish absolute path constants like TEST_DIR, TMP_DIR, and FILE_DIR.
It leverages the pytest_sessionstart hook to automatically create these temporary directories on the local file system before any tests execute.
The template then defines explicit fixtures that return these paths, allowing you to seamlessly read and write files during their tests without wrestling with relative path resolution.
Furthermore, the inclusion of a turn_on_logging fixture with the autouse=True parameter guarantees that diagnostic logging is automatically enabled for every single test session, providing immediate visibility into system behavior without requiring you to invoke the logger manually.
Finally, templates often include a test_example.py file containing a trivial assertion, such as verifying that a specific string matches an identical string literal.
While this may seem extraneous, it serves a critical infrastructural purpose.
Continuous Integration (CI) pipelines are typically configured to fail if no tests are discovered.
The inclusion of this placeholder test guarantees that the automated CI pipeline successfully completes its execution during the initial onboarding phase, preventing spurious pipeline failures before we have written our own domain-specific tests.
def test_example():
example_str = "Test example so CI does not fail"
assert example_str == "Test example so CI does not fail"