From 0abc77e8cf74e96126e3f151d3d4c8eb80352e58 Mon Sep 17 00:00:00 2001 From: Marcel Wilson Date: Thu, 8 Feb 2024 14:02:38 -0600 Subject: [PATCH] tests should assert reason for failure --- tests/test_actions.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/tests/test_actions.py b/tests/test_actions.py index 9b325f9..b556ee7 100644 --- a/tests/test_actions.py +++ b/tests/test_actions.py @@ -1,7 +1,8 @@ from __future__ import annotations import warnings -from typing import cast +from contextlib import contextmanager +from typing import Generator, cast from unittest import mock import pytest @@ -64,6 +65,20 @@ TARGET = FakeTarget() +@contextmanager +def not_raises(ExpectedException: type[Exception]) -> Generator: + try: + yield + + except ExpectedException as error: + msg = f"Incorrectly Raised {error}" + raise AssertionError(msg) from error + + except Exception as error: # noqa: BLE001 + msg = f"Unexpected exception {error}" + raise AssertionError(msg) from error + + class TestAcceptAlert: def test_can_be_instantiated(self) -> None: aa = AcceptAlert() @@ -441,11 +456,11 @@ def test_positional_arg_warns(self) -> None: Enter("", True) def test_keyword_arg_does_not_warn(self) -> None: - with warnings.catch_warnings(): + with not_raises(DeprecationWarning), warnings.catch_warnings(): warnings.simplefilter("error") Enter.the_secret("") - with warnings.catch_warnings(): + with not_raises(DeprecationWarning), warnings.catch_warnings(): warnings.simplefilter("error") Enter("", mask=True) @@ -638,11 +653,11 @@ def test_positional_arg_warns(self) -> None: HoldDown(None, True) def test_keyword_arg_does_not_warn(self) -> None: - with warnings.catch_warnings(): + with not_raises(DeprecationWarning), warnings.catch_warnings(): warnings.simplefilter("error") HoldDown.left_mouse_button() - with warnings.catch_warnings(): + with not_raises(DeprecationWarning), warnings.catch_warnings(): warnings.simplefilter("error") HoldDown(lmb=True) @@ -916,11 +931,11 @@ def test_positional_arg_warns(self) -> None: Release(None, True) def test_keyword_arg_does_not_warn(self) -> None: - with warnings.catch_warnings(): + with not_raises(DeprecationWarning), warnings.catch_warnings(): warnings.simplefilter("error") Release.left_mouse_button() - with warnings.catch_warnings(): + with not_raises(DeprecationWarning), warnings.catch_warnings(): warnings.simplefilter("error") Release(lmb=True)