【问题标题】:django test runner do not see tests on py2 version, but see them on py3django 测试运行器在 py2 版本上看不到测试,但在 py3 上看到它们
【发布时间】:2020-06-04 10:36:21
【问题描述】:

当我在py3 上运行命令时:

python manage.py test myproject.user_management --settings mysettings.py

我发现我的所有测试都运行良好:

进行了 29 次测试

但py2上的命令相同:

运行 0 次测试

py2 上的命令仅在我将 tests 文件夹添加到命令时才有效,例如:

python manage.py test myproject.user_management.test --settings mysettings.py

我已经: 1.仔细检查我在所有文件夹和子文件夹中导入的__init__.py 2.设置等

我的应用结构如下:

project_name
----user_management
--------__init__.py
--------tests
------------__init__.py
------------training
----------------__init__.py
----------------training.py

在python manage.py shell我可以轻松做到:

from myproject.user_management.tests import TrainingViewSetTest

这种行为只发生在我项目中的少数应用程序中。

【问题讨论】:

  • 你的测试文件名是否以test*开头?

标签: python django unit-testing testing


【解决方案1】:

按照这个结构使用默认的 Django 测试运行器:

user_management/
   __init__.py
   tests/
       __init__.py
       test_training.py

它现在应该能够在两个版本中找到您的测试。

话虽如此,多年来我已经开始将所有测试放在根目录 tests/ 中,并使用以下 pytest 配置来查找这些测试 (setup.cfg):

[tool:pytest]
addopts = --reuse-db --ds=settings.test
python_files =
    tests/integration/*.py
    tests/functional/*.py
test_paths =
    tests/

这也有利于快速收集测试(在 MacOSX 上无需搜索很多目录和分析 Docker 中的很多文件!)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2019-05-27
    • 1970-01-01
    • 2020-02-27
    • 2018-12-06
    相关资源
    最近更新 更多