Skip to content

Commit 62bf444

Browse files
committed
[docs] Extracted most Python code examples into separate files.
This allows for auto-formatting using the usual tools and enables running the code examples as tests to ensure they work. Tests that perform network operations or require additional test dependencies have been excluded. Signed-off-by: Michael Seifert <[email protected]>
1 parent b6b2c7f commit 62bf444

14 files changed

+202
-203
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import asyncio
2+
3+
import pytest_asyncio
4+
5+
6+
@pytest_asyncio.fixture
7+
async def async_gen_fixture():
8+
await asyncio.sleep(0.1)
9+
yield "a value"
10+
11+
12+
@pytest_asyncio.fixture(scope="module")
13+
async def async_fixture():
14+
return await asyncio.sleep(0.1)

docs/source/reference/decorators.rst renamed to docs/source/reference/decorators/index.rst

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,8 @@ Decorators
33
==========
44
Asynchronous fixtures are defined just like ordinary pytest fixtures, except they should be decorated with ``@pytest_asyncio.fixture``.
55

6-
.. code-block:: python3
7-
8-
import pytest_asyncio
9-
10-
11-
@pytest_asyncio.fixture
12-
async def async_gen_fixture():
13-
await asyncio.sleep(0.1)
14-
yield "a value"
15-
16-
17-
@pytest_asyncio.fixture(scope="module")
18-
async def async_fixture():
19-
return await asyncio.sleep(0.1)
6+
.. include:: fixture_strict_mode_example.py
7+
:code: python
208

219
All scopes are supported, but if you use a non-function scope you will need
2210
to redefine the ``event_loop`` fixture to have the same or broader scope.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import asyncio
2+
3+
4+
def test_event_loop_fixture(event_loop):
5+
event_loop.run_until_complete(asyncio.sleep(0))

docs/source/reference/fixtures.rst renamed to docs/source/reference/fixtures/index.rst

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ is available as the return value of this fixture or via `asyncio.get_running_loo
99
The event loop is closed when the fixture scope ends. The fixture scope defaults
1010
to ``function`` scope.
1111

12-
.. code-block:: python
13-
14-
def test_http_client(event_loop):
15-
url = "http://httpbin.org/get"
16-
resp = event_loop.run_until_complete(http_client(url))
17-
assert b"HTTP/1.1 200 OK" in resp
12+
.. include:: event_loop_example.py
13+
:code: python
1814

1915
Note that, when using the ``event_loop`` fixture, you need to interact with the event loop using methods like ``event_loop.run_until_complete``. If you want to *await* code inside your test function, you need to write a coroutine and use it as a test function. The `asyncio <#pytest-mark-asyncio>`__ marker
2016
is used to mark coroutines that should be treated as test functions.

docs/source/reference/index.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Reference
66
:hidden:
77

88
configuration
9-
fixtures
10-
markers
11-
decorators
9+
fixtures/index
10+
markers/index
11+
decorators/index
1212
changelog
1313

1414
This section of the documentation provides descriptions of the individual parts provided by pytest-asyncio.

docs/source/reference/markers.rst

Lines changed: 0 additions & 180 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
6+
@pytest.mark.asyncio_event_loop
7+
class TestClassScopedLoop:
8+
loop: asyncio.AbstractEventLoop
9+
10+
async def test_remember_loop(self):
11+
TestClassScopedLoop.loop = asyncio.get_running_loop()
12+
13+
async def test_this_runs_in_same_loop(self):
14+
assert asyncio.get_running_loop() is TestClassScopedLoop.loop
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
6+
@pytest.mark.asyncio_event_loop(
7+
policy=[
8+
asyncio.DefaultEventLoopPolicy(),
9+
asyncio.DefaultEventLoopPolicy(),
10+
]
11+
)
12+
class TestWithDifferentLoopPolicies:
13+
@pytest.mark.asyncio
14+
async def test_parametrized_loop(self):
15+
pass
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
6+
class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
7+
pass
8+
9+
10+
@pytest.mark.asyncio_event_loop(policy=CustomEventLoopPolicy())
11+
class TestUsesCustomEventLoopPolicy:
12+
@pytest.mark.asyncio
13+
async def test_uses_custom_event_loop_policy(self):
14+
assert isinstance(asyncio.get_event_loop_policy(), CustomEventLoopPolicy)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
6+
@pytest.mark.asyncio_event_loop
7+
class TestClassScopedLoop:
8+
loop: asyncio.AbstractEventLoop
9+
10+
@pytest.mark.asyncio
11+
async def test_remember_loop(self):
12+
TestClassScopedLoop.loop = asyncio.get_running_loop()
13+
14+
@pytest.mark.asyncio
15+
async def test_this_runs_in_same_loop(self):
16+
assert asyncio.get_running_loop() is TestClassScopedLoop.loop

0 commit comments

Comments
 (0)