Skip to content

Commit 4f805ee

Browse files
committed
Use pytest.raises()
1 parent 4fea711 commit 4f805ee

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

tests/messages/test_extract.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import unittest
1616
from io import BytesIO, StringIO
1717

18+
import pytest
19+
1820
from babel.messages import extract
1921

2022

@@ -380,8 +382,8 @@ def test_utf8_bom_with_latin_magic_comment_fails(self):
380382
# NOTE: hello
381383
msg = _('Bonjour à tous')
382384
""".encode('utf-8'))
383-
self.assertRaises(SyntaxError, list,
384-
extract.extract_python(buf, ('_',), ['NOTE:'], {}))
385+
with pytest.raises(SyntaxError):
386+
list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
385387

386388
def test_utf8_raw_strings_match_unicode_strings(self):
387389
buf = BytesIO(codecs.BOM_UTF8 + u"""
@@ -466,7 +468,8 @@ def test_invalid_filter(self):
466468

467469
def test_invalid_extract_method(self):
468470
buf = BytesIO(b'')
469-
self.assertRaises(ValueError, list, extract.extract('spam', buf))
471+
with pytest.raises(ValueError):
472+
list(extract.extract('spam', buf))
470473

471474
def test_different_signatures(self):
472475
buf = BytesIO(b"""

tests/test_dates.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,12 @@ def test_hour_formatting(self):
252252
class FormatDateTestCase(unittest.TestCase):
253253

254254
def test_with_time_fields_in_pattern(self):
255-
self.assertRaises(AttributeError, dates.format_date, date(2007, 4, 1),
256-
"yyyy-MM-dd HH:mm", locale='en_US')
255+
with pytest.raises(AttributeError):
256+
dates.format_date(date(2007, 4, 1), "yyyy-MM-dd HH:mm", locale='en_US')
257257

258258
def test_with_time_fields_in_pattern_and_datetime_param(self):
259-
self.assertRaises(AttributeError, dates.format_date,
260-
datetime(2007, 4, 1, 15, 30),
261-
"yyyy-MM-dd HH:mm", locale='en_US')
259+
with pytest.raises(AttributeError):
260+
dates.format_date(datetime(2007, 4, 1, 15, 30), "yyyy-MM-dd HH:mm", locale='en_US')
262261

263262
def test_with_day_of_year_in_pattern_and_datetime_param(self):
264263
# format_date should work on datetimes just as well (see #282)
@@ -362,13 +361,12 @@ def test_with_float(self):
362361
assert dates.format_time(epoch, format='long', locale='en_US') == u'3:30:29 PM UTC'
363362

364363
def test_with_date_fields_in_pattern(self):
365-
self.assertRaises(AttributeError, dates.format_time, date(2007, 4, 1),
366-
"yyyy-MM-dd HH:mm", locale='en_US')
364+
with pytest.raises(AttributeError):
365+
dates.format_time(datetime(2007, 4, 1), 'yyyy-MM-dd HH:mm', locale='en')
367366

368367
def test_with_date_fields_in_pattern_and_datetime_param(self):
369-
self.assertRaises(AttributeError, dates.format_time,
370-
datetime(2007, 4, 1, 15, 30),
371-
"yyyy-MM-dd HH:mm", locale='en_US')
368+
with pytest.raises(AttributeError):
369+
dates.format_time(datetime(2007, 4, 1, 15, 30), "yyyy-MM-dd HH:mm", locale='en_US')
372370

373371

374372
class FormatTimedeltaTestCase(unittest.TestCase):
@@ -395,12 +393,9 @@ def test_format_narrow(self):
395393
assert dates.format_timedelta(timedelta(hours=-2), locale='en', format='narrow') == '2h'
396394

397395
def test_format_invalid(self):
398-
self.assertRaises(TypeError, dates.format_timedelta,
399-
timedelta(hours=1), format='')
400-
self.assertRaises(TypeError, dates.format_timedelta,
401-
timedelta(hours=1), format='bold italic')
402-
self.assertRaises(TypeError, dates.format_timedelta,
403-
timedelta(hours=1), format=None)
396+
for format in (None, '', 'bold italic'):
397+
with pytest.raises(TypeError):
398+
dates.format_timedelta(timedelta(hours=1), format=format)
404399

405400

406401
class TimeZoneAdjustTestCase(unittest.TestCase):

0 commit comments

Comments
 (0)