Python django测试运行程序在py2版本上看不到测试,但在py3上看到它们

Python django测试运行程序在py2版本上看不到测试,但在py3上看到它们,python,django,unit-testing,testing,Python,Django,Unit Testing,Testing,在py3上运行命令时: python manage.py test myproject.user_management --settings mysettings.py 我看到我所有的测试都很好: 进行了29次测试 但是在py2上使用相同的命令: 运行了0个测试 只有在我向命令添加tests文件夹时,py2上的命令才起作用,如: python manage.py test myproject.user_management.test --settings mysettings.py 我已经:

py3
上运行命令时:

python manage.py test myproject.user_management --settings mysettings.py
我看到我所有的测试都很好:

进行了29次测试

但是在
py2
上使用相同的命令:

运行了0个测试

只有在我向命令添加
tests
文件夹时,
py2
上的命令才起作用,如:

python manage.py test myproject.user_management.test --settings mysettings.py
我已经: 1.在所有文件夹和子文件夹中仔细检查my imports a
\uuuu init\uuuu.py
2.设置等

我的应用程序结构如下所示:

project_name
----user_management
--------__init__.py
--------tests
------------__init__.py
------------training
----------------__init__.py
----------------training.py
python manage.py shell中
我可以轻松做到:

来自myproject.user\u management.tests导入培训ViewSetTest


此行为仅在我的项目中的少数应用程序中发生。

使用默认的Django测试运行程序时遵循以下结构:

user_management/
   __init__.py
   tests/
       __init__.py
       test_training.py
它现在应该能够在两个版本中找到您的测试

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


这也有快速测试收集的好处(不需要在MacOSX上的Docker中搜索许多目录和分析许多文件!)

测试的文件名是否以
test*
开头?
[tool:pytest]
addopts = --reuse-db --ds=settings.test
python_files =
    tests/integration/*.py
    tests/functional/*.py
test_paths =
    tests/