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.
When
collect_imported_testsis disabled (false), pytest only collects tests defined directly in the test modules.When
collect_imported_testsis enabled (true, the default), pytest also collects test classes and test functions imported into the test 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:**
pytester(Pytester): A pytest fixture providing a test environment with utilities for creating files and running pytest in isolation.
**Behavior:**
Creates two directories:
srcandtests.Writes a Python source file
foo.pyinsrcwith:A class
Testamentcontaining a test methodtest_collections.A standalone test function
test_testament.
Writes a test file
foo_test.pyinteststhat importsTestamentandtest_testamentfromfoo.pyand defines a test classTestDomainwith one test method.Inserts
srcin the Python path so imports in tests succeed.
**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:**
pytester(Pytester): Pytest testing environment fixture.
**Behavior:**
Creates a pytest configuration file (
pytest.ini) withcollect_imported_tests = false.Calls
setup_filesto prepare the source and test files.Runs pytest on the
testsdirectory.Asserts that only the test method inside the test module (
tests/foo_test.py::TestDomain::test) is collected and run.Checks internal pytest hooks and reports to confirm only the relevant test was collected.
**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:**
pytester(Pytester): Pytest testing environment fixture.configure_ini(bool): Whether to explicitly setcollect_imported_tests = truein pytest.ini or rely on the default value.
**Behavior:**
Optionally creates pytest.ini with
collect_imported_tests = trueifconfigure_iniisTrue.Calls
setup_filesto prepare source and test files.Runs pytest on the
testsdirectory.Asserts that all tests are collected and run, including:
Testament.test_collections(imported class method)test_testament(imported standalone function)TestDomain.test(defined in the test module)
**Usage Example:**
pytest -v tests
# Expect tests from imported classes/functions and test module to run.
Important Implementation Details
Uses
pytesterfixture frompytestto create temporary test environments with isolated files and run commands.Uses
textwrap.dedentto write clean multi-line source code strings.Uses Python's pathlib-style path operations to create and write files.
Checks pytest internal hooks (
pytest_collectreport,pytest_collection_modifyitems,pytest_itemcollected) to validate collection behavior.Uses
pytest.mark.parametrizeto test both explicit and implicit enabling of the config option.
Interaction with Other Parts of the System
This test file is part of pytest's internal test suite or plugin test suite.
It ensures correctness of pytest's test collection logic when the
collect_imported_testssetting is toggled.Interacts with pytest core collection mechanism and configuration system.
Uses pytest's
Pytestertesting utility to simulate test runs and inspect results.Depends on the existence and behavior of the
collect_imported_testsconfiguration option in pytest's collection plugins.
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:**
setup_filesis a utility function called by both test functions.The two test functions represent different test scenarios depending on the configuration setting.
The flowchart shows dependencies and call relationships.
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.