From 7926f514fd3648ed157d590f122f587f8c087a4e Mon Sep 17 00:00:00 2001 From: Hao Zhang Date: Sun, 31 Aug 2025 12:20:08 +0800 Subject: [PATCH] Add assertion message matching in tests. --- tests/arithmetic_test.py | 16 ++++++++-------- tests/conversion_test.py | 6 +++--- tests/creation_test.py | 6 +++--- tests/permute_test.py | 12 ++++++------ tests/reverse_test.py | 10 +++++----- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/tests/arithmetic_test.py b/tests/arithmetic_test.py index d63c00c..8f56b59 100644 --- a/tests/arithmetic_test.py +++ b/tests/arithmetic_test.py @@ -220,29 +220,29 @@ def test_arithmetic_fail(mismatch_tensors: tuple[GrassmannTensor, GrassmannTenso tensor_a, tensor_b = mismatch_tensors # Test __add__ method. - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="must match for arithmetic operations"): tensor_a + tensor_b - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="must match for arithmetic operations"): tensor_c = tensor_a.clone() tensor_c += tensor_b # Test __sub__ method. - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="must match for arithmetic operations"): tensor_a - tensor_b - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="must match for arithmetic operations"): tensor_c = tensor_a.clone() tensor_c -= tensor_b # Test __mul__ method. - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="must match for arithmetic operations"): tensor_a * tensor_b - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="must match for arithmetic operations"): tensor_c = tensor_a.clone() tensor_c *= tensor_b # Test __truediv__ method. - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="must match for arithmetic operations"): tensor_a / tensor_b - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="must match for arithmetic operations"): tensor_c = tensor_a.clone() tensor_c /= tensor_b diff --git a/tests/conversion_test.py b/tests/conversion_test.py index 6c49f82..0151dc8 100644 --- a/tests/conversion_test.py +++ b/tests/conversion_test.py @@ -52,9 +52,9 @@ def test_conversion( def test_conversion_duplicated_value(x: GrassmannTensor) -> None: - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="Duplicate device specification"): x.to(torch.device("cpu"), device=torch.device("cpu")) - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="Duplicate dtype specification"): x.to(torch.complex128, dtype=torch.complex128) - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="Duplicate device specification"): x.to("cpu", device=torch.device("cpu")) diff --git a/tests/creation_test.py b/tests/creation_test.py index fd8adbc..e4eb406 100644 --- a/tests/creation_test.py +++ b/tests/creation_test.py @@ -20,7 +20,7 @@ def test_creation_success(x: Initialization) -> None: ((False, True), ((1, 1), (2, 2), (1, 1)), torch.randn([2, 4, 2])), ]) def test_creation_invalid_arrow(x: Initialization) -> None: - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="Arrow length"): GrassmannTensor(*x) @@ -30,7 +30,7 @@ def test_creation_invalid_arrow(x: Initialization) -> None: ((False, True, False), ((1, 1), (1, 1)), torch.randn([2, 4, 2])), ]) def test_creation_invalid_edges(x: Initialization) -> None: - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="Edges length"): GrassmannTensor(*x) @@ -40,5 +40,5 @@ def test_creation_invalid_edges(x: Initialization) -> None: ((False, True, False), ((1, 1), (2, 2), (1, 1)), torch.randn([4, 4, 2])), ]) def test_creation_invalid_shape(x: Initialization) -> None: - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match="must equal sum of"): GrassmannTensor(*x) diff --git a/tests/permute_test.py b/tests/permute_test.py index 99bb2fe..f7197f7 100644 --- a/tests/permute_test.py +++ b/tests/permute_test.py @@ -24,18 +24,18 @@ def test_permute(x: PermuteCase) -> None: assert torch.allclose(result.tensor, expected) -PermuteFailCase = tuple[tuple[bool, ...], tuple[tuple[int, int], ...], torch.Tensor, tuple[int, ...]] +PermuteFailCase = tuple[tuple[bool, ...], tuple[tuple[int, int], ...], torch.Tensor, tuple[int, ...], str] @pytest.mark.parametrize("x", [ - ((False, False), ((1, 1), (1, 1)), torch.tensor([[1, 0], [0, 4]]), (0, 0)), - ((False, False), ((1, 1), (1, 1)), torch.tensor([[1, 0], [0, 4]]), (2, 0)), - ((False, False), ((1, 1), (1, 1)), torch.tensor([[1, 0], [0, 4]]), (0, 0, 1)), + ((False, False), ((1, 1), (1, 1)), torch.tensor([[1, 0], [0, 4]]), (0, 0), "Permutation indices must be unique"), + ((False, False), ((1, 1), (1, 1)), torch.tensor([[1, 0], [0, 4]]), (2, 0), "Permutation indices must cover all dimensions"), + ((False, False), ((1, 1), (1, 1)), torch.tensor([[1, 0], [0, 4]]), (0, 0, 1), "Permutation indices must be unique"), ]) def test_permute_fail(x: PermuteFailCase) -> None: - arrow, edges, tensor, before_by_after = x + arrow, edges, tensor, before_by_after, message = x grassmann_tensor = GrassmannTensor(arrow, edges, tensor) - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match=message): grassmann_tensor.permute(before_by_after) diff --git a/tests/reverse_test.py b/tests/reverse_test.py index 7e1b3c6..59cb9f8 100644 --- a/tests/reverse_test.py +++ b/tests/reverse_test.py @@ -27,15 +27,15 @@ def test_reverse(x: ReverseCase) -> None: assert torch.allclose(result.tensor, expected) -ReverseFailCase = tuple[tuple[bool, ...], tuple[tuple[int, int], ...], torch.Tensor, tuple[int, ...]] +ReverseFailCase = tuple[tuple[bool, ...], tuple[tuple[int, int], ...], torch.Tensor, tuple[int, ...], str] @pytest.mark.parametrize("x", [ - ((False, False), ((1, 1), (1, 1)), torch.tensor([[1, 0], [0, 4]]), (0, 0)), - ((False, False), ((1, 1), (1, 1)), torch.tensor([[1, 0], [0, 4]]), (2,)), + ((False, False), ((1, 1), (1, 1)), torch.tensor([[1, 0], [0, 4]]), (0, 0), "Indices must be unique"), + ((False, False), ((1, 1), (1, 1)), torch.tensor([[1, 0], [0, 4]]), (2,), "Indices must be within tensor dimensions"), ]) def test_reverse_fail(x: ReverseFailCase) -> None: - arrow, edges, tensor, reverse_by = x + arrow, edges, tensor, reverse_by, message = x grassmann_tensor = GrassmannTensor(arrow, edges, tensor) - with pytest.raises(AssertionError): + with pytest.raises(AssertionError, match=message): grassmann_tensor.reverse(reverse_by)