Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Ignoring Asynch Tests Completely (Django Channels)

I'm working on a web server running Python3.6, Django 2.0 and Channels 2.0.2. I'd like to build some tests to make sure my websocket consumers are behaving themselves, unfortunately, whenever I run the tests they are all ignored completely.

I've been following the official Channels documentation on testing, and I copied the code Channels uses to test its generic consumers as is but whenever I run the tests, the test runner simply reports that it ran no tests:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Destroying test database for alias 'default'...

I get no errors or other warnings. I've double checked that I've installed pytest-asyncio and pytest-django, and I'm 100% sure the file itself is being loaded by placing a print statement at the top. All my other tests run normally. Any help is greatly appreciated.

like image 299
PyUnchained Avatar asked Mar 21 '18 01:03

PyUnchained


1 Answers

The default Django test runner is not implemented to work with asyncio and will not properly auto-discover test functions decorated with @pytest.mark.asyncio.

The FAQ does a decent job of outlining the reasons tests wont be found but here's what I did in order to fix this issue on my end.

Install the libraries pytest, pytest-django, and pytest-asyncio.

pipenv install pytest pytest-django pytest-asyncio

Create a file pytest.ini at the root of your project containing the following and swap projectname.settings for the proper name of your projects settings file.

[pytest]
DJANGO_SETTINGS_MODULE = projectname.settings
python_files = tests.py test_*.py *_tests.py

I was then able to run the tests using pytest as described in https://channels.readthedocs.io/en/latest/topics/testing.html

If this still doesn't work, give this a look-through: https://pytest-django.readthedocs.io/en/latest/faq.html#faq-tests-not-being-picked-up

like image 134
castis Avatar answered Oct 11 '22 14:10

castis