Skip to content

Commit

Permalink
Check exception subclass in assert_raises() instead of equal exceptio…
Browse files Browse the repository at this point in the history
…n types.
  • Loading branch information
eerimoq committed Jan 15, 2019
1 parent 61a5ab3 commit a4a1e47
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
20 changes: 13 additions & 7 deletions systest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


__author__ = 'Erik Moqvist'
__version__ = '3.10.0'
__version__ = '4.0.0'


_RUN_HEADER_FMT ="""
Expand Down Expand Up @@ -379,8 +379,8 @@ def assert_less_equal(self, first, second):
repr(second)))

def assert_raises(self, expected_type, expected_message=None):
"""Raise an exception if no exception of given type `exception_type`
is raised.
"""Raise an exception if no exception of given type(s) or subclass of
given type(s) `expected_type` is raised.
"""

Expand All @@ -398,11 +398,17 @@ def __exit__(self, exception_type, exception_value, tb):
if exception_type is None:
filename, line, _, _ = traceback.extract_stack()[-2]

try:
name = self.expected_type.__name__
except AttributeError:
name = ' or '.join([
expected_type.__name__
for expected_type in self.expected_type
])

raise SequencerTestFailedError(
'{}:{}: {} not raised'.format(filename,
line,
self.expected_type.__name__))
elif exception_type == self.expected_type:
'{}:{}: {} not raised'.format(filename, line, name))
elif issubclass(exception_type, self.expected_type):
# Python 2 and 3 compatibility.
try:
self.exception = exception_value.with_traceback(None)
Expand Down
30 changes: 27 additions & 3 deletions tests/test_systest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
from tests.testcases.asserts import AssertsLessTest
from tests.testcases.asserts import AssertsLessEqualTest
from tests.testcases.asserts import AssertsRaisesNoExceptionTest
from tests.testcases.asserts import AssertsRaisesNoExceptionTupleTest
from tests.testcases.asserts import AssertsRaisesWrongExceptionTest
from tests.testcases.asserts import AssertsRaisesWrongExceptionTupleTest
from tests.testcases.asserts import AssertsRaisesSubclassExceptionTest
from tests.testcases.asserts import AssertsRaisesExceptionTest
from tests.testcases.asserts import AssertsRaisesExceptionTupleTest
from tests.testcases.asserts import AssertsNoneTest
from tests.testcases.notexecuted import NotExecutedTest
from tests.testcases.description import DescriptionNoneTest
Expand Down Expand Up @@ -325,13 +330,15 @@ def test_failed_asserts(self):
AssertsLessTest(2, 2),
AssertsLessEqualTest(2, 1),
AssertsRaisesNoExceptionTest(),
AssertsRaisesNoExceptionTupleTest(),
AssertsRaisesWrongExceptionTest(),
AssertsRaisesWrongExceptionTupleTest(),
AssertsNoneTest(0)
)

sequencer.report()

self.assertEqual(result, (0, 15, 0))
self.assertEqual(result, (0, 17, 0))

# Failure messages.
with self.assertRaises(SequencerTestFailedError) as cm:
Expand Down Expand Up @@ -399,9 +406,23 @@ def test_failed_asserts(self):

self.assertTrue(str(cm.exception).endswith(': ValueError not raised'))

with self.assertRaises(SequencerTestFailedError) as cm:
AssertsRaisesNoExceptionTupleTest().run()

self.assertTrue(str(cm.exception).endswith(': ValueError or TypeError not raised'))

with self.assertRaises(TypeError) as cm:
AssertsRaisesWrongExceptionTest().run()

self.assertEqual(str(cm.exception), 'This is not a value error.')

with self.assertRaises(IndexError) as cm:
AssertsRaisesWrongExceptionTupleTest().run()

self.assertEqual(str(cm.exception), 'This is not a value error or type error.')

with self.assertRaises(SequencerTestFailedError) as cm:
AssertsNoneTest(0).run()
print(str(cm.exception))

self.assertTrue(str(cm.exception).endswith(': 0 is not None'))

Expand Down Expand Up @@ -431,12 +452,15 @@ def test_passed_asserts(self):
AssertsLessTest(1, 2),
AssertsLessEqualTest(1, 2),
AssertsLessEqualTest(2, 2),
AssertsRaisesSubclassExceptionTest(),
AssertsRaisesExceptionTest(),
AssertsRaisesExceptionTupleTest(),
AssertsNoneTest(None)
)

sequencer.report()

self.assertEqual(result, (19, 0, 0))
self.assertEqual(result, (22, 0, 0))


def test_testcase_description(self):
Expand Down
38 changes: 38 additions & 0 deletions tests/testcases/asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,51 @@ def run(self):
pass


class AssertsRaisesNoExceptionTupleTest(TestCase):

def run(self):
with self.assert_raises((ValueError, TypeError)):
pass


class AssertsRaisesWrongExceptionTest(TestCase):

def run(self):
with self.assert_raises(ValueError):
raise TypeError('This is not a value error.')


class AssertsRaisesSubclassExceptionTest(TestCase):

def run(self):
with self.assert_raises(Exception):
raise ValueError


class AssertsRaisesWrongExceptionTupleTest(TestCase):

def run(self):
with self.assert_raises((ValueError, TypeError)):
raise IndexError('This is not a value error or type error.')


class AssertsRaisesExceptionTest(TestCase):

def run(self):
with self.assert_raises(ValueError):
raise ValueError


class AssertsRaisesExceptionTupleTest(TestCase):

def run(self):
with self.assert_raises((ValueError, TypeError)):
raise ValueError

with self.assert_raises((ValueError, TypeError)):
raise TypeError


class AssertsNoneTest(TestCase):

def __init__(self, obj):
Expand Down

0 comments on commit a4a1e47

Please sign in to comment.