|
26 | 26 | Union,
|
27 | 27 | overload,
|
28 | 28 | )
|
29 |
| -from unittest import SkipTest |
30 | 29 |
|
31 | 30 | import pytest
|
32 |
| -from _pytest.outcomes import OutcomeException |
33 | 31 | from pytest import (
|
34 | 32 | Class,
|
35 | 33 | Collector,
|
@@ -608,25 +606,28 @@ def scoped_event_loop(
|
608 | 606 |
|
609 | 607 | # @pytest.fixture does not register the fixture anywhere, so pytest doesn't
|
610 | 608 | # know it exists. We work around this by attaching the fixture function to the
|
611 |
| - # collected Python class, where it will be picked up by pytest.Class.collect() |
| 609 | + # collected Python object, where it will be picked up by pytest.Class.collect() |
612 | 610 | # or pytest.Module.collect(), respectively
|
613 |
| - try: |
614 |
| - pyobject = collector.obj |
615 |
| - # If the collected module is a DoctestTextfile, collector.obj is None |
616 |
| - if pyobject is None: |
617 |
| - return |
618 |
| - pyobject.__pytest_asyncio_scoped_event_loop = scoped_event_loop |
619 |
| - except (OutcomeException, Collector.CollectError): |
| 611 | + if type(collector) is Module: |
620 | 612 | # Accessing Module.obj triggers a module import executing module-level
|
621 | 613 | # statements. A module-level pytest.skip statement raises the "Skipped"
|
622 | 614 | # OutcomeException or a Collector.CollectError, if the "allow_module_level"
|
623 | 615 | # kwargs is missing. These cases are handled correctly when they happen inside
|
624 | 616 | # Collector.collect(), but this hook runs before the actual collect call.
|
625 |
| - return |
626 |
| - except SkipTest: |
627 |
| - # Users may also have a unittest suite that they run with pytest. |
628 |
| - # Therefore, we need to handle SkipTest to avoid breaking test collection. |
629 |
| - return |
| 617 | + # Therefore, we monkey patch Module.collect to add the scoped fixture to the |
| 618 | + # module before it runs the actual collection. |
| 619 | + def _patched_collect(): |
| 620 | + collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop |
| 621 | + return collector.__original_collect() |
| 622 | + |
| 623 | + collector.__original_collect = collector.collect |
| 624 | + collector.collect = _patched_collect |
| 625 | + else: |
| 626 | + pyobject = collector.obj |
| 627 | + # If the collected module is a DoctestTextfile, collector.obj is None |
| 628 | + if pyobject is None: |
| 629 | + return |
| 630 | + pyobject.__pytest_asyncio_scoped_event_loop = scoped_event_loop |
630 | 631 | # When collector is a package, collector.obj is the package's __init__.py.
|
631 | 632 | # pytest doesn't seem to collect fixtures in __init__.py.
|
632 | 633 | # Using parsefactories to collect fixtures in __init__.py their baseid will end
|
|
0 commit comments