@@ -22,7 +22,7 @@ class BadTestCaseDataException(Exception):
22
22
23
23
def pytest_addoption (parser , pluginmanager ):
24
24
"""
25
- Pytest hook function.
25
+ Pytest hook function that adds the command line options .
26
26
27
27
Adds the command line option to automatically load responses and the
28
28
additional option whether to require all responses to be fired.
@@ -246,19 +246,18 @@ def _extract_fixture_data(fixture_raw_data_dict: dict[str, dict[str, Any]]) -> t
246
246
247
247
@pytest .fixture (scope = "function" )
248
248
def psf_responses (request : pytest .FixtureRequest ) -> Generator [Response , None , None ]:
249
- """Load fixture data into responses mock .
249
+ """Returns a ResponsesMock with scenario data loaded .
250
250
251
- Each test scenario needs to have its own responses mock, as they will be testing
252
- different aspects. We need to define a pytest fixture here and provide it to
253
- any scenario that uses responses.
251
+ Used for integration with the Responses package. Each test scenario will get its
252
+ own active ResponsesMock object. This object can then be updated at runtime
253
+ to override the responses loaded from files .
254
254
"""
255
255
# Ultimately we need to wrap this up so that responses and httpx-responses
256
256
# are both supported.
257
- # No qa flag as Ruff doesn't recognize the conditional import in pytest_configure()
258
257
import responses
259
258
260
259
psf_fire_all_responses = request .config .getoption ("psf-fire-all-responses" )
261
- with responses .RequestsMock (assert_all_requests_are_fired = psf_fire_all_responses ) as rsps : # noqa F821
260
+ with responses .RequestsMock (assert_all_requests_are_fired = psf_fire_all_responses ) as rsps :
262
261
for one_response in request .param :
263
262
rsps .add (** one_response )
264
263
yield rsps
@@ -345,11 +344,17 @@ def _extract_responses(fixture_data_dict: dict[str, dict[str, Any]]) -> None:
345
344
346
345
347
346
def pytest_generate_tests (metafunc : pytest .Metafunc ) -> None :
348
- """Pytest hook function called for each test .
347
+ """Pytest hook function that does the parameterization .
349
348
350
- This is where the heavy lifting is done. This walks the directory tree
351
- looking for files that match the name of the test. Any data are loaded
352
- and used to parameterize the test for scenarios.
349
+ This is where the heavy lifting is done. The process is:
350
+
351
+ 1. Walk the directory tree looking for files that match the name of the test.
352
+ 2. Load test data from the files.
353
+ 3. Extract fixture names and check for consistency.
354
+ 4. Pull out indirect fixtures and remove suffixes from fixture names. (Includes
355
+ handling the Responses integration.)
356
+ 5. Reformat the data for the parameterization.
357
+ 6. Make the function call.
353
358
354
359
:param metafunc: Pytest fixture used to create the parameterization
355
360
"""
0 commit comments