metafunc.py


Overview

The [metafunc.py](/projects/286/67337) file is a comprehensive test suite module primarily focused on testing the behavior and functionality of the `python.Metafunc` class within the pytest testing framework. The `Metafunc` object in pytest represents the test function's meta-information during test collection and generation phases, enabling dynamic test parameterization and fixture interaction.

This file contains an extensive set of unit and functional tests that validate core features such as:

The file uses mock objects to simulate parts of pytest internals for unit testing and leverages pytest's own testing utilities (`Pytester`) for integration-style tests.


Detailed Explanation of Classes and Methods

Class: TestMetafunc

This class contains unit tests for the core functionalities of the `python.Metafunc` class, focusing on correctness of parameterization logic and ID generation.

Method: Metafunc(self, func, config=None) -> python.Metafunc


Test Methods Overview

The class contains many test methods, each testing specific aspects of `Metafunc`. Highlights include:


Class: TestMetafuncFunctional

This class consists of functional tests that primarily use pytest's `Pytester` utility to run tests in isolated environments and verify pytest's behavior end-to-end.

Key test behaviors:


Class: TestMetafuncFunctionalAuto

Focused on tests related to automatic scope detection for parametrized tests.


Class: TestMarkersWithParametrization

Tests the interaction between pytest markers (e.g., `xfail`, user-defined marks) and parametrized tests.


Class: TestHiddenParam

Tests the feature of `pytest.HIDDEN_PARAM` which allows hiding certain parameter sets from test IDs.


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

classDiagram
    class TestMetafunc {
        +Metafunc(func, config=None) python.Metafunc
        +test_no_funcargs()
        +test_function_basic()
        +test_parametrize_error()
        +test_parametrize_error_iterator()
        +test_parametrize_bad_scope()
        +test_parametrize_request_name()
        +test_find_parametrized_scope()
        +test_parametrize_and_id()
        +test_unicode_idval()
        +test_idmaker_autoname()
        +test_parametrize_indirect()
        +test_parametrize_uses_no_fixture_error_indirect_false()
        +test_high_scoped_parametrize_reordering()
        +test_parametrize_multiple_times()
        +test_parametrize_class_scenarios()
        +test_parametrize_ids_exception()
        +test_parametrize_with_identical_ids_get_unique_names()
    }

    class TestMetafuncFunctional {
        +test_attributes(pytester)
        +test_two_functions(pytester)
        +test_generate_tests_in_class(pytester)
        +test_parametrize_functional2(pytester)
        +test_parametrize_and_inner_getfixturevalue(pytester)
        +test_parametrize_with_ids(pytester)
        +test_parametrize_iterator(pytester)
        ...
    }

    class TestMetafuncFunctionalAuto {
        +test_parametrize_auto_scope(pytester)
        +test_parametrize_auto_scope_indirect(pytester)
        +test_parametrize_all_indirects(pytester)
        +test_parametrize_some_arguments_auto_scope(pytester)
        +test_parametrize_issue634(pytester)
    }

    class TestMarkersWithParametrization {
        +test_simple_mark(pytester)
        +test_select_based_on_mark(pytester)
        +test_simple_xfail(pytester)
        +test_parametrize_called_in_generate_tests(pytester)
        +test_parametrize_ID_generation_string_int_works(pytester)
        +test_parametrize_marked_value(pytester)
    }

    class TestHiddenParam {
        +test_parametrize_ids(pytester)
        +test_param_id(pytester)
        +test_multiple_hidden_param_is_forbidden(pytester)
        +test_multiple_hidden_param_is_forbidden_idmaker()
        +test_multiple_parametrize(pytester)
    }

    %% Relationships
    TestMetafunc ..> python.Metafunc : tests
    TestMetafunc ..> IdMaker : uses
    TestMetafuncFunctional ..> Pytester : uses
    TestMarkersWithParametrization ..> pytest.mark : tests interaction
    TestHiddenParam ..> pytest.HIDDEN_PARAM : tests feature

Summary

The [metafunc.py](/projects/286/67337) file is a critical pytest internal testing module that extensively tests the `Metafunc` class and related parametrization features. Through a combination of unit tests (with mocks), property-based tests, and integration tests (using `Pytester`), it ensures robust and correct behavior of test parameterization, ID generation, scope determination, and marker handling in pytest.

This module helps verify that:

By rigorously testing these behaviors, this file helps maintain and improve pytest's powerful and flexible parameterization capabilities.


End of Documentation for metafunc.py