From: Aarni Koskela Date: Thu, 9 Jan 2025 12:44:36 +0000 (+0200) Subject: Use `pytest.raises(match=...)` (#1166) X-Git-Tag: v2.17.0~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6fe076a99beefac46c2d4b215a06ccd41e9f96e9;p=thirdparty%2Fbabel.git Use `pytest.raises(match=...)` (#1166) --- diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py index ed9da78f..ab7c7f4b 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -1088,9 +1088,8 @@ def test_issue_1134(case: str, abort_invalid: bool): if abort_invalid: # Catalog not created, aborted with PoFileError - with pytest.raises(pofile.PoFileError) as excinfo: + with pytest.raises(pofile.PoFileError, match="missing msgstr for msgid 'foo' on 0"): pofile.read_po(buf, abort_invalid=True) - assert str(excinfo.value) == "missing msgstr for msgid 'foo' on 0" else: # Catalog is created with warning, no abort output = pofile.read_po(buf) diff --git a/tests/test_core.py b/tests/test_core.py index 2dc571f5..8aa21b4c 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -296,10 +296,8 @@ def test_parse_locale(): assert core.parse_locale('zh_Hans_CN') == ('zh', 'CN', 'Hans', None) assert core.parse_locale('zh-CN', sep='-') == ('zh', 'CN', None, None) - with pytest.raises(ValueError) as excinfo: + with pytest.raises(ValueError, match="'not_a_LOCALE_String' is not a valid locale identifier"): core.parse_locale('not_a_LOCALE_String') - assert (excinfo.value.args[0] == - "'not_a_LOCALE_String' is not a valid locale identifier") assert core.parse_locale('it_IT@euro') == ('it', 'IT', None, None, 'euro') assert core.parse_locale('it_IT@something') == ('it', 'IT', None, None, 'something') diff --git a/tests/test_numbers.py b/tests/test_numbers.py index 9787afd1..f3e2dec7 100644 --- a/tests/test_numbers.py +++ b/tests/test_numbers.py @@ -244,9 +244,8 @@ def test_list_currencies(): assert isinstance(list_currencies(locale='fr'), set) assert list_currencies('fr').issuperset(['BAD', 'BAM', 'KRO']) - with pytest.raises(ValueError) as excinfo: + with pytest.raises(ValueError, match="expected only letters, got 'yo!'"): list_currencies('yo!') - assert excinfo.value.args[0] == "expected only letters, got 'yo!'" assert list_currencies(locale='pa_Arab') == {'PKR', 'INR', 'EUR'} @@ -256,9 +255,8 @@ def test_list_currencies(): def test_validate_currency(): validate_currency('EUR') - with pytest.raises(UnknownCurrencyError) as excinfo: + with pytest.raises(UnknownCurrencyError, match="Unknown currency 'FUU'."): validate_currency('FUU') - assert excinfo.value.args[0] == "Unknown currency 'FUU'." def test_is_currency(): @@ -514,10 +512,8 @@ def test_format_currency_format_type(): format_type="accounting") == '$0.00') - with pytest.raises(numbers.UnknownCurrencyFormatError) as excinfo: - numbers.format_currency(1099.98, 'USD', locale='en_US', - format_type='unknown') - assert excinfo.value.args[0] == "'unknown' is not a known currency format type" + with pytest.raises(numbers.UnknownCurrencyFormatError, match="'unknown' is not a known currency format type"): + numbers.format_currency(1099.98, 'USD', locale='en_US', format_type='unknown') assert (numbers.format_currency(1099.98, 'JPY', locale='en_US') == '\xa51,100') @@ -757,9 +753,8 @@ def test_parse_number(): assert numbers.parse_number('1.099', locale='de_DE') == 1099 assert numbers.parse_number('1Ù¬099', locale='ar_EG', numbering_system="default") == 1099 - with pytest.raises(numbers.NumberFormatError) as excinfo: + with pytest.raises(numbers.NumberFormatError, match="'1.099,98' is not a valid number"): numbers.parse_number('1.099,98', locale='de') - assert excinfo.value.args[0] == "'1.099,98' is not a valid number" with pytest.raises(numbers.UnsupportedNumberingSystemError): numbers.parse_number('1.099,98', locale='en', numbering_system="unsupported") @@ -778,9 +773,8 @@ def test_parse_decimal(): == decimal.Decimal('1099.98')) assert numbers.parse_decimal('1.099,98', locale='de') == decimal.Decimal('1099.98') - with pytest.raises(numbers.NumberFormatError) as excinfo: + with pytest.raises(numbers.NumberFormatError, match="'2,109,998' is not a valid decimal number"): numbers.parse_decimal('2,109,998', locale='de') - assert excinfo.value.args[0] == "'2,109,998' is not a valid decimal number" @pytest.mark.parametrize('string', [ diff --git a/tests/test_support.py b/tests/test_support.py index a102ae66..65147210 100644 --- a/tests/test_support.py +++ b/tests/test_support.py @@ -299,11 +299,9 @@ class LazyProxyTestCase(unittest.TestCase): raise AttributeError('message') proxy = support.LazyProxy(raise_attribute_error) - with pytest.raises(AttributeError) as exception: + with pytest.raises(AttributeError, match='message'): _ = proxy.value - assert str(exception.value) == 'message' - class TestFormat: def test_format_datetime(self, timezone_getter):