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

Treating the "coroutine was never awaited" RuntimeWarning as an error #184

Closed
puddly opened this issue Aug 23, 2020 · 5 comments
Closed

Comments

@puddly
Copy link

puddly commented Aug 23, 2020

Here's a simple test case:

# test_raise_warning.py

import pytest
import asyncio


@pytest.mark.asyncio
async def test_raise_warning():
    async def foo():
        pass

    foo()
    await asyncio.sleep(0.5)

And how I setup my test environment:

$ python3.8 -m venv venv
$ source venv/bin/activate
(venv) $ pip install pytest pytest-asyncio

If you try running pytest normally, it logs a warning:

$ pytest test_raise_warning.py
=========================== test session starts ===========================
platform darwin -- Python 3.8.3, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /private/tmp
plugins: asyncio-0.14.0
collected 1 item

test_raise.py .                                                     [100%]

============================ warnings summary =============================
test_raise_warning.py::test_raise_warning
  /private/tmp/test_raise_warning.py:10: RuntimeWarning: coroutine 'test_raise_warning.<locals>.foo' was never awaited
    foo()

-- Docs: https://docs.pytest.org/en/stable/warnings.html
====================== 1 passed, 1 warning in 0.54s =======================

However, passing -Werror silences it completely:

$ pytest -Werror test_raise_warning.py
=========================== test session starts ===========================
platform darwin -- Python 3.8.3, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /private/tmp
plugins: asyncio-0.14.0
collected 1 item

test_raise_warning.py .                                             [100%]

============================ 1 passed in 0.52s ============================

I'm trying to get this test case to fail. The with warnings.catch_warnings(): ... context manager doesn't work either because I think it needs to be around the call to loop.run_until_complete() call.

Am I misconfiguring something?

@graingert
Copy link
Member

I think you need to use pytest.ini's filterwarnings = "error" and set sys.unraisablehook before and run gc.collect after each test

@puddly
Copy link
Author

puddly commented Aug 24, 2020

@graingert Oh, you actually introduced sys.unraisablehook into CPython. Thank you!

I see now that this issue is related to pytest-dev/pytest#5299

I'll try to write a plugin for it or something, I've not worked with pytest internals before so it will take some time.

@graingert
Copy link
Member

@puddly my need for this feature is now more pressing, have you got something I can collaborate on?

@puddly
Copy link
Author

puddly commented Sep 10, 2020

Here's my quick in-tree implementation of it: puddly@9957292

I'll have to investigate how to wrap the required changes into a plugin but it works for my trivial unit test.

@puddly
Copy link
Author

puddly commented Sep 11, 2020

I've implemented this as a separate plugin https://github.com/puddly/pytest-unraisable/

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

No branches or pull requests

2 participants