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

--asyncio-mode=auto don't find static tests #265

Closed
PerchunPak opened this issue Jan 14, 2022 · 12 comments · Fixed by #295
Closed

--asyncio-mode=auto don't find static tests #265

PerchunPak opened this issue Jan 14, 2022 · 12 comments · Fixed by #295

Comments

@PerchunPak
Copy link

PerchunPak commented Jan 14, 2022

Full log

C:\Users\perch\Desktop\programming\PingerBot>pytest --no-cov --asyncio-mode=auto
================================================= test session starts =================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: C:\Users\perch\Desktop\programming\PingerBot
plugins: asyncio-0.17.1, cov-3.0.0
asyncio: mode=auto
collected 83 items

tests\test_cmd_add.py .......                                                                                    [  8%]
tests\test_cmd_alias.py s.......                                                                                 [ 18%]
tests\test_cmd_motd.py ......                                                                                    [ 25%]
tests\test_cmd_other.py ........                                                                                 [ 34%]
tests\test_cmd_ping.py .........                                                                                 [ 45%]
tests\test_cmd_stat.py ..............                                                                            [ 62%]
tests\test_database.py sssssssssssssssss                                                                         [ 83%]
tests\test_metods_for_commands.py sss.ss........                                                                 [100%]

================================================== warnings summary ===================================================
tests/test_cmd_alias.py: 1 warning
tests/test_database.py: 17 warnings
tests/test_metods_for_commands.py: 5 warnings
  C:\Users\perch\AppData\Local\Programs\Python\Python39\lib\site-packages\_pytest\python.py:172: PytestUnhandledCoroutineWarning: async def functions are not natively supported and have been skipped.
  You need to install a suitable plugin for your async framework, for example:
    - anyio
    - pytest-asyncio
    - pytest-tornasync
    - pytest-trio
    - pytest-twisted
    warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid)))

-- Docs: https://docs.pytest.org/en/stable/warnings.html
==================================== 60 passed, 23 skipped, 23 warnings in 18.39s =====================================

P.S. My computer is 64bit, I don't know why it is thinking that 32.

@asvetlov
Copy link
Contributor

Interesting. How can I reproduce it locally?

@PerchunPak
Copy link
Author

I don't really know, try this commands with my project.

git clone https://github.com/PerchunPak/PingerBot.git
cd PingerBot
git checkout rewrite
pip install -U pip wheel
(here better create virtual environment)
pip install -Ur requirements.txt -r tests_requirements.txt

Then rename or delete pytest.ini and tests/pytest.ini, after it set Postgres credentials to config.yml or make virtual environment variable with name PINGERBOT_POSTGRES
After all of it, you can finaly run tests.

pytest --no-cov --asyncio-mode=auto

P.S. I tried create a simple test project, but it works fine, so I give my prod project.

@asvetlov
Copy link
Contributor

File tests/test_metods_for_commands.py, that generates a warning in the first message, doesn't exist in your project.
Sorry, it doesn't look like an introducer :(

@PerchunPak
Copy link
Author

Here this file on github.
Check if you switch to branch rewrite

@PerchunPak
Copy link
Author

Also I forgot say, before run tests, you need delete @mark.asyncio everywhere.

@asvetlov
Copy link
Contributor

Sorry. Works fine for me.
Could you provide a reliable test case?

@PerchunPak
Copy link
Author

Here full logs of reproduce, check if you done all steps and have same result.

@asvetlov
Copy link
Contributor

I see something suspicious.
Make your async test methods non-static, please.

@PerchunPak
Copy link
Author

Yes, it is works now.

@PerchunPak PerchunPak changed the title --asyncio-mode=auto don't find my async tests --asyncio-mode=auto don't find static tests Jan 23, 2022
@asvetlov
Copy link
Contributor

Should we fix it?
I think the problem is very minor, I have never seen static tests before.

@seifertm
Copy link
Contributor

I've only encountered static tests with the unittest framework so far, never with pytest. I agree that we probably shouldn't address this use case and close the issue. Feel free to voice concerns, though :)

For posteriority:
I was wondering why pytest-asyncio didn't find the tests when they were decorated with @staticmethod. After all, pytest does have support for that kind of tests.

It turns out the obj argument in pytest_pycollect_makeitem is of type <class 'staticmethod'>. Consequently, inspect.iscoroutinefunction(obj) returns False and the test doesn't get marked.

This is weird since Python seems to recognize static coroutines correctly:

$ python
Python 3.9.9 (main, Jan 10 2022, 14:15:11) 
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class MyClass:
...     @staticmethod
...     async def test_a():
...         assert True
... 
>>> type(MyClass.test_a)
<class 'function'>

>>> import inspect
>>> inspect.iscoroutinefunction(MyClass.test_a)
True

I stopped digging here, but there is a possibility that this needs to be addressed in pytest itself. (If it needs to be addressed at all)

@asvetlov
Copy link
Contributor

The plugin uses asyncio.iscoroutinefunction(), maybe it behaves a little differently than inspect.iscoroutinefunction()

asvetlov added a commit that referenced this issue Feb 23, 2022
…292).  (#295)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
clrpackages pushed a commit to clearlinux-pkgs/pypi-pytest_asyncio that referenced this issue Mar 7, 2022
… to version 0.18.2

Andrew Svetlov (1):
      Update changelog

Michael Seifert (2):
      fix: Add positional arguments to the synchronous test function wrapper. (#303)
      docs: Prepare release 0.18.2. (#304)

dependabot[bot] (2):
      Bump actions/setup-python from 2 to 3 (#300)
      Bump actions/checkout from 2 to 3 (#301)

kriek (1):
      Fix asyncio auto mode not marking static methods (closes pytest-dev/pytest-asyncio#265, closes pytest-dev/pytest-asyncio#292).  (#295)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants