为什么 pytest-django 找不到 manage.py?
Why can't pytest-django find manage.py?
我有一个这样的项目结构:
setup.cfg
integration_tests/
|----tests.py
src/
|----manage.py
|----my_django_app/
还有一个 setup.cfg:
[tool:pytest]
addopts=--tb=short --strict -ra
DJANGO_SETTINGS_MODULE = my_django_app.settings
env =
APP_ENV=testing
USE_SQLITE_DB=True
但是当我导航到该目录结构的顶部并 运行 pytest
时,我得到以下信息:
"pytest-django could not find a Django project (no manage.py file could be found). You must explicitly add your Django project to the Python path to have it picked up."
如果我改为导航到 /src/
和 运行 pytest
,测试 运行 没有问题,但很明显,因为我下了一个级别 none集成测试 运行.
根据文档,它似乎应该从您 运行 pytest
的位置向下钻取以尝试找到 manage.py
:https://pytest-django.readthedocs.io/en/latest/managing_python_path.html#automatic-looking-for-of-django-projects
如果不是这种情况,那么有什么方法可以在 setup.cfg 中配置设置以将 src/
添加到 PYTHONPATH?文档中关于使用 pip 在可编辑模式下安装 src/
的建议在我们的环境中并不成立。
您可以使用 pytest-pythonpaths pytests 插件。
示例:
[pytest]
python_paths = your/path/apps your/path/libs
在您的具体情况下:
[tool:pytest]
addopts=--tb=short --strict -ra
DJANGO_SETTINGS_MODULE = my_django_app.settings
env =
APP_ENV=testing
USE_SQLITE_DB=True
python_paths = src
为什么找不到manage.py?
先看看pytest是如何测试发现的
Conventions for Python test discovery
pytest implements the following standard test discovery:
If no arguments are specified then collection starts from testpaths (if configured) or the current directory. Alternatively, command line arguments can be used in any combination of directories, file names or node ids.
Recurse into directories, unless they match norecursedirs.
In those directories, search for test_*.py or *_test.py files, imported by their test package name.
From those files, collect test items:
test prefixed test functions or methods outside of class
test prefixed test functions or methods inside Test prefixed test classes (without an init method)
因此,manage.py
不符合测试文件的条件,因此不会被自动发现,您需要在 python 路径中才能 运行 您的测试。
我有一个这样的项目结构:
setup.cfg
integration_tests/
|----tests.py
src/
|----manage.py
|----my_django_app/
还有一个 setup.cfg:
[tool:pytest]
addopts=--tb=short --strict -ra
DJANGO_SETTINGS_MODULE = my_django_app.settings
env =
APP_ENV=testing
USE_SQLITE_DB=True
但是当我导航到该目录结构的顶部并 运行 pytest
时,我得到以下信息:
"pytest-django could not find a Django project (no manage.py file could be found). You must explicitly add your Django project to the Python path to have it picked up."
如果我改为导航到 /src/
和 运行 pytest
,测试 运行 没有问题,但很明显,因为我下了一个级别 none集成测试 运行.
根据文档,它似乎应该从您 运行 pytest
的位置向下钻取以尝试找到 manage.py
:https://pytest-django.readthedocs.io/en/latest/managing_python_path.html#automatic-looking-for-of-django-projects
如果不是这种情况,那么有什么方法可以在 setup.cfg 中配置设置以将 src/
添加到 PYTHONPATH?文档中关于使用 pip 在可编辑模式下安装 src/
的建议在我们的环境中并不成立。
您可以使用 pytest-pythonpaths pytests 插件。
示例:
[pytest]
python_paths = your/path/apps your/path/libs
在您的具体情况下:
[tool:pytest]
addopts=--tb=short --strict -ra
DJANGO_SETTINGS_MODULE = my_django_app.settings
env =
APP_ENV=testing
USE_SQLITE_DB=True
python_paths = src
为什么找不到manage.py?
先看看pytest是如何测试发现的
Conventions for Python test discovery
pytest implements the following standard test discovery:
If no arguments are specified then collection starts from testpaths (if configured) or the current directory. Alternatively, command line arguments can be used in any combination of directories, file names or node ids.
Recurse into directories, unless they match norecursedirs. In those directories, search for test_*.py or *_test.py files, imported by their test package name.
From those files, collect test items: test prefixed test functions or methods outside of class test prefixed test functions or methods inside Test prefixed test classes (without an init method)
因此,manage.py
不符合测试文件的条件,因此不会被自动发现,您需要在 python 路径中才能 运行 您的测试。