Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/mkdocs/docs/en/tests/writing/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
30 changes: 30 additions & 0 deletions tests/common/test_assert_rewrite.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions tests/utils/view_only_and_offline_wallet_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 9 additions & 9 deletions tests/utils/wallet_error_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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"
2 changes: 1 addition & 1 deletion tests/utils/wallet_notifications_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tests/wallet/model/test_output_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading