goodpractices.rst

Overview

This file, [goodpractices.rst](/projects/286/67223), serves as a comprehensive guide outlining best practices and recommendations for integrating and testing Python projects using `pytest`. It provides detailed instructions on:

Its primary purpose is to help developers structure their Python projects and tests effectively, ensuring smooth integration, reliable test execution, and maintainable codebases.

Detailed Content Breakdown

1. Installation and Environment Setup

**Example `pyproject.toml` snippet:**

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "PACKAGENAME"
version = "PACKAGEVERSION"

2. Python Test Discovery Conventions

`pytest` uses a standard discovery mechanism that:

3. Test Layout Recommendations

Two common layouts are discussed:

a) Tests Outside Application Code (Recommended)

Structure:

pyproject.toml
src/
    mypkg/
        __init__.py
        app.py
        view.py
tests/
    test_app.py
    test_view.py

Benefits:

**Example config addition:**

[tool.pytest.ini_options]
addopts = [
    "--import-mode=importlib",
]
pythonpath = "src"

b) Tests Inside Application Code

Structure:

pyproject.toml
[src/]mypkg/
    __init__.py
    app.py
    view.py
    tests/
        __init__.py
        test_app.py
        test_view.py

4. Import Modes

Details on `pytest` import modes:

Workarounds when using `prepend` import mode include adding `__init__.py` files to test directories to make them packages, allowing duplicate test module names with qualified imports.

5. Using tox

6. Deprecated Practices

7. Code Style Checking with flake8-pytest-style

Implementation Details and Algorithms

This file is a **documentation guide** rather than executable code. Therefore, it contains no classes or functions, nor any algorithms. Instead, it focuses on textual guidelines, configuration snippets, and command-line examples.

Its content is structured in reStructuredText (reST) format, suitable for Sphinx or similar documentation generators, with cross-references (`:doc:`, `:ref:`, `:confval:`) to other documentation topics.

Interaction with Other Parts of the System

Overall, this file acts as an integration point consolidating best practices for Python project testing and distribution workflows, ensuring consistency and maintainability across the development lifecycle.

Visual Diagram

Since this file is a **utility documentation guide** without classes or functions, the most appropriate diagram is a **flowchart** illustrating the main concepts and their relationships, focusing on test discovery, layout choices, and tooling.

flowchart TD
    A[Start: Setup Python Package]
    A --> B[Create pyproject.toml]
    B --> C{Choose Test Layout}
    C -->|Tests Outside App Code| D[Use src/ and tests/ folders]
    C -->|Tests Inside App Code| E[Inline tests in package folder]
    D --> F[Install package editable: pip install -e .]
    E --> F
    F --> G{Select Import Mode}
    G -->|prepend (default)| H[Require unique test filenames]
    G -->|importlib (recommended)| I[No sys.path changes, fewer conflicts]
    H --> J[May add __init__.py to tests/ for packages]
    I --> K[Run pytest normally]
    J --> K
    K --> L[Use tox for automated testing]
    L --> M[Use flake8-pytest-style for linting]
    M --> N[Run tests and maintain code quality]
    N --> O[End]

This flowchart shows the typical workflow a developer follows when setting up testing with pytest according to the recommendations in this file.


**Summary** The [goodpractices.rst](/projects/286/67223) file is a detailed guide that helps Python developers set up their project environments, structure tests effectively, and avoid common pitfalls during test discovery and execution. It promotes modern tooling and practices such as editable installs, `src` layouts, importlib import mode, and use of `tox` and linting tools to achieve robust and maintainable test suites.