test_funcarg_lookup_classlevel.py


Overview

This file contains a minimal pytest test class demonstrating the use of **class-level fixture injection** in pytest. It showcases how a fixture defined within a test class can access the current test instance (`self`) through pytest’s `request` fixture, and how that fixture can then be used in test methods to assert properties about the test instance.

The primary purpose is to verify that pytest fixtures at the class scope can correctly resolve and inject the test class instance (`self`) into test methods, enabling tests to interact with their own context.


Detailed Explanation

Imports


Class: TestClass

This class encapsulates the test scenario.

Fixture: something

@pytest.fixture
def something(self, request):
    return request.instance

**Implementation Detail:** `request.instance` is a pytest feature that returns the instance of the test class that is currently running the test method. This allows fixtures to access or manipulate the test instance directly.


Test Method: test_method

def test_method(self, something):
    assert something is self

**Usage Example:**

pytest test_funcarg_lookup_classlevel.py

This will run `test_method`, and the assertion verifies that the `something` fixture is implemented correctly.


Implementation Details


Interaction with Other Parts of the System


Mermaid Class Diagram

classDiagram
    class TestClass {
        +something(request) : instance
        +test_method(something)
    }
    TestClass : +pytest.fixture

Summary

This file provides a concise example of how to use pytest fixtures to inject the test class instance into test methods. It is primarily educational or demonstrative, showing how to leverage `request.instance` for class-level fixture lookups in pytest tests. The test asserts that the fixture correctly returns the class instance, ensuring the mechanism works as intended.