0
0
PyTesttesting~8 mins

Asserting log messages in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Asserting log messages
Folder Structure
tests/
├── test_example.py       # Test files
├── conftest.py           # Fixtures and hooks
utils/
├── logger.py             # Logger setup and helpers
pytest.ini               # Pytest configuration
requirements.txt         # Dependencies
Test Framework Layers
  • Logger Setup Layer: Defines logging configuration and helpers (e.g., utils/logger.py).
  • Test Layer: Contains test cases that use pytest and assert log messages (tests/test_example.py).
  • Fixtures Layer: Provides pytest fixtures and hooks for capturing logs (conftest.py).
  • Configuration Layer: pytest.ini or pyproject.toml to configure pytest behavior.
Configuration Patterns
  • pytest.ini: Configure log capturing and log level.
    [pytest]
    log_cli = true
    log_cli_level = INFO
    
  • Logger Setup: Centralize logger creation in utils/logger.py to keep consistent format and level.
  • Environment Handling: Use environment variables or pytest command line options to adjust log levels if needed.
Test Reporting and CI/CD Integration
  • Pytest captures logs and shows them on test failure or when configured with log_cli=true.
  • Integrate pytest with CI tools (GitHub Actions, Jenkins) to run tests and collect logs for debugging.
  • Use pytest plugins like pytest-html to generate reports including captured logs.
Best Practices
  • Use pytest's built-in log capturing: Use the caplog fixture to capture and assert log messages in tests.
  • Centralize logger configuration: Keep logger setup in one place to maintain consistent log format and level.
  • Assert specific log messages: Check for exact or partial log message content to verify correct logging behavior.
  • Keep tests independent: Avoid relying on global log state; use fixtures to isolate log capture per test.
  • Configure log level appropriately: Use INFO or DEBUG levels during development, WARN or ERROR for CI runs to reduce noise.
Self Check

Where in this framework structure would you add a new test that verifies a warning log message is generated when invalid input is given?

Key Result
Use pytest's caplog fixture and centralized logger setup to assert log messages reliably.