|
2 | 2 | import platform
|
3 | 3 | import sys
|
4 | 4 | from contextlib import contextmanager
|
5 |
| -from typing import Callable, Any, Tuple, Generator |
| 5 | +from typing import Callable, Any, Tuple, Generator, Type |
6 | 6 | from unittest.mock import MagicMock
|
7 | 7 |
|
8 | 8 | import pytest
|
@@ -235,17 +235,32 @@ def bar(self, arg):
|
235 | 235 | assert spy.spy_return == 20
|
236 | 236 |
|
237 | 237 |
|
238 |
| -def test_instance_method_spy_exception(mocker: MockerFixture) -> None: |
| 238 | +# Ref: https://docs.python.org/3/library/exceptions.html#exception-hierarchy |
| 239 | +@pytest.mark.parametrize( |
| 240 | + 'exc_cls', |
| 241 | + ( |
| 242 | + BaseException, |
| 243 | + Exception, |
| 244 | + GeneratorExit, # BaseException |
| 245 | + KeyboardInterrupt, # BaseException |
| 246 | + RuntimeError, # regular Exception |
| 247 | + SystemExit, # BaseException |
| 248 | + ) |
| 249 | +) |
| 250 | +def test_instance_method_spy_exception( |
| 251 | + exc_cls: Type[BaseException], |
| 252 | + mocker: MockerFixture, |
| 253 | +) -> None: |
239 | 254 | class Foo:
|
240 | 255 | def bar(self, arg):
|
241 |
| - raise Exception("Error with {}".format(arg)) |
| 256 | + raise exc_cls("Error with {}".format(arg)) |
242 | 257 |
|
243 | 258 | foo = Foo()
|
244 | 259 | spy = mocker.spy(foo, "bar")
|
245 | 260 |
|
246 | 261 | expected_calls = []
|
247 | 262 | for i, v in enumerate([10, 20]):
|
248 |
| - with pytest.raises(Exception, match="Error with {}".format(v)) as exc_info: |
| 263 | + with pytest.raises(exc_cls, match="Error with {}".format(v)) as exc_info: |
249 | 264 | foo.bar(arg=v)
|
250 | 265 |
|
251 | 266 | expected_calls.append(mocker.call(arg=v))
|
|
0 commit comments