pythoncollection.rst

Overview

This file serves as a comprehensive guide and reference documentation on how to customize and control Python test discovery and collection behavior when using the **pytest** testing framework. It explains various command-line options, configuration settings, and Python hooks that influence which tests pytest collects and runs.

The document covers topics such as ignoring files or directories during test collection, deselecting specific tests, handling duplicate test paths, modifying recursive directory traversal, changing naming conventions for test files and functions, interpreting command-line arguments as Python packages, and customizing test collection logic via `conftest.py`.

This is not source code but a rich documentation file (reStructuredText) designed to instruct users and developers on pytest’s test discovery customization capabilities through examples and detailed explanations.


Detailed Explanations of Key Topics

Ignoring Paths During Test Collection


Deselecting Individual Tests


Handling Duplicate Paths


Changing Directory Recursion


Changing Naming Conventions for Tests


Interpreting Command-Line Arguments as Python Packages


Viewing Collected Tests Without Running


Customizing Test Collection via conftest.py


Implementation Details & Algorithms


Interaction with Other System Parts


Usage Examples Summary

  1. Ignoring a directory and a file:

    pytest --ignore=tests/foobar/test_foobar_03.py --ignore=tests/hello/
    
  2. Deselecting a single test function:

    pytest --deselect tests/foobar/test_foobar_01.py::test_a
    
  3. Collecting duplicate items:

    pytest --keep-duplicates path_a path_a
    
  4. Skipping recursion in certain directories:

    [pytest]
    norecursedirs = .svn _build tmp*
    
  5. Changing test naming conventions:

    [pytest]
    python_files = check_*.py
    python_classes = Check
    python_functions = *_check
    
  6. Ignoring files dynamically in conftest.py:

    import sys
    collect_ignore = ["setup.py"]
    if sys.version_info[0] > 2:
        collect_ignore.append("pkg/module_py2.py")
    

Visual Diagram: Test Collection Customization Structure

flowchart TD
    A[pytest Test Collection] --> B[Command Line Options]
    A --> C[Configuration Files (pytest.ini)]
    A --> D[conftest.py Hooks]
    B --> B1(--ignore)
    B --> B2(--ignore-glob)
    B --> B3(--deselect)
    B --> B4(--keep-duplicates)
    B --> B5(--pyargs)
    C --> C1(python_files)
    C --> C2(python_classes)
    C --> C3(python_functions)
    C --> C4(norecursedirs)
    C --> C5(addopts)
    D --> D1(collect_ignore)
    D --> D2(collect_ignore_glob)
    D --> D3(__test__ attribute)
    A --> E[Test Discovery Process]
    E --> F[Directory Traversal]
    E --> G[File & Class Matching]
    E --> H[Test Item Collection]

Summary

This documentation file is a detailed manual on customizing pytest’s Python test discovery mechanisms. It provides users with practical options and techniques to control which tests are collected and run, enhancing test suite flexibility and reliability. Understanding and applying these settings can optimize test runs, avoid unwanted tests, and conform pytest’s behavior to project-specific needs.


**End of Documentation**