test_funcarg_lookup_modulelevel.py


Overview

This file contains a simple Pytest test module demonstrating the **module-level fixture argument lookup** feature in Pytest. It shows how a fixture defined at the module level can be injected into both test functions and test methods within a test class.

The main purpose of this file is to verify that the `something` fixture correctly resolves the name of the currently running test function or method and that this fixture can be used seamlessly in different test scopes.


Detailed Explanation

Imports and Decorators


Fixture: something

@pytest.fixture
def something(request):
    return request.function.__name__
def test_example(something):
    assert something == "test_example"

Here, `something` will be `"test_example"` during the test execution.


Class: TestClass

class TestClass:
    def test_method(self, something):
        assert something == "test_method"

Method Name

Parameters

Returns

Description

`test_method`

`self`, `something`

None

Asserts that the `something` fixture value equals the method name `"test_method"`.

# Pytest will automatically inject the 'something' fixture
test_obj = TestClass()
test_obj.test_method(something="test_method")  # Normally invoked by Pytest

Function: test_func

def test_func(something):
    assert something == "test_func"

Parameter

Type

Description

`something`

str

Injected fixture returning test function name

def test_func(something):
    assert something == "test_func"

Implementation Details and Algorithms


Interaction with Other Parts of the System


Mermaid Diagram

The file is a **utility test module** primarily containing one fixture, one test class, and one test function, showing the relationship between these elements.

flowchart TD
    A[something (fixture)]
    B[TestClass]
    C[test_func]

    B -->|uses| A
    C -->|uses| A

Summary


If you want to run the tests in this file, simply execute the following command in the terminal:

pytest test_funcarg_lookup_modulelevel.py

You should see both tests passing, confirming the fixture works as expected.