Ts Lib Pytest #
Version #
v0.2.2
Table of Contents #
Summary#
This repo contains a pytest plugin, that when added to a task script environment gives access to TetraScience specific fixtures.
Installation#
poetry add --group dev ts-lib-pytest
For DataFrame snapshot testing with Polars support:
poetry add --group dev "ts-lib-pytest[polars]"
Fixtures#
mock_uuid_generator#
This fixture seeds and mocks the TaskScriptUUIDGenerator object, and mocks the task_script_utils.uuid.uuid function to return the UUID string of the form 00000000-0000-0000-0000-00000000000x, where x increments by 1 for each call.
The fixture can be used as follows:
import pytest
@pytest.mark.usefixtures("mock_uuid_generator")
def test_task_script_uuid_generator():
# Act
result = function_which_calls_uuid_generator(...)
# Assert
assert result.pk == "00000000-0000-0000-0000-000000000001"
df_snapshot#
This fixture provides a DataframeSnapshot instance automatically configured with a snapshot file path based on your test file and function name. The path follows the pattern: <test_file_stem>_snapshots/<test_name>.parquet.
The DataframeSnapshot class stores snapshots as Parquet files and uses polars.testing.assert_frame_equal for comparison.
import polars as pl
from ts_lib_pytest.df_snapshot import DataframeSnapshot
def test_my_dataframe(df_snapshot: DataframeSnapshot):
# Arrange
df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
# Act (do the action to be tested here)
# Assert
df_snapshot.assert_frame_equal(df)
To update snapshots, run your tests with the --df-snapshot-update flag:
pytest --df-snapshot-update
Note: The --snapshot-update flag is also supported so that dataframe snapshot tests can be updated with the same flag as other pytest-snapshot tests.
Features#
Automatic snapshot creation on first run
Clear diff output when snapshots don’t match
Support for all
polars.testing.assert_frame_equalparametersGraceful handling when polars is not installed (import works, usage fails with helpful error)
Changelog#
v0.2.2#
Add LICENSE
v0.2.1#
Add
df_snapshotfixture to simplify usage ofDataframeSnapshot
v0.2.0#
Update the
mock_uuid_generatorfixture to mocktask_script_utils.uuid._uuid
v0.1.0#
Add the
mock_uuid_generatorfixture