Delete Set public Set private Add tags Delete tags
  Add tag   Cancel
  Delete tag   Cancel
  • • DevOps notes •
  •  
  • AI
  • Tags
  • Login

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 Permalink
cluster icon
  • Working with Environment Variables : Working with Environment Variables Environment variables are dynamic, named values provided by the operating system to running processes, enabling co...
  • Custom Exceptions: Tailoring Error Signals : Custom Exceptions: Tailoring Error Signals Built-in exceptions are great, but often too generic for application-specific failures. A custom excepti...
  • Working with JSON files : Working with JSON files JSON is the standard format for data exchange in web services and cloud APIs. Python’s built-in json module provides function...
  • Dictionaries : Dictionaries (dict) Dictionaries are mutable, insertion-ordered collections of key-value pairs. Keys must be unique and immutable; values can be of an...
  • Automated Testing with Pytest : Assertions in Pytest Pytest uses Python’s built-in assert statement to declare expected conditions in tests, making test code concise and readable. W...


(97)
Filter untagged links
Fold Fold all Expand Expand all Are you sure you want to delete this link? Are you sure you want to delete this tag? The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community