File: test_should_raise.py

package info (click to toggle)
python-testfixtures 9.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,036 kB
  • sloc: python: 10,373; makefile: 76; sh: 9
file content (368 lines) | stat: -rw-r--r-- 12,978 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from textwrap import dedent

import pytest

from testfixtures import Comparison as C, ShouldRaise, should_raise, compare
from unittest import TestCase

from ..shouldraise import ShouldAssert, NoException


class TestShouldAssert:

    def test_no_exception(self) -> None:
        try:
            with ShouldAssert('foo'):
                pass
        except AssertionError as e:
            assert str(e) == "Expected AssertionError('foo'), None raised!"

    def test_wrong_exception(self) -> None:
        try:
            with ShouldAssert('foo'):
                raise KeyError()
        except KeyError:
            pass

    def test_wrong_text(self) -> None:
        try:
            with ShouldAssert('foo'):
                assert False, 'bar'
        except AssertionError as e:
            assert str(e) == dedent("""\
                --- expected
                +++ actual
                @@ -1 +1,2 @@
                -foo
                +bar
                +assert False""")

    def test_show_whitespace(self) -> None:
        try:
            with ShouldAssert('foo ', show_whitespace=True):
                assert False, ' foo'
        except AssertionError as e:
            assert str(e) == dedent(
                """\
                --- expected
                +++ actual
                @@ -1 +1,2 @@
                -'foo '
                +' foo\\n'
                +'assert False'"""
            )



class TestShouldRaise(TestCase):

    def test_no_params(self) -> None:
        def to_test() -> None:
            raise ValueError('wrong value supplied')
        should_raise(ValueError('wrong value supplied'))(to_test)()

    def test_no_exception(self) -> None:
        def to_test() -> None:
            pass
        with ShouldAssert('ValueError() (expected) != None (raised)'):
            should_raise(ValueError())(to_test)()

    def test_wrong_exception(self) -> None:
        def to_test() -> None:
            raise ValueError('bar')
        expected = "ValueError('foo') (expected) != ValueError('bar') (raised)"
        with ShouldAssert(expected):
            should_raise(ValueError('foo'))(to_test)()

    def test_only_exception_class(self) -> None:
        def to_test() -> None:
            raise ValueError('bar')
        should_raise(ValueError)(to_test)()

    def test_wrong_exception_class(self) -> None:
        expected_exception = ValueError('bar')
        def to_test() -> None:
            raise expected_exception
        try:
            should_raise(KeyError)(to_test)()
        except ValueError as actual_exception:
            assert actual_exception is expected_exception
        else:  # pragma: no cover
            self.fail(('Wrong exception raised'))

    def test_wrong_exception_type(self) -> None:
        expected_exception = ValueError('bar')
        def to_test() -> None:
            raise expected_exception
        try:
            should_raise(KeyError('foo'))(to_test)()
        except ValueError as actual_exception:
            assert actual_exception is expected_exception
        else:  # pragma: no cover
            self.fail(('Wrong exception raised'))

    def test_no_supplied_or_raised(self) -> None:
        # effectively we're saying "something should be raised!"
        # but we want to inspect s.raised rather than making
        # an up-front assertion
        def to_test() -> None:
            pass
        with ShouldAssert("No exception raised!"):
            should_raise()(to_test)()

    def test_args(self) -> None:
        def to_test(*args: object) -> None:
            raise ValueError('%s' % repr(args))
        should_raise(ValueError('(1,)'))(to_test)(1)

    def test_kw_to_args(self) -> None:
        def to_test(x: object) -> None:
            raise ValueError('%s' % x)
        should_raise(ValueError('1'))(to_test)(x=1)

    def test_kw(self) -> None:
        def to_test(**kw: object) -> None:
            raise ValueError('%r' % kw)
        should_raise(ValueError("{'x': 1}"))(to_test)(x=1)

    def test_both(self) -> None:
        def to_test(*args: object, **kw: object) -> None:
            raise ValueError('%r %r' % (args, kw))
        should_raise(ValueError("(1,) {'x': 2}"))(to_test)(1, x=2)

    def test_method_args(self) -> None:
        class X:
            def to_test(self, *args: object) -> None:
                self.args = args
                raise ValueError()
        x = X()
        should_raise(ValueError)(x.to_test)(1, 2, 3)
        self.assertEqual(x.args, (1, 2, 3))

    def test_method_kw(self) -> None:
        class X:
            def to_test(self, **kw: object) -> None:
                self.kw = kw
                raise ValueError()
        x = X()
        should_raise(ValueError)(x.to_test)(x=1, y=2)
        self.assertEqual(x.kw, {'x': 1, 'y': 2})

    def test_method_both(self) -> None:
        class X:
            def to_test(self, *args: object, **kw: object) -> None:
                self.args = args
                self.kw = kw
                raise ValueError()
        x = X()
        should_raise(ValueError)(x.to_test)(1, y=2)
        self.assertEqual(x.args, (1, ))
        self.assertEqual(x.kw, {'y': 2})

    def test_class_class(self) -> None:
        class Test:
            def __init__(self, x: object) -> None:
                # The TypeError is raised due to the mis-matched parameters
                # so the pass never gets executed
                pass  # pragma: no cover
        should_raise(TypeError)(Test)()  # type: ignore[call-arg]

    def test_raised(self) -> None:
        # If you're strict on typing, then if you want this pattern, you'll
        # need to specify the type, so might as well use ShouldRaise(ValueError)!
        with ShouldRaise[ValueError]() as s:
            raise ValueError('wrong value supplied')
        self.assertEqual(s.raised, C(ValueError('wrong value supplied')))

    def test_catch_baseexception_1(self) -> None:
        with ShouldRaise(SystemExit):
            raise SystemExit()

    def test_catch_baseexception_2(self) -> None:
        with ShouldRaise(KeyboardInterrupt):
            raise KeyboardInterrupt()

    def test_with_exception_class_supplied(self) -> None:
        with ShouldRaise(ValueError):
            raise ValueError('foo bar')

    def test_with_exception_supplied(self) -> None:
        with ShouldRaise(ValueError('foo bar')):
            raise ValueError('foo bar')

    def test_with_exception_supplied_wrong_args(self) -> None:
        expected = "ValueError('foo') (expected) != ValueError('bar') (raised)"
        with ShouldAssert(expected):
            with ShouldRaise(ValueError('foo')):
                raise ValueError('bar')

    def test_neither_supplied(self) -> None:
        with ShouldRaise():
            raise ValueError('foo bar')

    def test_with_no_exception_when_expected(self) -> None:
        expected = "ValueError('foo') (expected) != None (raised)"
        with ShouldAssert(expected):
            with ShouldRaise(ValueError('foo')):
                pass

    def test_with_no_exception_when_expected_by_type(self) -> None:
        with ShouldAssert("<class 'ValueError'> (expected) != None (raised)"):
            with ShouldRaise(ValueError):
                pass

    def test_with_no_exception_when_neither_expected(self) -> None:
        with ShouldAssert("No exception raised!"):
            with ShouldRaise():
                pass

    def test_with_getting_raised_exception(self) -> None:
        e = ValueError('foo bar')
        # If you're not strictly type checking, then you don't need to specify a type.
        # Using type ignore here just to check this works:
        with ShouldRaise() as s:  # type: ignore[var-annotated]
            raise e
        assert e is s.raised

    def test_import_errors_1(self) -> None:
        with ShouldRaise(ModuleNotFoundError("No module named 'textfixtures'")):
            import textfixtures.foo.bar  # type: ignore[import-not-found]

    def test_import_errors_2(self) -> None:
        with ShouldRaise(ImportError('X')):
            raise ImportError('X')

    def test_custom_exception(self) -> None:

        class FileTypeError(Exception):
            def __init__(self, value: object) -> None:
                self.value = value

        with ShouldRaise(FileTypeError('X')):
            raise FileTypeError('X')

    def test_decorator_usage(self) -> None:

        @should_raise(ValueError('bad'))
        def to_test() -> None:
            raise ValueError('bad')

        to_test()

    def test_unless_false_okay(self) -> None:
        with ShouldRaise(unless=False):
            raise AttributeError()

    def test_unless_false_bad(self) -> None:
        with ShouldAssert("No exception raised!"):
            with ShouldRaise(unless=False):
                pass

    def test_unless_true_okay(self) -> None:
        # I don't know why anyone would do this, but it's what mypy recommends:
        s: ShouldRaise[NoException]
        # If you're strict typing, and this is intended rather than a test failre,
        # then you should probably do `with ShouldRaise[NoException](unless=True):` instead.
        with ShouldRaise(unless=True) as s:
            pass
        # This documents the value of .raised in the rare care where it isn't
        # the exception raised within the context manager:
        assert isinstance(s.raised, NoException)

    def test_unless_true_not_okay(self) -> None:
        expected_exception = AttributeError('foo')
        try:
            with ShouldRaise(unless=True):
                raise expected_exception
        except AttributeError as actual_exception:
            assert actual_exception is expected_exception
        else:  # pragma: no cover
            self.fail('Wrong exception raised')

    def test_unless_decorator_usage(self) -> None:

        @should_raise(unless=True)
        def to_test() -> None:
            pass

        to_test()

    def test_identical_reprs(self) -> None:
        class AnnoyingException(Exception):
            def __init__(self, **kw: object) -> None:
                self.other = kw.get('other')

        with ShouldAssert(
            "AnnoyingException not as expected:\n\n"
            'attributes same:\n'
            "['args']\n\n"
            "attributes differ:\n"
            "'other': 'bar' (expected) != 'baz' (raised)\n\n"
            "While comparing .other: 'bar' (expected) != 'baz' (raised)"
        ):
            with ShouldRaise(AnnoyingException(other='bar')):
                raise AnnoyingException(other='baz')

    def test_identical_reprs_but_args_different(self) -> None:

        class MessageError(Exception):
           def __init__(self, message: object, type: object = None) -> None:
               self.message = message
               self.type = type
           def __repr__(self) -> str:
               return 'MessageError({!r}, {!r})'.format(self.message, self.type)

        with ShouldAssert(
            "MessageError not as expected:\n\n"
            'attributes same:\n'
            "['message', 'type']\n\n"
            "attributes differ:\n"
            "'args': ('foo',) (expected) != ('foo', None) (raised)\n\n"
            "While comparing .args: sequence not as expected:\n\n"
            "same:\n"
            "('foo',)\n\n"
            "expected:\n"
            "()\n\n"
            "raised:\n"
            "(None,)"
        ):
            with ShouldRaise(MessageError('foo')):
                raise MessageError('foo', None)

    def test_exception_group_okay(self) -> None:
        with ShouldRaise(ExceptionGroup('foo', [Exception('bar')])):
            raise ExceptionGroup('foo', [Exception('bar')])

    def test_exception_group_different(self) -> None:
        with ShouldAssert(
                "exception group not as expected:\n\n"
                "While comparing msg: 'fob' (expected) != 'foo' (raised)\n\n"
                "While comparing excs: sequence not as expected:\n\n"
                "same:\n"
                "[]\n\n"
                "expected:\n"
                "[Exception('baz')]\n\n"
                "raised:\n"
                "[Exception('bar')]\n\n"
                "While comparing excs[0]: "
                "Exception('baz') (expected) != Exception('bar') (raised)"
        ):
            with ShouldRaise(ExceptionGroup('fob', [Exception('baz')])):
                raise ExceptionGroup('foo', [Exception('bar')])

    def test_check_notes_on_raised_exception(self) -> None:
        exception = ValueError('foo')
        exception.add_note('bar')
        with ShouldRaise(ValueError) as s:
            raise exception
        assert s.raised.__notes__ == ['bar']

    def test_typing_on_attributes(self) -> None:
        class MyException(Exception):
            def __init__(self, x: int) -> None:
                self.x = x

        with ShouldRaise(MyException(1)) as r:
            raise MyException(1)

        compare(r.raised.x, expected= 1)