File tree 3 files changed +70
-31
lines changed
3 files changed +70
-31
lines changed Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ from textwrap import dedent
2
+
3
+ from pytest import Pytester
4
+
5
+
6
+ def test_event_loop_override (pytester : Pytester ):
7
+ pytester .makeconftest (
8
+ dedent (
9
+ '''\
10
+ import asyncio
11
+
12
+ import pytest
13
+
14
+
15
+ @pytest.fixture
16
+ def dependent_fixture(event_loop):
17
+ """A fixture dependent on the event_loop fixture, doing some cleanup."""
18
+ counter = 0
19
+
20
+ async def just_a_sleep():
21
+ """Just sleep a little while."""
22
+ nonlocal event_loop
23
+ await asyncio.sleep(0.1)
24
+ nonlocal counter
25
+ counter += 1
26
+
27
+ event_loop.run_until_complete(just_a_sleep())
28
+ yield
29
+ event_loop.run_until_complete(just_a_sleep())
30
+
31
+ assert counter == 2
32
+
33
+
34
+ class CustomSelectorLoop(asyncio.SelectorEventLoop):
35
+ """A subclass with no overrides, just to test for presence."""
36
+
37
+
38
+ @pytest.fixture
39
+ def event_loop():
40
+ """Create an instance of the default event loop for each test case."""
41
+ loop = CustomSelectorLoop()
42
+ yield loop
43
+ loop.close()
44
+ '''
45
+ )
46
+ )
47
+ pytester .makepyfile (
48
+ dedent (
49
+ '''\
50
+ """Unit tests for overriding the event loop."""
51
+ import asyncio
52
+
53
+ import pytest
54
+
55
+
56
+ @pytest.mark.asyncio
57
+ async def test_for_custom_loop():
58
+ """This test should be executed using the custom loop."""
59
+ await asyncio.sleep(0.01)
60
+ assert type(asyncio.get_event_loop()).__name__ == "CustomSelectorLoop"
61
+
62
+
63
+ @pytest.mark.asyncio
64
+ async def test_dependent_fixture(dependent_fixture):
65
+ await asyncio.sleep(0.1)
66
+ '''
67
+ )
68
+ )
69
+ result = pytester .runpytest_subprocess ("--asyncio-mode=strict" )
70
+ result .assert_outcomes (passed = 2 )
You can’t perform that action at this time.
0 commit comments