[pytest]# Minimum pytest version requiredminversion=7.0# Directories to search for teststestpaths=tests# Test file patternspython_files=test_*.py *_test.py# Test class patternspython_classes=Test* *Tests# Test function patternspython_functions=test_*# Default command line optionsaddopts=-ra--strict-markers--strict-config--verbose--cov=myproject--cov-report=html--cov-report=term-missing# Custom markersmarkers=slow:marks tests as slow (deselect with '-m "not slow"')integration:marks tests as integration testsunit:marks tests as unit testssmoke:marks tests as smoke testsdatabase:marks tests requiring database connectionapi:marks tests for API testing# Directories to ignorenorecursedirs=.git .tox dist build *.egg venv node_modules# Warning filtersfilterwarnings=errorignore::UserWarningignore::DeprecationWarning# Logging configurationlog_cli=truelog_cli_level=INFOlog_cli_format=%(asctime)s [%(levelname)s] %(message)slog_cli_date_format=%Y-%m-%d %H:%M:%S
[tool.pytest.ini_options]minversion="7.0"testpaths=["tests"]python_files=["test_*.py","*_test.py"]python_classes=["Test*"]python_functions=["test_*"]addopts=["-ra","--strict-markers","--cov=myproject","--cov-branch","--cov-report=html","--cov-report=term-missing:skip-covered","--cov-fail-under=80",]markers=["slow: marks tests as slow","integration: integration tests","unit: unit tests","smoke: smoke tests",]filterwarnings=["error","ignore::UserWarning",]
importpytest# Session-scoped fixture (runs once per test session)@pytest.fixture(scope="session")defdatabase():"""Provide database connection for entire test session"""db=create_database_connection()yielddbdb.close()# Module-scoped fixture (runs once per test module)@pytest.fixture(scope="module")defapi_client():"""Provide API client for test module"""client=APIClient()yieldclientclient.cleanup()# Function-scoped fixture (default, runs for each test)@pytest.fixturedefsample_data():"""Provide sample data for testing"""return{"id":1,"name":"Test User"}# Autouse fixture (automatically used by all tests)@pytest.fixture(autouse=True)defreset_state():"""Reset application state before each test"""clear_cache()yieldcleanup_resources()# Parametrized fixture@pytest.fixture(params=["sqlite","postgres","mysql"])defdb_type(request):"""Test with multiple database types"""returnrequest.param# Configure pytest hooksdefpytest_configure(config):"""Add custom configuration"""config.addinivalue_line("markers","custom: custom marker description")
# Create tests with markerscat>test_api.py<< 'EOF'import pytest@pytest.mark.unitdef test_data_validation(): assert True@pytest.mark.integrationdef test_api_endpoint(): # Simulated API test assert True@pytest.mark.slow@pytest.mark.integrationdef test_full_workflow(): # Long-running test assert TrueEOF# Run only unit testspytesttest_api.py-munit-v
# Run integration tests excluding slow onespytesttest_api.py-m"integration and not slow"-v
# Run tests with coverage and generate reportspytest--cov=myproject--cov-report=html--cov-report=term-missing
# View coverage report# HTML report will be in htmlcov/index.html# Run with coverage thresholdpytest--cov=myproject--cov-fail-under=80# Generate coverage badgepytest--cov=myproject--cov-report=term--cov-report=html