1
# Copyright (c) 2011 testtools developers. See LICENSE for details.
3
from __future__ import with_statement
5
from testtools import (
10
class TestExpectedException(TestCase):
11
"""Test the ExpectedException context manager."""
13
def test_pass_on_raise(self):
14
with ExpectedException(ValueError, 'tes.'):
15
raise ValueError('test')
17
def test_raise_on_text_mismatch(self):
19
with ExpectedException(ValueError, 'tes.'):
20
raise ValueError('mismatch')
21
except AssertionError, e:
22
self.assertEqual('"mismatch" does not match "tes.".', str(e))
24
self.fail('AssertionError not raised.')
26
def test_raise_on_error_mismatch(self):
28
with ExpectedException(TypeError, 'tes.'):
29
raise ValueError('mismatch')
31
self.assertEqual('mismatch', str(e))
33
self.fail('ValueError not raised.')
35
def test_raise_if_no_exception(self):
37
with ExpectedException(TypeError, 'tes.'):
39
except AssertionError, e:
40
self.assertEqual('TypeError not raised.', str(e))
42
self.fail('AssertionError not raised.')