From 9e74d0464d553a8dbc44df0c8eb661e427acb9b1 Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Sun, 22 Nov 2026 15:01:04 +0100 Subject: [PATCH 1/3] tests: rewrite the asserts of tests/utils as the ones of the tests, so that a failed one shows its values --- conftest.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/conftest.py b/conftest.py index c90a995..089b204 100644 --- a/conftest.py +++ b/conftest.py @@ -8,6 +8,11 @@ import pytest +# pytest shows the values of a failed assert only in the modules that it rewrites, the tests and the conftest files: the asserts of the +# helpers would fail with a bare AssertionError. They are rewritten too, under both the names that they are imported with, before the +# first import of them +pytest.register_assert_rewrite("utils", "tests.utils") + from tests.utils.gen_utils import GenUtils from tests.utils.test_utils import TestUtils from tests.utils.wallet_calls import NOT_SUPPORTED_HINTS From 03bc764ce144893e1acaedf61dc8417ca78b532d Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Sun, 22 Nov 2026 15:01:04 +0100 Subject: [PATCH 2/3] tests: drop the messages of the asserts that only repeat a value that pytest shows --- .../view_only_and_offline_wallet_tester.py | 6 +++--- tests/utils/wallet_error_utils.py | 18 +++++++++--------- tests/utils/wallet_notifications_tester.py | 2 +- tests/wallet/model/test_output_query.py | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/utils/view_only_and_offline_wallet_tester.py b/tests/utils/view_only_and_offline_wallet_tester.py index df77532..dca22ca 100644 --- a/tests/utils/view_only_and_offline_wallet_tester.py +++ b/tests/utils/view_only_and_offline_wallet_tester.py @@ -65,19 +65,19 @@ def _test_view_only_wallet(self) -> str: self._view_only_wallet.get_seed() raise Exception(err_msg) except Exception as e: - assert err_msg != str(e), str(e) + assert err_msg != str(e) try: self._view_only_wallet.get_seed_language() raise Exception(err_msg) except Exception as e: - assert err_msg != str(e), str(e) + assert err_msg != str(e) try: self._view_only_wallet.get_private_spend_key() raise Exception(err_msg) except Exception as e: - assert err_msg != str(e), str(e) + assert err_msg != str(e) # TODO: this fails with monero-wallet-rpc and monerod with authentication assert self._view_only_wallet.is_connected_to_daemon() diff --git a/tests/utils/wallet_error_utils.py b/tests/utils/wallet_error_utils.py index f60a440..f323fd6 100644 --- a/tests/utils/wallet_error_utils.py +++ b/tests/utils/wallet_error_utils.py @@ -23,7 +23,7 @@ def test_invalid_address_error(cls, ex: Exception, address: str | None = None) - err_msg: str = "Invalid address" if address is not None: err_msg = f"{err_msg}: {address}" - assert msg == err_msg, msg + assert msg == err_msg @classmethod def test_invalid_tx_hash_error(cls, ex: Exception) -> None: @@ -32,7 +32,7 @@ def test_invalid_tx_hash_error(cls, ex: Exception) -> None: :param Exception ex: exception to test. """ msg: str = str(ex) - assert msg == "TX hash has invalid format", msg + assert msg == "TX hash has invalid format" @classmethod def test_invalid_tx_key_error(cls, ex: Exception) -> None: @@ -41,7 +41,7 @@ def test_invalid_tx_key_error(cls, ex: Exception) -> None: :param Exception ex: exception to test. """ msg: str = str(ex) - assert msg == "Tx key has invalid format", msg + assert msg == "Tx key has invalid format" @classmethod def test_invalid_signature_error(cls, ex: Exception) -> None: @@ -50,7 +50,7 @@ def test_invalid_signature_error(cls, ex: Exception) -> None: :param Exception ex: exception to test. """ msg: str = str(ex) - assert msg == "Signature size mismatch with additional tx pubkeys", msg + assert msg == "Signature size mismatch with additional tx pubkeys" @classmethod def test_no_subaddress_error(cls, ex: Exception) -> None: @@ -59,7 +59,7 @@ def test_no_subaddress_error(cls, ex: Exception) -> None: :param Exception ex: exception to test. """ msg: str = str(ex) - assert msg == "Address must not be a subaddress", msg + assert msg == "Address must not be a subaddress" @classmethod def test_signature_header_error(cls, ex: Exception) -> None: @@ -68,7 +68,7 @@ def test_signature_header_error(cls, ex: Exception) -> None: :param Exception ex: exception to test. """ msg: str = str(ex) - assert msg == "Signature header check error", msg + assert msg == "Signature header check error" @classmethod def test_no_wallet_file_error(cls, error: Exception) -> None: @@ -77,7 +77,7 @@ def test_no_wallet_file_error(cls, error: Exception) -> None: :param Exception | None error: error to test. """ err_msg: str = str(error) - assert err_msg == "No wallet file", err_msg + assert err_msg == "No wallet file" @classmethod def test_wallet_is_closed_error(cls, error: Exception) -> None: @@ -86,7 +86,7 @@ def test_wallet_is_closed_error(cls, error: Exception) -> None: :param Exception | None error: error to test. """ err_msg: str = str(error) - assert err_msg == cls.WALLET_IS_CLOSED_ERROR, err_msg + assert err_msg == cls.WALLET_IS_CLOSED_ERROR @classmethod def test_wallet_is_not_connected_error(cls, error: Exception) -> None: @@ -105,4 +105,4 @@ def test_deprecated_payment_id_error(cls, error: Exception) -> None: :param Exception error: error to test. """ err_msg: str = str(error) - assert err_msg == "Standalone payment id deprecated, use integrated address instead", err_msg + assert err_msg == "Standalone payment id deprecated, use integrated address instead" diff --git a/tests/utils/wallet_notifications_tester.py b/tests/utils/wallet_notifications_tester.py index d37cf0a..b529ab0 100644 --- a/tests/utils/wallet_notifications_tester.py +++ b/tests/utils/wallet_notifications_tester.py @@ -99,7 +99,7 @@ def test(self, test_name: str) -> None: f"create_then_relay={self.create_then_relay}) generated {len(issues)} issues:\n{self.issues_to_str(issues)}" ) logger.warning(msg) - assert "ERROR:" not in msg, msg + assert "ERROR:" not in msg def run(self) -> list[str]: """Send the funds and test the notifications until they unlock. diff --git a/tests/wallet/model/test_output_query.py b/tests/wallet/model/test_output_query.py index 2c38233..925a311 100644 --- a/tests/wallet/model/test_output_query.py +++ b/tests/wallet/model/test_output_query.py @@ -24,7 +24,7 @@ def test_output_query(self) -> None: output_query.tx_query = tx_query # type: ignore except AttributeError as e: err_msg: str = str(e) - assert "object has no setter" in err_msg, err_msg + assert "object has no setter" in err_msg # assign tx query to output query output_query.set_tx_query(tx_query, True) From a59e78e739f39971079bd9cad58bbcb429f3a743 Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Sun, 22 Nov 2026 15:05:03 +0100 Subject: [PATCH 3/3] tests: check that the asserts of the helpers show their values when they fail --- docs/mkdocs/docs/en/tests/writing/helpers.md | 4 +++ tests/common/test_assert_rewrite.py | 30 ++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/common/test_assert_rewrite.py diff --git a/docs/mkdocs/docs/en/tests/writing/helpers.md b/docs/mkdocs/docs/en/tests/writing/helpers.md index a02b803..4f5c690 100644 --- a/docs/mkdocs/docs/en/tests/writing/helpers.md +++ b/docs/mkdocs/docs/en/tests/writing/helpers.md @@ -19,3 +19,7 @@ title: Helpers | `WalletCall`, `WALLET_CALLS` | a call of each method of the wallet interface | The data of a test (addresses, keys, seeds) goes in `tests/config`, in an `.ini` of its own or in the one of its topic. + +The asserts of the helpers show their values when they fail, as the ones of the tests: `conftest.py` has pytest rewrite them +(`register_assert_rewrite`), and `tests/common/test_assert_rewrite.py` checks it. A message after an assert is for what the values do +not say: `assert msg == "Invalid address"` already shows `msg`. diff --git a/tests/common/test_assert_rewrite.py b/tests/common/test_assert_rewrite.py new file mode 100644 index 0000000..49265b7 --- /dev/null +++ b/tests/common/test_assert_rewrite.py @@ -0,0 +1,30 @@ +import pytest + +from utils import BaseTestClass +# the modules, not their classes: a class imported here with a name that starts with Test would be collected as a test +import utils.gen_utils as helpers_of_the_tests +import tests.utils.gen_utils as helpers_of_conftest + + +@pytest.mark.unit +class TestAssertRewrite(BaseTestClass): + """The asserts of the helpers of tests/utils show their values when they fail, as the ones of the tests: conftest.py has pytest + rewrite them. Without it, a failed assert of a helper is a bare AssertionError, with only its message if it has one.""" + + #region Tests + + # A helper imported as the tests import it, from utils + def test_helpers_of_the_tests_show_the_values_of_a_failed_assert(self) -> None: + with pytest.raises(AssertionError) as exc_info: + helpers_of_the_tests.GenUtils.test_unsigned_big_integer(-5) + # its message, and the comparison with the value that failed it, which only a rewritten assert has + assert "Value cannot be negative" in str(exc_info.value) + assert "assert -5 >= 0" in str(exc_info.value) + + # The same helper imported as conftest.py imports it, from tests.utils + def test_helpers_of_conftest_show_the_values_of_a_failed_assert(self) -> None: + with pytest.raises(AssertionError) as exc_info: + helpers_of_conftest.GenUtils.test_unsigned_big_integer(-5) + assert "assert -5 >= 0" in str(exc_info.value) + + #endregion