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,
*,
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:
|
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.
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],
)
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. |