collect.py


Overview

The [collect.py](/projects/286/67331) file contains a comprehensive suite of tests primarily targeting **pytest's collection mechanism** for test modules, test classes, and test functions. The file validates how pytest discovers, collects, and organizes test items from Python source files, ensuring correct behavior across a wide variety of scenarios including syntax errors, import errors, parametrization, custom collection hooks, and edge cases like duplicate test names or special Python constructs.

The tests in this file leverage pytest's own testing utilities (`pytester`, `MonkeyPatch`, `Pytester`, etc.) to simulate and verify the behavior of pytest internals during test collection, parametrization, and reporting phases.

In essence, this file acts as a **meta-test suite** for pytest's test collection subsystem, verifying both correctness and robustness against regressions.


Detailed Explanation of Classes and Functions

Class: TestModule

Tests related to the collection of **test modules** (Python files interpreted as test containers).

Methods:


Class: TestClass

Tests related to the collection of **test classes**.

Methods:


Class: TestFunction

Tests related to the collection and behavior of **test functions**.

Methods:


Class: TestSorting

Tests related to sorting and comparison behaviors of pytest test items.


Class: TestConftestCustomization

Tests for pytest hooks related to customizing the **collection** process via `conftest.py` files.


Other Functions and Tests


Important Implementation Details / Algorithms


Interaction with Other Parts of the System


Usage Examples

The file itself is a test suite and not intended for direct usage beyond pytest's internal test runs. However, examples of how tests are written in this file:

def test_class_with_init_warning(self, pytester: Pytester) -> None:
    pytester.makepyfile(
        """
        class TestClass1(object):
            def __init__(self):
                pass
    """
    )
    result = pytester.runpytest()
    result.stdout.fnmatch_lines(
        [
            "*cannot collect test class 'TestClass1' because it has "
            "a __init__ constructor*"
        ]
    )

This creates a test file with a class having an `__init__` method, runs pytest, and asserts that the output contains a specific warning.


Mermaid Diagram: Class Structure

classDiagram
    class TestModule {
        +test_failing_import(pytester)
        +test_import_duplicate(pytester)
        +test_import_prepend_append(pytester, monkeypatch)
        +test_syntax_error_in_module(pytester)
        +test_module_considers_pluginmanager_at_import(pytester)
        +test_invalid_test_module_name(pytester)
        +test_show_traceback_import_error(pytester, verbose)
        +test_show_traceback_import_error_unicode(pytester)
    }
    class TestClass {
        +test_class_with_init_warning(pytester)
        +test_class_with_new_warning(pytester)
        +test_class_subclassobject(pytester)
        +test_static_method(pytester)
        +test_setup_teardown_class_as_classmethod(pytester)
        +test_issue1035_obj_has_getattr(pytester)
        +test_issue1579_namedtuple(pytester)
        +test_issue2234_property(pytester)
        +test_does_not_discover_properties(pytester)
        +test_does_not_discover_instance_descriptors(pytester)
        +test_abstract_class_is_not_collected(pytester)
    }
    class TestFunction {
        +test_getmodulecollector(pytester)
        +test_function_as_object_instance_ignored(pytester)
        +make_function(pytester, **kwargs)
        +test_function_equality(pytester)
        +test_repr_produces_actual_test_id(pytester)
        +test_issue197_parametrize_emptyset(pytester)
        +test_single_tuple_unwraps_values(pytester)
        +test_issue213_parametrize_value_no_equal(pytester)
        +test_parametrize_with_non_hashable_values(pytester)
        +test_parametrize_with_non_hashable_values_indirect(pytester)
        +test_parametrize_overrides_fixture(pytester)
        +test_parametrize_overrides_parametrized_fixture(pytester)
        +test_parametrize_overrides_indirect_dependency_fixture(pytester)
        +test_parametrize_with_mark(pytester)
        +test_parametrize_with_empty_string_arguments(pytester)
        +test_function_equality_with_callspec(pytester)
        +test_pyfunc_call(pytester)
        +test_multiple_parametrize(pytester)
        +test_issue751_multiple_parametrize_with_ids(pytester)
        +test_parametrize_skipif(pytester)
        +test_parametrize_skip(pytester)
        +test_parametrize_skipif_no_skip(pytester)
        +test_parametrize_xfail(pytester)
        +test_parametrize_passed(pytester)
        +test_parametrize_xfail_passed(pytester)
        +test_function_originalname(pytester)
        +test_function_with_square_brackets(pytester)
    }
    class TestSorting {
        +test_check_equality(pytester)
        +test_allow_sane_sorting_for_decorators(pytester)
        +test_ordered_by_definition_order(pytester)
    }
    class TestConftestCustomization {
        +test_pytest_pycollect_module(pytester)
        +test_customized_pymakemodule_issue205_subdir(pytester)
        +test_customized_pymakeitem(pytester)
        +test_pytest_pycollect_makeitem(pytester)
        +test_issue2369_collect_module_fileext(pytester)
        +test_early_ignored_attributes(pytester)
    }
    class TestTracebackCutting {
        +test_skip_simple()
        +test_traceback_argsetup(pytester)
        +test_traceback_error_during_import(pytester)
        +test_traceback_filter_error_during_fixture_collection(pytester)
        +test_filter_traceback_generated_code()
        +test_filter_traceback_path_no_longer_valid(pytester)
    }
    class TestReportInfo {
        +test_itemreport_reportinfo(pytester)
        +test_func_reportinfo(pytester)
        +test_class_reportinfo(pytester)
        +test_reportinfo_with_nasty_getattr(pytester)
    }
    
    TestModule ..|> object
    TestClass ..|> object
    TestFunction ..|> object
    TestSorting ..|> object
    TestConftestCustomization ..|> object
    TestTracebackCutting ..|> object
    TestReportInfo ..|> object

Summary

[collect.py](/projects/286/67331) contains a rich set of tests targeting the core pytest test collection mechanism. It validates:

This file is crucial for maintaining the reliability and correctness of pytest's test discovery and collection subsystem, ensuring that end-users experience consistent and predictable test runs.


End of Documentation for collect.py