test_collect_imported_tests.py


Overview

This file contains automated tests for the `collect_imported_tests` configuration option in **pytest**. The `collect_imported_tests` setting controls whether pytest collects and runs test functions and test classes imported into test modules from other modules.

The test cases here verify that pytest behaves correctly based on these configuration settings by creating temporary source and test files, running pytest on them, and asserting the collected tests.


Detailed Explanation of Components

Functions


setup_files(pytester: Pytester) -> None

**Purpose:** Prepares a sample project structure with source and test files used in the tests.

**Parameters:**

**Behavior:**

**Usage Example:**

def test_example(pytester: Pytester) -> None:
    setup_files(pytester)
    # After this, the test environment has 'src/foo.py' and 'tests/foo_test.py' created.

test_collect_imports_disabled(pytester: Pytester) -> None

**Purpose:** Tests that when `collect_imported_tests` is set to `false`, only test objects defined directly in test modules are collected.

**Parameters:**

**Behavior:**

**Usage Example:**

pytest -v tests
# Expect only tests defined directly in test modules to run.

test_collect_imports_enabled(pytester: Pytester, configure_ini: bool) -> None

**Purpose:** Tests that when `collect_imported_tests` is enabled (default or explicitly set), pytest collects tests imported into test modules as well.

**Parameters:**

**Behavior:**

**Usage Example:**

pytest -v tests
# Expect tests from imported classes/functions and test module to run.

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram: Class and Functions Structure

flowchart TD
    A[setup_files(pytester: Pytester) : None]
    B[test_collect_imports_disabled(pytester: Pytester) : None]
    C[test_collect_imports_enabled(pytester: Pytester, configure_ini: bool) : None]

    B --> A
    C --> A

**Diagram Explanation:**


Summary

This file contains functional tests that validate the behavior of pytest's `collect_imported_tests` configuration. It programmatically generates source and test files, runs pytest with different configuration states, and asserts that test collection aligns with expectations. This ensures that users can control whether pytest collects imported test functions and classes, enhancing flexibility in how test suites are organized.


End of Documentation for test_collect_imported_tests.py