Skip to content

Commit 6fe076a

Browse files
authored
Use pytest.raises(match=...) (#1166)
1 parent 1174cd1 commit 6fe076a

File tree

4 files changed

+9
-20
lines changed

4 files changed

+9
-20
lines changed

tests/messages/test_pofile.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,9 +1088,8 @@ def test_issue_1134(case: str, abort_invalid: bool):
10881088

10891089
if abort_invalid:
10901090
# Catalog not created, aborted with PoFileError
1091-
with pytest.raises(pofile.PoFileError) as excinfo:
1091+
with pytest.raises(pofile.PoFileError, match="missing msgstr for msgid 'foo' on 0"):
10921092
pofile.read_po(buf, abort_invalid=True)
1093-
assert str(excinfo.value) == "missing msgstr for msgid 'foo' on 0"
10941093
else:
10951094
# Catalog is created with warning, no abort
10961095
output = pofile.read_po(buf)

tests/test_core.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,8 @@ def test_parse_locale():
296296
assert core.parse_locale('zh_Hans_CN') == ('zh', 'CN', 'Hans', None)
297297
assert core.parse_locale('zh-CN', sep='-') == ('zh', 'CN', None, None)
298298

299-
with pytest.raises(ValueError) as excinfo:
299+
with pytest.raises(ValueError, match="'not_a_LOCALE_String' is not a valid locale identifier"):
300300
core.parse_locale('not_a_LOCALE_String')
301-
assert (excinfo.value.args[0] ==
302-
"'not_a_LOCALE_String' is not a valid locale identifier")
303301

304302
assert core.parse_locale('it_IT@euro') == ('it', 'IT', None, None, 'euro')
305303
assert core.parse_locale('it_IT@something') == ('it', 'IT', None, None, 'something')

tests/test_numbers.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,8 @@ def test_list_currencies():
244244
assert isinstance(list_currencies(locale='fr'), set)
245245
assert list_currencies('fr').issuperset(['BAD', 'BAM', 'KRO'])
246246

247-
with pytest.raises(ValueError) as excinfo:
247+
with pytest.raises(ValueError, match="expected only letters, got 'yo!'"):
248248
list_currencies('yo!')
249-
assert excinfo.value.args[0] == "expected only letters, got 'yo!'"
250249

251250
assert list_currencies(locale='pa_Arab') == {'PKR', 'INR', 'EUR'}
252251

@@ -256,9 +255,8 @@ def test_list_currencies():
256255
def test_validate_currency():
257256
validate_currency('EUR')
258257

259-
with pytest.raises(UnknownCurrencyError) as excinfo:
258+
with pytest.raises(UnknownCurrencyError, match="Unknown currency 'FUU'."):
260259
validate_currency('FUU')
261-
assert excinfo.value.args[0] == "Unknown currency 'FUU'."
262260

263261

264262
def test_is_currency():
@@ -514,10 +512,8 @@ def test_format_currency_format_type():
514512
format_type="accounting")
515513
== '$0.00')
516514

517-
with pytest.raises(numbers.UnknownCurrencyFormatError) as excinfo:
518-
numbers.format_currency(1099.98, 'USD', locale='en_US',
519-
format_type='unknown')
520-
assert excinfo.value.args[0] == "'unknown' is not a known currency format type"
515+
with pytest.raises(numbers.UnknownCurrencyFormatError, match="'unknown' is not a known currency format type"):
516+
numbers.format_currency(1099.98, 'USD', locale='en_US', format_type='unknown')
521517

522518
assert (numbers.format_currency(1099.98, 'JPY', locale='en_US')
523519
== '\xa51,100')
@@ -757,9 +753,8 @@ def test_parse_number():
757753
assert numbers.parse_number('1.099', locale='de_DE') == 1099
758754
assert numbers.parse_number('1٬099', locale='ar_EG', numbering_system="default") == 1099
759755

760-
with pytest.raises(numbers.NumberFormatError) as excinfo:
756+
with pytest.raises(numbers.NumberFormatError, match="'1.099,98' is not a valid number"):
761757
numbers.parse_number('1.099,98', locale='de')
762-
assert excinfo.value.args[0] == "'1.099,98' is not a valid number"
763758

764759
with pytest.raises(numbers.UnsupportedNumberingSystemError):
765760
numbers.parse_number('1.099,98', locale='en', numbering_system="unsupported")
@@ -778,9 +773,8 @@ def test_parse_decimal():
778773
== decimal.Decimal('1099.98'))
779774
assert numbers.parse_decimal('1.099,98', locale='de') == decimal.Decimal('1099.98')
780775

781-
with pytest.raises(numbers.NumberFormatError) as excinfo:
776+
with pytest.raises(numbers.NumberFormatError, match="'2,109,998' is not a valid decimal number"):
782777
numbers.parse_decimal('2,109,998', locale='de')
783-
assert excinfo.value.args[0] == "'2,109,998' is not a valid decimal number"
784778

785779

786780
@pytest.mark.parametrize('string', [

tests/test_support.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,9 @@ def raise_attribute_error():
299299
raise AttributeError('message')
300300

301301
proxy = support.LazyProxy(raise_attribute_error)
302-
with pytest.raises(AttributeError) as exception:
302+
with pytest.raises(AttributeError, match='message'):
303303
_ = proxy.value
304304

305-
assert str(exception.value) == 'message'
306-
307305

308306
class TestFormat:
309307
def test_format_datetime(self, timezone_getter):

0 commit comments

Comments
 (0)