test_warnings.py (1181B)
1 from pathlib import Path 2 3 import pytest 4 5 from pluggy import HookimplMarker 6 from pluggy import HookspecMarker 7 from pluggy import PluggyTeardownRaisedWarning 8 from pluggy import PluginManager 9 10 11 hookspec = HookspecMarker("example") 12 hookimpl = HookimplMarker("example") 13 14 15 def test_teardown_raised_warning(pm: PluginManager) -> None: 16 class Api: 17 @hookspec 18 def my_hook(self): 19 raise NotImplementedError() 20 21 pm.add_hookspecs(Api) 22 23 class Plugin1: 24 @hookimpl 25 def my_hook(self): 26 pass 27 28 class Plugin2: 29 @hookimpl(hookwrapper=True) 30 def my_hook(self): 31 yield 32 1 / 0 33 34 class Plugin3: 35 @hookimpl(hookwrapper=True) 36 def my_hook(self): 37 yield 38 39 pm.register(Plugin1(), "plugin1") 40 pm.register(Plugin2(), "plugin2") 41 pm.register(Plugin3(), "plugin3") 42 with pytest.warns( 43 PluggyTeardownRaisedWarning, 44 match=r"\bplugin2\b.*\bmy_hook\b.*\n.*ZeroDivisionError", 45 ) as wc: 46 with pytest.raises(ZeroDivisionError): 47 pm.hook.my_hook() 48 assert len(wc.list) == 1 49 assert Path(wc.list[0].filename).name == "test_warnings.py"