0
0
PyTesttesting~8 mins

Test modules in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Test modules
Folder Structure of a Pytest Project
project-root/
├── tests/
│   ├── test_login.py
│   ├── test_shopping_cart.py
│   ├── test_checkout.py
│   └── __init__.py
├── src/
│   └── app_code.py
├── conftest.py
├── pytest.ini
└── requirements.txt
  
Test Framework Layers in Pytest
  • Test Modules: Python files starting with test_ or ending with _test.py inside the tests/ folder. Each module groups related test functions.
  • Test Functions: Individual test cases inside test modules. Named starting with test_.
  • Fixtures: Setup and teardown code shared across tests, defined in conftest.py or test modules.
  • Application Code: The actual code under test, usually in src/ or similar folder.
  • Configuration: Files like pytest.ini to configure pytest behavior.
Configuration Patterns for Pytest Test Modules
  • pytest.ini: Central place to configure test discovery, markers, and options.
  • conftest.py: Define fixtures and hooks shared across test modules.
  • Environment Variables: Use os.environ or python-dotenv to manage environment-specific data like URLs or credentials.
  • Command Line Options: Add custom options in conftest.py to select browsers, environments, or test groups.
Test Reporting and CI/CD Integration
  • Built-in pytest reports: Console output shows passed/failed/skipped tests.
  • Plugins: Use plugins like pytest-html for HTML reports or pytest-cov for coverage reports.
  • CI/CD Integration: Run tests automatically in pipelines (GitHub Actions, Jenkins) using pytest commands.
  • Artifacts: Save reports and logs as pipeline artifacts for review.
Best Practices for Pytest Test Modules
  • Keep test modules focused on one feature or area for clarity.
  • Name test modules and functions clearly to describe what they test.
  • Use fixtures in conftest.py to avoid repeating setup code across modules.
  • Organize tests in the tests/ folder separate from application code.
  • Use markers to group or skip tests easily during runs.
Self Check

Where would you add a new test module for user profile features in this framework structure?

Key Result
Pytest test modules are organized Python files in the tests/ folder grouping related test functions for clear, maintainable automated testing.