简体   繁体   English

Django完全忽略Asynch测试(Django频道)

[英]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. 我正在开发运行Python3.6,Django 2.0和Channels 2.0.2的Web服务器。 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. 我想构建一些测试来确保我的websocket消费者在表现自己,不幸的是,每当我运行测试时,他们都被完全忽略。

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: 我一直在关注测试官方Channels文档 ,并且我复制了Channels 用于测试其通用消费者的代码,但是每当我运行测试时,测试运行器只是报告它没有运行测试:

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. 我已经检查过我已经安装了pytest-asynciopytest-django ,并且我100%确定通过在顶部放置一个print语句来加载文件本身。 All my other tests run normally. 我所有的其他测试都正常运行。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

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 . 默认的Django测试运行器未实现与asyncio一起使用,并且无法正确自动发现用@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 . 安装库pytestpytest-djangopytest-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.ini ,其中包含以下内容并交换projectname.settings以获取项目设置文件的正确名称。

[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 然后,我可以使用pytest运行测试,如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 如果这仍然不起作用,请给它一个外观: https//pytest-django.readthedocs.io/en/latest/faq.html#faq-tests-not-being-picked-up

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM