Skip to content

Commit

Permalink
Pytest plugin should now install automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
AKuederle committed Apr 17, 2024
1 parent 30c6ce1 commit 8c155a1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ readme = "README.md"
homepage = "https://github.com/mad-lab-fau/tpcp"
repository = "https://github.com/mad-lab-fau/tpcp"

[tool.poetry.plugins."pytest11"]
tpcp_snapshots = "tpcp.testing._regression_utils"

[tool.poetry.dependencies]
python = ">=3.9,<4.0"
joblib = ">=1.3"
Expand Down
57 changes: 28 additions & 29 deletions tpcp/testing/_regression_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,11 @@ class PyTestSnapshotTest:
This supports standard datatypes and scientific datatypes like numpy arrays and pandas DataFrames.
To use this in your tests, add the following lines to your conftest.py:
.. code-block:: python
@pytest.fixture()
def snapshot(request):
with PyTestSnapshotTest(request) as snapshot_test:
yield snapshot_test
def pytest_addoption(parser):
group = parser.getgroup("snapshottest")
group.addoption(
"--snapshot-update",
action="store_true",
default=False,
dest="snapshot_update",
help="Update the snapshots.",
)
group.addoption(
"--snapshot-only-check",
action="store_true",
default=False,
dest="snapshot_only_check",
help="Update the snapshots.",
)
This will register the snapshot fixture that you can use in your tests.
This plugin will be automatically registered when you install tpcp.
It adds the `snapshot` fixture to your tests.
Further, it will register the `--snapshot-update` commandline flag, which you can use to update the snapshots.
You can also run pytest with the `--snapshot-only-check` flag to fail if a snapshot file is not found.
Without that flag, missing snapshots will be automatically created.
To use the fixture in your tests, simply add it as a parameter to your test function:
Expand Down Expand Up @@ -210,3 +185,27 @@ def assert_match(self, value: Union[str, pd.DataFrame, np.ndarray], name: Option
raise TypeError(f"The dtype {value_dtype} is not supported for snapshot testing")

self.curr_snapshot_number += 1


@pytest.fixture()
def snapshot(request):
with PyTestSnapshotTest(request) as snapshot_test:
yield snapshot_test


def pytest_addoption(parser):
group = parser.getgroup("tpcp_snapshots")
group.addoption(
"--snapshot-update",
action="store_true",
default=False,
dest="snapshot_update",
help="Update the snapshots.",
)
group.addoption(
"--snapshot-only-check",
action="store_true",
default=False,
dest="snapshot_only_check",
help="Run as normal, but fail if a snapshot file is not found. This is usefull for CI runs.",
)

0 comments on commit 8c155a1

Please sign in to comment.