Skip to content

Commit

Permalink
test: Use pytest-snapshot for error tests (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-koch authored Oct 9, 2024
1 parent 9600411 commit 3aecbf7
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 37 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dev-dependencies = [
"pytest >=8.3.2,<9",
"pytest-cov >=5.0.0,<6",
"pytest-notebook >=0.10.0,<0.11",
"pytest-snapshot >=0.9.0,<1",
"mypy ==1.10.0",
"pre-commit >=3.6.0,<4",
"ruff >=0.6.2,<0.7",
Expand Down
4 changes: 2 additions & 2 deletions tests/error/test_array_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@


@pytest.mark.parametrize("file", files)
def test_array_errors(file, capsys):
run_error_test(file, capsys)
def test_array_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)
4 changes: 2 additions & 2 deletions tests/error/test_comprehension_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@


@pytest.mark.parametrize("file", files)
def test_comprehension_errors(file, capsys):
run_error_test(file, capsys)
def test_comprehension_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)
4 changes: 2 additions & 2 deletions tests/error/test_errors_on_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@


@pytest.mark.parametrize("file", files)
def test_errors_on_usage(file, capsys):
run_error_test(file, capsys)
def test_errors_on_usage(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)
4 changes: 2 additions & 2 deletions tests/error/test_experimental_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@


@pytest.mark.parametrize("file", files)
def test_experimental_errors(file, capsys):
def test_experimental_errors(file, capsys, snapshot):
with disable_experimental_features():
run_error_test(file, capsys)
run_error_test(file, capsys, snapshot)
4 changes: 2 additions & 2 deletions tests/error/test_inout_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@


@pytest.mark.parametrize("file", files)
def test_inout_errors(file, capsys):
run_error_test(file, capsys)
def test_inout_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)
4 changes: 2 additions & 2 deletions tests/error/test_iter_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@


@pytest.mark.parametrize("file", files)
def test_iter_errors(file, capsys):
run_error_test(file, capsys)
def test_iter_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)
4 changes: 2 additions & 2 deletions tests/error/test_linear_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@


@pytest.mark.parametrize("file", files)
def test_linear_errors(file, capsys):
run_error_test(file, capsys)
def test_linear_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)
4 changes: 2 additions & 2 deletions tests/error/test_misc_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@


@pytest.mark.parametrize("file", files)
def test_misc_errors(file, capsys):
run_error_test(file, capsys)
def test_misc_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)


def test_extern_bad_type_syntax():
Expand Down
4 changes: 2 additions & 2 deletions tests/error/test_nested_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@


@pytest.mark.parametrize("file", files)
def test_nested_errors(file, capsys):
run_error_test(file, capsys)
def test_nested_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)
4 changes: 2 additions & 2 deletions tests/error/test_poly_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@


@pytest.mark.parametrize("file", files)
def test_type_errors(file, capsys):
run_error_test(file, capsys)
def test_type_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)
8 changes: 4 additions & 4 deletions tests/error/test_py_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@


@pytest.mark.parametrize("file", files)
def test_py_errors(file, capsys):
run_error_test(file, capsys)
def test_py_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)


@pytest.mark.skipif(tket2_installed, reason="tket2 is installed")
def test_tket2_not_installed(capsys):
def test_tket2_not_installed(capsys, snapshot):
path = (
pathlib.Path(__file__).parent.resolve() / "py_errors" / "tket2_not_installed.py"
)
run_error_test(str(path), capsys)
run_error_test(str(path), capsys, snapshot)
4 changes: 2 additions & 2 deletions tests/error/test_struct_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@


@pytest.mark.parametrize("file", files)
def test_struct_errors(file, capsys):
run_error_test(file, capsys)
def test_struct_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)
4 changes: 2 additions & 2 deletions tests/error/test_tensor_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@


@pytest.mark.parametrize("file", files)
def test_type_errors(file, capsys):
run_error_test(file, capsys)
def test_type_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)
4 changes: 2 additions & 2 deletions tests/error/test_type_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@


@pytest.mark.parametrize("file", files)
def test_type_errors(file, capsys):
run_error_test(file, capsys)
def test_type_errors(file, capsys, snapshot):
run_error_test(file, capsys, snapshot)
10 changes: 4 additions & 6 deletions tests/error/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@
import guppylang.decorator as decorator


def run_error_test(file, capsys):
def run_error_test(file, capsys, snapshot):
file = pathlib.Path(file)

with pytest.raises(GuppyError):
importlib.import_module(f"tests.error.{file.parent.name}.{file.name}")

err = capsys.readouterr().err
err = err.replace(str(file), "$FILE")

with pathlib.Path(file.with_suffix(".err")).open() as f:
exp_err = f.read()

exp_err = exp_err.replace("$FILE", str(file))
assert err == exp_err
snapshot.snapshot_dir = str(file.parent)
snapshot.assert_match(err, file.with_suffix(".err").name)


util = GuppyModule("test")
Expand Down
20 changes: 19 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3aecbf7

Please sign in to comment.