0
0
PyTesttesting~8 mins

Checking exception attributes in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Checking exception attributes
Folder Structure
tests/
├── test_example.py
├── conftest.py
utilities/
├── helpers.py
pytest.ini
requirements.txt
Test Framework Layers
  • Tests: Contains test files like test_example.py where tests are written using pytest syntax.
  • Fixtures & Config: conftest.py holds reusable fixtures and setup/teardown code.
  • Utilities: Helper functions or classes in utilities/helpers.py to support tests.
  • Configuration: pytest.ini for pytest settings and options.
Configuration Patterns
  • Use pytest.ini to set default options like markers, test paths, and logging.
  • Manage environment variables or test data in conftest.py fixtures.
  • Use fixtures to provide test data or setup that can be reused across tests.
  • Separate sensitive data (like credentials) from code, use environment variables or secure vaults.
Test Reporting and CI/CD Integration
  • Use pytest built-in reporting with clear pass/fail output.
  • Integrate plugins like pytest-html for detailed HTML reports.
  • Configure CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests on push or pull requests.
  • Fail builds if tests fail to ensure quality gate.
Best Practices for Checking Exception Attributes
  • Use pytest.raises() as a context manager to catch exceptions.
  • Access the exception object via as exc_info to check attributes like message or custom fields.
  • Write clear assertions on exception attributes to verify correct error handling.
  • Keep tests focused: one test per exception scenario.
  • Use descriptive test names to indicate which exception attribute is checked.
Self Check

Where would you add a new test that verifies a custom attribute of a raised exception in this framework structure?

Key Result
Use pytest's context manager to catch exceptions and assert their attributes clearly in organized test files.