Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mechanism for explicit marking of fixtures which should be run with asyncio #125

Conversation

pipermerriam
Copy link
Contributor

Opening this up to get feedback from the maintainers.

With this patch existing users of pytest-asyncio should not experience any change in behavior.

This exposes a new API pytest_asyncio.asyncio_fixture that is intended to be used like this:

import pytest_asyncio


@pytest_asyncio.asyncio_fixture
async def server():   # async yield fixture
    await server.start()
    yield
    await server.stop()

@pytest_asyncio.asyncio_fixture
async def client():  # normal async function
    await client.connect_to_server()
    return client

This PR also adds a pytest.ini configuration value: asyncio_mode which defaults to True. When set to False, only fixtures with the above mark (as well as a few other conditions) will be run by the asyncio event loop.

Some notes:

  • I did not see a test suite. This was tested manually against a local project. If you'd like me to write up some tests I don't mind, but it felt heavy handed to do that without the blessing of a maintainer.
  • Looking for feedback on whether this is a feature that will be accepted before I polish this and make it nice.

@pipermerriam

This comment has been minimized.

@asvetlov
Copy link
Contributor

I'm not a maintainer but have 5 cents to add.

I think we need not a bool flag but three-state:

  1. "on": automatically apply pytest.mark.asyncio to all discovered asynchronous fixtures and tests
  2. "off": do nothing with autoapplying
  3. "compat" (which is default and deprecated): the current behavior. Apply asyncio mark to async fixtures but don't apply to async tests.

Having default "compat" mode deprecated we notice people switch either to "on" or "off" in their pytest configuration to avoid deprecation warning displayed by pytest.
After some deprecation period we can switch default and even drop "compat" mode eventually.

@asvetlov
Copy link
Contributor

@pipermerriam do you want to implement on/off/compat mode?

@pipermerriam
Copy link
Contributor Author

@asvetlov yes, but it will be about 2 weeks before I pick this back up. Life and work things taking front seat for a bit and won't be able to focus on this for till then. (won't be offended if anyone takes what I started and moves forward with it)

@asvetlov
Copy link
Contributor

I'm personally is overwhelmed as well by my job and fixing CPython 3.8 beta.
I had a hope to finish 3.8 hacking this week but seems I need more time.
There is a low chance that I can pick up your job in a week or two.

@graingert
Copy link
Member

@pipermerriam I had a go at resolving the conflicts, but I'm not sure what needs doing in pytest_asyncio/plugin.py

Gentoo are currently having trouble when running the anyio test suite with pytest-asyncio installed

@codecov-commenter
Copy link

codecov-commenter commented Jan 7, 2022

Codecov Report

Merging #125 (7fb70ed) into master (d48569e) will decrease coverage by 0.58%.
The diff coverage is 94.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #125      +/-   ##
==========================================
- Coverage   96.17%   95.59%   -0.59%     
==========================================
  Files           2        2              
  Lines         157      227      +70     
==========================================
+ Hits          151      217      +66     
- Misses          6       10       +4     
Impacted Files Coverage Δ
pytest_asyncio/plugin.py 95.53% <94.52%> (-0.62%) ⬇️
pytest_asyncio/__init__.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d48569e...7fb70ed. Read the comment docs.

@graingert
Copy link
Member

@asvetlov thanks for taking this on!

README.rst Outdated Show resolved Hide resolved
@asvetlov
Copy link
Contributor

asvetlov commented Jan 7, 2022

The PR is done and tested.

@Tinche @seifertm I very much appreciate your review.

Please feel free to ask a question if elaboration is needed.

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
@Tinche Tinche self-requested a review January 8, 2022 15:37
Copy link
Member

@Tinche Tinche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left suggestions for a couple of typos, looks good overall. A really good change!

README.rst Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
@seifertm seifertm self-requested a review January 8, 2022 16:19
Co-authored-by: Tin Tvrtković <tinchester@gmail.com>
Copy link
Contributor

@seifertm seifertm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Tests shouldn't just break when pytest-asyncio is installed in addition to any other package. The new modes are also more consistent than the current behavior.

I like that we switched from @pytest_asyncio.asyncio_fixture to the shorter @pytest_asyncio.fixture. I also appreciate that asyncio_mode=strict is the new default.

I annotated a few typos and issues in the docs. Other than that: Great job!

pytest_asyncio/plugin.py Outdated Show resolved Hide resolved
pytest_asyncio/plugin.py Outdated Show resolved Hide resolved
tests/modes/test_legacy_mode.py Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
asvetlov and others added 2 commits January 8, 2022 19:27
Co-authored-by: Michael Seifert <m.seifert@digitalernachschub.de>
Co-authored-by: Michael Seifert <m.seifert@digitalernachschub.de>
@asvetlov asvetlov merged commit f86d900 into pytest-dev:master Jan 8, 2022
@asvetlov
Copy link
Contributor

asvetlov commented Jan 8, 2022

Thanks, guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Require explicit marks for async fixtures
6 participants