1 # Copyright (c) 2011 testtools developers. See LICENSE for details.
3 from __future__
import with_statement
7 from testtools
import (
11 from testtools
.matchers
import (
17 class TestExpectedException(TestCase
):
18 """Test the ExpectedException context manager."""
20 def test_pass_on_raise(self
):
21 with
ExpectedException(ValueError, 'tes.'):
22 raise ValueError('test')
24 def test_pass_on_raise_matcher(self
):
25 with
ExpectedException(
26 ValueError, AfterPreprocessing(str, Equals('test'))):
27 raise ValueError('test')
29 def test_raise_on_text_mismatch(self
):
31 with
ExpectedException(ValueError, 'tes.'):
32 raise ValueError('mismatch')
33 except AssertionError:
35 self
.assertEqual("'mismatch' does not match /tes./", str(e
))
37 self
.fail('AssertionError not raised.')
39 def test_raise_on_general_mismatch(self
):
40 matcher
= AfterPreprocessing(str, Equals('test'))
41 value_error
= ValueError('mismatch')
43 with
ExpectedException(ValueError, matcher
):
45 except AssertionError:
47 self
.assertEqual(matcher
.match(value_error
).describe(), str(e
))
49 self
.fail('AssertionError not raised.')
51 def test_raise_on_error_mismatch(self
):
53 with
ExpectedException(TypeError, 'tes.'):
54 raise ValueError('mismatch')
57 self
.assertEqual('mismatch', str(e
))
59 self
.fail('ValueError not raised.')
61 def test_raise_if_no_exception(self
):
63 with
ExpectedException(TypeError, 'tes.'):
65 except AssertionError:
67 self
.assertEqual('TypeError not raised.', str(e
))
69 self
.fail('AssertionError not raised.')
71 def test_pass_on_raise_any_message(self
):
72 with
ExpectedException(ValueError):
73 raise ValueError('whatever')