Supprimer Rendre public Rendre privé Add tags Delete tags
  Ajouter un tag   Annuler
  Supprimer le tag   Annuler
  • • DevOps notes •
  •  
  • AI
  • Tags
  • Connexion

Configuring Pytest/shaare/bti1Ng

  • python
  • python

Configuring Pytest

  • As you start using Pytest extensively, typing -v or -m on the command line every time becomes tedious.
  • Centralize your defaults in pyproject.toml under the [tool.pytest.ini_options] table.
  • A single source of truth means every developer—and your CI system—runs tests with the same settings.
  • Putting Pytest alongside other PEP 518 tools (Black, isort, Flake8) keeps your repo tidy and consistent.

Why a Configuration File?

  • Consistency: Run pytest without remembering flags; everyone gets the same behavior.
  • Simplicity: Remove boilerplate from docs and CI scripts.
  • Project-specific discovery: Set testpaths, python_files and markers in one place.
  • Cleaner output: Declare markers to silence PytestUnknownMarkWarning, enable color and rich tracebacks by default.

Configuration File Hierarchy

Pytest searches for settings in this order, using the first match from the current or a parent directory:

  1. pyproject.toml under [tool.pytest.ini_options]
  2. pytest.ini
  3. tox.ini with a [pytest] section
  4. setup.cfg under [tool:pytest]

Embrace pyproject.toml as the modern hub for all your tool configurations.

Creating pyproject.toml

  1. Create or open pyproject.toml at your project root.
  2. Add a [tool.pytest.ini_options] table.
  3. Define your defaults using TOML syntax and inline strings.

Common Configuration Options

  • addopts
    Defines default command-line flags that Pytest applies on every run (verbosity, reporting, color, etc.).

  • markers
    Pre-registers custom markers with descriptions so that you can categorize tests and avoid unknown-marker warnings.

  • testpaths
    Restricts test discovery to the listed directories, preventing Pytest from scanning other parts of the project.

  • python_files
    Specifies filename patterns that Pytest treats as test files (e.g., test_*.py).

  • python_classes
    Indicates class name patterns Pytest will consider when looking for test classes (e.g., classes starting with Test).

  • python_functions
    Sets function name patterns Pytest uses to identify individual test functions (e.g., functions beginning with test_).

  • Other options

    • norecursedirs: directories to skip during discovery
    • minversion: enforce a minimum Pytest version
    • filterwarnings: configure how warnings are handled
    • and many more built-in settings for fine-tuning

Example of pyproject.toml

from typing import TypedDict
import re

class TextAttributes(TypedDict):
    word_count: int
    unique_words: set[str]
    average_word_length: float
    longest_word: str

def calculate_text_attributes(input_text: str) -> TextAttributes:
    split_text = re.findall(r"\w+", input_text)
    word_length_sum = sum(len(word) for word in split_text)
    avg_word_length = (
        word_length_sum / len(split_text)
        if len(split_text)
        else 0
    )

    return {
        "word_count": len(split_text),
        "unique_words": set(text.lower() for text in split_text),
        "average_word_length": avg_word_length,
        "longest_word": (
            max(split_text, key=len) if split_text else ""
        ),
    }
1 month ago Permalien
cluster icon
  • Logging Anatomy : Python Logging Anatomy Python’s logging module has five core components: Loggers, Log Records, Handlers, Formatters and Filters. Loggers are hierar...
  • Fixtures in Pytest : Fixtures in Pytest As tests grow more complex, repeating setup and cleanup steps makes tests harder to read and maintain. Pytest fixtures allow centr...
  • Concise Iteration: List Comprehensions : Concise Iteration: List Comprehensions Simple for loops to create lists can be verbose. We can leverage list comprehensions to define the list content...
  • Temporary Files and Directories : Temporary Files and Directories Automation scripts often need scratch space for intermediate data without cluttering the filesystem or risking name c...
  • Typing : Introduction Python is a dynamically typed language, meaning you can assign values to variables without declaring their types, and type checking happ...


(110)
Filtrer par liens sans tag
Replier Replier tout Déplier Déplier tout Êtes-vous sûr de vouloir supprimer ce lien ? Êtes-vous sûr de vouloir supprimer ce tag ? Le gestionnaire de marque-pages personnel, minimaliste, et sans base de données par la communauté Shaarli