API¶
Package that will generate Pytest unit test scenarios based on data files.
- pytest_scenario_files.psf_expected_result(request: FixtureRequest) AbstractContextManager ¶
Convenience fixture for possible expected exceptions
Pytest has a pattern called Parameterized Conditional Raising (See: https://docs.pytest.org/en/8.3.x/example/parametrize.html#parametrizing-conditional-raising). This fixture allows the user to specify either an expected exception (including a match string or regexp) in the scenario file, or any other expected result value. An exception gets wrapped in a pytest.raises() context manager, while any other value gets wrapped in a nullcontext() context manager. The test function can then use a call like:
with psf_expected_result as expected_result: assert expected_result == function_being_tested()
- Parameters:
request (pytest.FixtureRequest) – The fixture request object containing the test parameters.
- Returns:
A context manager that either catches the expected exception or a nullcontext() context manager.
- Return type:
AbstractContextManager:
- pytest_scenario_files.psf_responses(request: pytest.FixtureRequest) Generator[RequestsMock, None, None] ¶
Returns a responses.RequestsMock with scenario data loaded.
Used for integration with the Responses package. Each test scenario will get its own active RequestsMock object. This object can then be updated at runtime to override the responses loaded from files.
- pytest_scenario_files.psf_respx_mock(request: pytest.FixtureRequest) Generator[MockRouter, None, None] ¶
Returns a respx.MockRouter with scenario data loaded.
Used for integration with the Respx package. Each test scenario will get its own active MockRouter object. This object can then be updated at runtime to override the responses loaded from files.
- pytest_scenario_files.pytest_addoption(parser: Parser, pluginmanager)¶
Pytest hook function that adds the command line options.
Adds the command line options to automatically load http responses for the Responses package or the Respx package and additional options whether to require all responses to be fired and all calls to be mocked.
- pytest_scenario_files.pytest_configure(config: Config)¶
Get config data from command line flags
Raises an exception if both –psf-load-responses and –psf-load-respx are both enabled since these are mutually exclusive options. Also stores values for psf-fire-all-responses, psf-assert-all-fired, and psf-assert-all-mocked.
- Parameters:
config (pytest.Config) – The pytest configuration object containing all command-line options and plugin configuration.
- Raises:
pytest.UsageError – If both –psf-load-responses and –psf-load-respx options are specified simultaneously.
- pytest_scenario_files.pytest_generate_tests(metafunc: Metafunc) None ¶
Pytest hook function that does the parameterization.
This is where the heavy lifting is done. The process is:
Walk the directory tree looking for files that match the name of the test.
Load test data from the files.
Extract fixture names and check for consistency.
(Optional) Gather data from all fixtures with the suffixes
_response
or_responses
into a single fixture for use with the Responses or Respx integrations.Get list of indirect fixtures and remove the suffix
_indirect
from indirect fixture names.Reformat the data into lists for the parameterization call.
Make the function call.
- Parameters:
metafunc – Pytest fixture used to create the parameterization