outsource(data)
General¶
Storing snapshots in the source code is the main feature of inline snapshots. This has the advantage that you can easily see changes in code reviews. But it also has some problems:
- It is problematic to snapshot a lot of data, because it takes up a lot of space in your tests.
- Binary data or images are not readable in your tests.
The outsource()
function solves this problem and integrates itself nicely with the inline snapshot.
It stores the data in a special external()
object that can be compared in snapshots.
The object is represented by the hash of the data.
The actual data is stored in a separate file in your project.
This allows the test to be renamed and moved around in your code without losing the connection to the stored data.
Example:
The external
object can be used inside other data structures.
API¶
inline_snapshot.outsource(data, *, suffix=None)
¶
Outsource some data into an external file.
>>> png_data = b"some_bytes" # should be the replaced with your actual data
>>> outsource(png_data, suffix=".png")
external("212974ed1835*.png")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Union[str, bytes]
|
data which should be outsourced. strings are encoded with |
required |
suffix
|
Optional[str]
|
overwrite file suffix. The default is |
None
|
Returns:
Type | Description |
---|---|
external
|
The external data. |
Source code in src/inline_snapshot/_external.py
inline_snapshot.external
¶
Source code in src/inline_snapshot/_external.py
__eq__(other)
¶
Two external objects are equal if they have the same hash and suffix.
Source code in src/inline_snapshot/_external.py
__init__(name)
¶
External objects are used as a representation for outsourced data. You should not create them directly.
The external data is stored inside <pytest_config_dir>/.inline_snapshot/external
,
where <pytest_config_dir>
is replaced by the directory containing the Pytest configuration file, if any.
Data which is outsourced but not referenced in the source code jet has a '-new' suffix in the filename.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
the name of the external stored object. |
required |
Source code in src/inline_snapshot/_external.py
__repr__()
¶
Returns the representation of the external object.
The length of the hash can be specified in the config.
Source code in src/inline_snapshot/_external.py
pytest options¶
It interacts with the following --inline-snapshot
flags:
trim
removes every snapshots form the storage which is not referenced withexternal(...)
in the code.