pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
12.13k stars 2.69k forks source link

Arbitrary nested test functions #12646

Closed Gochkin closed 2 weeks ago

Gochkin commented 3 months ago

Description

I would like to have the ability to arbitrarily nest test functions in the same manner as facilitated by the pytest-describe plugin. While we can currently nest test functions using nested test classes, these nested classes impose limitations when accessing fields and properties from outer classes. My goal is to organize and group tests to mirror the structure of the code being tested. Specifically, I want a test class for each class (including nested and inherited classes) and a group (or multiple groups) of test functions for each function. Although pytest-describe provides this functionality, I believe it should be an integral part of pytest's general test discovery, organizational grouping, and structure.

What's the Problem This Feature Will Solve?

If implemented, this feature will allow:

Describe the Solution You'd Like

I would like the ability to arbitrarily nest test functions. pytest should be able to:

Alternative Solutions

I could use the pytest-describe plugin, but I believe this functionality should be part of the core pytest. I also found the pytest-relaxed plugin, which helps achieve similar functionality with inner classes by automatically passing all members of the outer class to the inner class. However, it seems to be incompatible with hypothesis.

RonnyPfannschmidt commented 3 months ago

A hard no for including this in core

It's using tracing to hack on collection and it is completely unclear how to integrate it with fixtures and parameterization

Python has no good Way to manage controlled namespaces beside classes