0
0
PyTesttesting~8 mins

Test file and function naming conventions in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Test file and function naming conventions
Folder Structure
project-root/
├── tests/
│   ├── test_login.py
│   ├── test_shopping_cart.py
│   ├── test_checkout.py
│   └── __init__.py
├── src/
│   └── app_code.py
├── conftest.py
└── pytest.ini
  
Test Framework Layers
  • Tests: Files named starting with test_ (e.g., test_login.py), containing test functions also starting with test_.
  • Fixtures/Setup: conftest.py holds shared setup code and fixtures.
  • Application Code: Source code under src/ folder.
  • Configuration: pytest.ini for pytest settings.
Configuration Patterns
  • Use pytest.ini to configure test discovery patterns, e.g., python_files = test_*.py and python_functions = test_*.
  • Keep test files and functions named with test_ prefix to be auto-discovered by pytest.
  • Use conftest.py for shared fixtures and environment setup.
  • Manage environment variables or credentials securely outside test files.
Test Reporting and CI/CD Integration
  • pytest automatically reports test results with pass/fail status in the console.
  • Use plugins like pytest-html to generate readable HTML reports.
  • Integrate pytest runs in CI/CD pipelines (e.g., GitHub Actions, Jenkins) by running pytest command.
  • Ensure test naming conventions are followed so CI tools can detect and run tests automatically.
Best Practices
  1. Always start test file names with test_ and end with .py for pytest to find them.
  2. Name test functions starting with test_ to be recognized as tests.
  3. Keep test names descriptive but concise to understand what is tested.
  4. Organize tests by feature or module in separate test files for clarity.
  5. Use conftest.py for shared fixtures instead of repeating setup code.
Self Check

Where would you add a new test function for verifying user registration in this framework structure?

Key Result
Use clear 'test_' prefixes for files and functions so pytest can find and run tests automatically.