PyU*_*ned 6 python testing django django-channels
我正在开发运行Python3.6,Django 2.0和Channels 2.0.2的Web服务器.我想构建一些测试来确保我的websocket消费者在表现自己,不幸的是,每当我运行测试时,他们都被完全忽略.
我一直在关注测试的官方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'...
Run Code Online (Sandbox Code Playgroud)
我没有错误或其他警告.我仔细检查过,我已经安装pytest-asyncio和pytest-django,我敢肯定,该文件本身是由顶部放置一个print语句加载100%.我所有的其他测试都正常运行.任何帮助是极大的赞赏.
默认的 Django 测试运行器未实现与 asyncio 一起使用,并且不会正确自动发现用@pytest.mark.asyncio.
常见问题解答很好地概述了找不到测试的原因,但这就是我为了解决这个问题所做的事情。
安装库pytest、pytest-django和pytest-asyncio。
pipenv install pytest pytest-django pytest-asyncio
Run Code Online (Sandbox Code Playgroud)
pytest.ini在项目的根目录创建一个包含以下内容的文件,并替换projectname.settings为项目设置文件的正确名称。
[pytest]
DJANGO_SETTINGS_MODULE = projectname.settings
python_files = tests.py test_*.py *_tests.py
Run Code Online (Sandbox Code Playgroud)
然后,我可以使用https://channels.readthedocs.io/en/latest/topics/testing.htmlpytest中的描述来运行测试
如果这仍然不起作用,请仔细查看:https://pytest-django.readthedocs.io/en/latest/faq.html#faq-tests-not-being-picked-up