API
pytest-regtest public API
pytest_regtest.RegtestStream
Fixture for recording textual output in regression tests.
The regtest fixture provides an instance of this class. It behaves like a
file handle that can be used to record output for comparison.
| ATTRIBUTE | DESCRIPTION |
|---|---|
version |
An optional identifier to distinguish multiple recorded outputs within the same test function.
|
identifier |
Alias for
|
pytest_regtest.Snapshot.check
check(
obj: Any,
*,
transform: Optional[Callable[[Any], Any]] = None,
failure_handler: Optional[
Callable[[Any, Any], None]
] = None,
version: Optional[Union[str, int]] = None,
**options: Any,
) -> None
Record and check a snapshot of the given object.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The object to record.
TYPE:
|
transform
|
An optional function applied to
TYPE:
|
failure_handler
|
A callback function that is called when the check fails. The function receives the current and the recorded object as arguments.
TYPE:
|
version
|
An optional identifier to distinguish multiple snapshots within the same test function.
TYPE:
|
**options
|
Additional options passed to the snapshot handler.
Supported options depend on the handler (e.g.,
TYPE:
|
pytest_regtest.register_converter_post
Registers a new conversion function at the head of the list of existing converters
| PARAMETER | DESCRIPTION |
|---|---|
function
|
Function to cleanup given string and remove data which can change
between test runs without affecting the correctness of the test.
The second argument is optional and is a
TYPE:
|
pytest_regtest.register_converter_pre
Registers a new conversion function at the head of the list of existing converters.
| PARAMETER | DESCRIPTION |
|---|---|
function
|
Function to cleanup given string and remove data which can change
between test runs without affecting the correctness of the test.
The second argument is optional and is a
TYPE:
|
pytest_regtest.clear_converters
Unregisters all converters, including the builtin converters.
pytest_regtest.register_snapshot_transform
register_snapshot_transform(
check_function: Callable[[Any], bool],
transform_function: Callable[[Any], Any],
) -> None
Registers a transform applied to every snapshotted object that matches
check_function, before it is recorded and compared. Useful for cleaning up
data which can change between test runs without affecting the correctness of
the test, e.g. stripping timestamps or uuids from dicts, without having to
write a full custom snapshot handler.
Multiple registered transforms are applied in registration order to every
object for which check_function returns True.
| PARAMETER | DESCRIPTION |
|---|---|
check_function
|
A function which takes the snapshotted object and returns
TYPE:
|
transform_function
|
A function which takes the snapshotted object and returns the cleaned-up object to record and compare instead.
TYPE:
|
pytest_regtest.clear_snapshot_transforms
Unregisters all globally registered snapshot transforms.
Snapshot cleaning helpers
pytest_regtest.cleaning.drop_keys
Returns a transform that recursively removes the given dict keys from
obj, wherever they occur at any nesting depth.
pytest_regtest.cleaning.round_floats
Returns a transform that recursively rounds every float in obj to
ndigits decimal places, so insignificant numerical noise (e.g. from
floating point summation order) doesn't cause spurious snapshot failures.
pytest_regtest.cleaning.redact_regex
Returns a transform that recursively replaces every regex match of
pattern in string values of obj with repl.
pytest_regtest.cleaning.walk
Recursively walks obj, rebuilding it so the original is left untouched.
Only the builtin containers dict, list, tuple and set are recursed into;
everything else is considered a leaf and passed through leaf_fn.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The (possibly nested) object to walk.
TYPE:
|
leaf_fn
|
Function applied to each leaf value encountered.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
A new object with |
Snapshot handler API
pytest_regtest.snapshot_handler.SnapshotHandlerRegistry
This class serves as a registry for the builtin snapshot handlers and can be used to add more handlers for particular data types.
add_handler
classmethod
add_handler(
clz,
check_function: Callable[[Any], bool],
handler_class: type[BaseSnapshotHandler],
insert_front: bool = False,
)
Add a handler.
| PARAMETER | DESCRIPTION |
|---|---|
check_function
|
A function which takes an object and returns
TYPE:
|
get_handler
classmethod
Find and initialize handler for the given object.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The object for which we try to find a handler.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BaseSnapshotHandler
|
An instance of the handler or |
pytest_regtest.snapshot_handler.BaseSnapshotHandler
Bases: ABC
Abstract base class to implement snapshot handlers.
__init__
abstractmethod
This method is called within snapshot.check to configure the handler.
| PARAMETER | DESCRIPTION |
|---|---|
handler_options
|
Keyword arguments from
TYPE:
|
pytest_config
|
TYPE:
|
tw
|
Terminal width.
TYPE:
|
compare
abstractmethod
The method compares if the current object from the
snapshot.check call and the object loaded with the load
method are considered to be the same. If this returns True
the snapshot.check call will pass.
| PARAMETER | DESCRIPTION |
|---|---|
current_obj
|
The object the snapshot handler receiving from
TYPE:
|
recorded_obj
|
The object the snapshot handler is loading with
the
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Boolean value which decides is the |
load
abstractmethod
This method loads an existing snapshot from the given folder and must
be consistent with the save method.
| PARAMETER | DESCRIPTION |
|---|---|
folder
|
Path to the folder. The folder exists already when this method is called.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
The loaded object. |
save
abstractmethod
This method is called when a snapshot is reset, and a subclass must store the given object in the given folder. How this folder is managed internally is entirely up to the snapshot handler. It can be a single file, or a complex structure of multiple files and sub-folders.
| PARAMETER | DESCRIPTION |
|---|---|
folder
|
Path to the folder. The folder exists already when this method is called.
TYPE:
|
obj
|
The object from the
TYPE:
|
show
abstractmethod
This method returns a line wise textual representation of the given object.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The object the snapshot handler is managing.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of strings. |
show_differences
abstractmethod
show_differences(
current_obj: Any,
recorded_obj: Any,
has_markup: bool,
use_ddiff: bool,
) -> list[str]
The method shows differences between the object from the
snapshot.check call and the object loaded with the load
method.
| PARAMETER | DESCRIPTION |
|---|---|
current_obj
|
The object the snapshot handler receiving from
TYPE:
|
recorded_obj
|
The object the snapshot handler is loading with
the
TYPE:
|
has_markup
|
Indicates if the tests run within a terminal and
the method can use color output in case
TYPE:
|
use_ddiff
|
Use the ddiff library to print diffs, may not be supported by all handlers.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of strings to describe the differences. |