fixtures: refactor FixtureRequest
/SubRequest
a bit
#11218
Labels
topic: fixtures
anything involving fixtures directly or indirectly
type: proposal
proposal for a new feature, often to gather opinions or design the API around the new feature
The
FixtureRequest
/SubRequest
situation is currently quite confusing.FixtureRequest
(on its own, not its subclassSubRequest
) is used for therequest
fixture in a test function itself. One is created even if therequest
fixture is not explicitly requested. The name is a bit confusing -- it is not a request for a fixture. I think it wanted to be namedRequestFixture
i.e. therequest
fixture, but not 100% sure.A test function itself cannot be parametrized using
request.param
, only a fixture can.For
FixtureRequest
therequest.scope
is alwaysfunction
and therequest.node
is the item.In the pytest internals, it is used to drive getting the fixture values of the fixtures needed by the item (
_fillfixtures
).SubRequest
is used forrequest
inside a fixture. TheSubRequest
holds a reference to theFixtureDef
it handles.For parametrized fixtures it holds the parameter value (
request.param
).In the pytest internals, it is used to drive the execution of a specific "fixture request" .
While executing an item, the
SubRequest
s and the topFixtureRequest
form a chain/stack. For example, if we havetest_it
requestingfix1
requestingfix2
, then there isSubRequest(fix2)
which points toSubRequest(fix1)
which points toFixtureRequest(test_it)
. This is only used in practice for printing a "fixture stack" in some errors, but is useful to understand conceptually.SubRequest
inherits fromFixtureRequest
, but in a pretty hard to understand way, e.g. its__init__
doesn't call the super.As an initial way to clarify things a bit, I propose making
FixtureRequest
itself abstract, and add a newTopRequest
subclass for the test-functionrequest
.The name
TopRequest
would not be my TopChoice, but renamingFixtureRequest
orSubRequest
at this point would be a disruptive breaking change, so better to choose a name which makes sense in relation toSubRequest
.This will break plugins which instantiate
FixtureRequest
directly. It's private so not an official breaking change. I foundpytest-alembic
,pytest-yield
,pytest-wdl
,pytest-play
. I can notify them about this change if this proposal is accepted.The text was updated successfully, but these errors were encountered: