【问题标题】:Django Ignoring Asynch Tests Completely (Django Channels)Django 完全忽略异步测试(Django 通道)
【发布时间】:2018-03-21 01:54:05
【问题描述】:

我正在开发一个运行 Python3.6、Django 2.0 和 Channels 2.0.2 的 Web 服务器。我想构建一些测试来确保我的 websocket 消费者行为正常,不幸的是,每当我运行测试时,它们都会被完全忽略。

我一直在关注official Channels documentation on testing,并按原样复制了代码 Channels uses to test its generic consumers,但每当我运行测试时,测试运行器只会报告它没有运行任何测试:

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'...

我没有收到任何错误或其他警告。我已经仔细检查了我是否安装了pytest-asynciopytest-django,并且我100% 确定文件本身正在通过在顶部放置一个打印语句来加载。我所有的其他测试都正常运行。非常感谢任何帮助。

【问题讨论】:

  • 我也有同样的问题,在文件之前抛出打印语句向我保证文件正在被加载,而只是将 assert False 放在函数体中没有任何作用,因为测试根本不会运行。跨度>
  • 尝试在他们的mailing list 上提问,或者在 Github 上创建问题。
  • 您需要使用pytest 运行测试,因为测试与标准unittest 库不兼容(函数而不是测试类,这就是unittest 没有发现任何测试的原因) .这也是channels 在他们的.travis.yml 中所做的。
  • 如果您想尝试运行 channels 测试,请安装 Django 和 channels 以及测试附加功能:pip install Django channels[tests] 并运行 pytest 命令。

标签: python django testing django-channels


【解决方案1】:

默认的 Django 测试运行器未实现与 asyncio 一起使用,并且不会正确地自动发现带有 @pytest.mark.asyncio 修饰的测试函数。

The FAQ does a decent job of outlining the reasons tests wont be found 但这是我为最终解决此问题所做的工作。

安装库pytestpytest-djangopytest-asyncio

pipenv install pytest pytest-django pytest-asyncio

在项目的根目录下创建一个文件 pytest.ini,其中包含以下内容,并将 projectname.settings 替换为项目设置文件的正确名称。

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

然后我可以使用pytest 运行测试,如https://channels.readthedocs.io/en/latest/topics/testing.html 中所述

如果这仍然不起作用,请浏览一下:https://pytest-django.readthedocs.io/en/latest/faq.html#faq-tests-not-being-picked-up

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-18
    • 2018-07-07
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2016-01-30
    相关资源
    最近更新 更多