Skip to content

Commit 97ece55

Browse files
authored
Include changelog in user docs (#481)
* [docs] Split up the reference section into different documents, one for each topic. Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de> * [docs] Move CHANGELOG from project root to user documentation. Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de> * [build] Add links to documentation, changelog, and bug tracker to the project URLs. Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de> Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
1 parent 6adce31 commit 97ece55

File tree

10 files changed

+160
-149
lines changed

10 files changed

+160
-149
lines changed

MANIFEST.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
include CHANGELOG.rst
2-
31
recursive-exclude .github *
42
exclude .gitignore
53
exclude .pre-commit-config.yaml

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Welcome to pytest-asyncio!
77
:hidden:
88

99
concepts
10-
reference
10+
reference/index
1111
support
1212

1313
pytest-asyncio is a `pytest <https://docs.pytest.org/en/latest/contents.html>`_ plugin. It facilitates testing of code that uses the `asyncio <https://docs.python.org/3/library/asyncio.html>`_ library.

docs/source/reference.rst

Lines changed: 0 additions & 145 deletions
This file was deleted.
File renamed without changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=============
2+
Configuration
3+
=============
4+
5+
The pytest-asyncio mode can be set by the ``asyncio_mode`` configuration option in the `configuration file
6+
<https://docs.pytest.org/en/latest/reference/customize.html>`_:
7+
8+
.. code-block:: ini
9+
10+
# pytest.ini
11+
[pytest]
12+
asyncio_mode = auto
13+
14+
The value can also be set via the ``--asyncio-mode`` command-line option:
15+
16+
.. code-block:: bash
17+
18+
$ pytest tests --asyncio-mode=strict
19+
20+
21+
If the asyncio mode is set in both the pytest configuration file and the command-line option, the command-line option takes precedence. If no asyncio mode is specified, the mode defaults to `strict`.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
==========
2+
Decorators
3+
==========
4+
Asynchronous fixtures are defined just like ordinary pytest fixtures, except they should be decorated with ``@pytest_asyncio.fixture``.
5+
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)
20+
21+
All scopes are supported, but if you use a non-function scope you will need
22+
to redefine the ``event_loop`` fixture to have the same or broader scope.
23+
Async fixtures need the event loop, and so must have the same or narrower scope
24+
than the ``event_loop`` fixture.
25+
26+
*auto* mode automatically converts async fixtures declared with the
27+
standard ``@pytest.fixture`` decorator to *asyncio-driven* versions.

docs/source/reference/fixtures.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
========
2+
Fixtures
3+
========
4+
5+
``event_loop``
6+
==============
7+
Creates a new asyncio event loop based on the current event loop policy. The new loop
8+
is available as the return value of this fixture or via `asyncio.get_running_loop <https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_running_loop>`__.
9+
The event loop is closed when the fixture scope ends. The fixture scope defaults
10+
to ``function`` scope.
11+
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
18+
19+
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
20+
is used to mark coroutines that should be treated as test functions.
21+
22+
The ``event_loop`` fixture can be overridden in any of the standard pytest locations,
23+
e.g. directly in the test file, or in ``conftest.py``. This allows redefining the
24+
fixture scope, for example:
25+
26+
.. code-block:: python
27+
28+
@pytest.fixture(scope="session")
29+
def event_loop():
30+
policy = asyncio.get_event_loop_policy()
31+
loop = policy.new_event_loop()
32+
yield loop
33+
loop.close()
34+
35+
If you need to change the type of the event loop, prefer setting a custom event loop policy over redefining the ``event_loop`` fixture.
36+
37+
If the ``pytest.mark.asyncio`` decorator is applied to a test function, the ``event_loop``
38+
fixture will be requested automatically by the test function.
39+
40+
``unused_tcp_port``
41+
===================
42+
Finds and yields a single unused TCP port on the localhost interface. Useful for
43+
binding temporary test servers.
44+
45+
``unused_tcp_port_factory``
46+
===========================
47+
A callable which returns a different unused TCP port each invocation. Useful
48+
when several unused TCP ports are required in a test.
49+
50+
.. code-block:: python
51+
52+
def a_test(unused_tcp_port_factory):
53+
port1, port2 = unused_tcp_port_factory(), unused_tcp_port_factory()
54+
...
55+
56+
``unused_udp_port`` and ``unused_udp_port_factory``
57+
===================================================
58+
Works just like their TCP counterparts but returns unused UDP ports.

docs/source/reference/index.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=========
2+
Reference
3+
=========
4+
5+
.. toctree::
6+
:hidden:
7+
8+
configuration
9+
fixtures
10+
markers
11+
decorators
12+
changelog
13+
14+
This section of the documentation provides descriptions of the individual parts provided by pytest-asyncio.
15+
The reference section also provides a chronological list of changes for each release.

docs/source/reference/markers.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
=======
2+
Markers
3+
=======
4+
5+
``pytest.mark.asyncio``
6+
=======================
7+
A coroutine or async generator with this marker will be treated as a test function by pytest. The marked function will be executed as an
8+
asyncio task in the event loop provided by the ``event_loop`` fixture.
9+
10+
In order to make your test code a little more concise, the pytest |pytestmark|_
11+
feature can be used to mark entire modules or classes with this marker.
12+
Only test coroutines will be affected (by default, coroutines prefixed by
13+
``test_``), so, for example, fixtures are safe to define.
14+
15+
.. code-block:: python
16+
17+
import asyncio
18+
19+
import pytest
20+
21+
# All test coroutines will be treated as marked.
22+
pytestmark = pytest.mark.asyncio
23+
24+
25+
async def test_example(event_loop):
26+
"""No marker!"""
27+
await asyncio.sleep(0, loop=event_loop)
28+
29+
In *auto* mode, the ``pytest.mark.asyncio`` marker can be omitted, the marker is added
30+
automatically to *async* test functions.
31+
32+
33+
.. |pytestmark| replace:: ``pytestmark``
34+
.. _pytestmark: http://doc.pytest.org/en/latest/example/markers.html#marking-whole-classes-or-modules

setup.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ name = pytest-asyncio
33
version = attr: pytest_asyncio.__version__
44
url = https://github.com/pytest-dev/pytest-asyncio
55
project_urls =
6-
GitHub = https://github.com/pytest-dev/pytest-asyncio
6+
Documentation = https://pytest-asyncio.readthedocs.io
7+
Changelog = https://pytest-asyncio.readthedocs.io/en/latest/reference/changelog.html
8+
Source Code = https://github.com/pytest-dev/pytest-asyncio
9+
Bug Tracker = https://github.com/pytest-dev/pytest-asyncio/issues
710
description = Pytest support for asyncio
811
long_description = file: README.rst
912
long_description_content_type = text/x-rst

0 commit comments

Comments
 (0)