Populate tests from module-level fixture #9945
-
A fixture (scope='module') is bound to a test function as a parameter. That function pulls some data from a remote database or from a local file snapshot (depends on command line parameters and/or environment variables that could override) which defines some rules according to which the testcase should generate values to run through the business logic being tested and also populates some variables that describe what items were processed, which of them generate a single test function run and so on. So the fixture itself is the function that defines the parameters for test function parametrization. But according to the code All the examples in the documentation just define some static switching logic (like class A for this case and class B for another and here we go on with parametrization on those two). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @AndySemenov, A normal pytest session is designed to execute two separate stages:
Under this design, is not possible for fixture code (which executes in stage 2) to generate more tests. The only solution is to decouple the "test generation" code from your fixture, and execute it during Hope the general overview helps! |
Beta Was this translation helpful? Give feedback.
Hi @AndySemenov,
A normal pytest session is designed to execute two separate stages:
conftest.py
files discovered while we search the file system). At this stagepytest_generate_tests
kicks in for each module it is defined, and can generate more tests. At this point we also discover fixtures, their scopes, and their visibility to each test (no fixture code is executed yet).setup
,call
, andteardown
. Duringsetup
, each fixture requested by the test is executed, with the return value injected into the test function.Under this design…