Skip to content

Commit

Permalink
Use pytest.raises()
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed May 10, 2022
1 parent f9da3fb commit 044c285
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
9 changes: 6 additions & 3 deletions tests/messages/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import unittest
from io import BytesIO, StringIO

import pytest

from babel.messages import extract


Expand Down Expand Up @@ -380,8 +382,8 @@ def test_utf8_bom_with_latin_magic_comment_fails(self):
# NOTE: hello
msg = _('Bonjour à tous')
""".encode('utf-8'))
self.assertRaises(SyntaxError, list,
extract.extract_python(buf, ('_',), ['NOTE:'], {}))
with pytest.raises(SyntaxError):
list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))

def test_utf8_raw_strings_match_unicode_strings(self):
buf = BytesIO(codecs.BOM_UTF8 + u"""
Expand Down Expand Up @@ -466,7 +468,8 @@ def test_invalid_filter(self):

def test_invalid_extract_method(self):
buf = BytesIO(b'')
self.assertRaises(ValueError, list, extract.extract('spam', buf))
with pytest.raises(ValueError):
list(extract.extract('spam', buf))

def test_different_signatures(self):
buf = BytesIO(b"""
Expand Down
27 changes: 11 additions & 16 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,12 @@ def test_hour_formatting(self):
class FormatDateTestCase(unittest.TestCase):

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

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

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

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

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


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

def test_format_invalid(self):
self.assertRaises(TypeError, dates.format_timedelta,
timedelta(hours=1), format='')
self.assertRaises(TypeError, dates.format_timedelta,
timedelta(hours=1), format='bold italic')
self.assertRaises(TypeError, dates.format_timedelta,
timedelta(hours=1), format=None)
for format in (None, '', 'bold italic'):
with pytest.raises(TypeError):
dates.format_timedelta(timedelta(hours=1), format=format)


class TimeZoneAdjustTestCase(unittest.TestCase):
Expand Down

0 comments on commit 044c285

Please sign in to comment.