尝试使用django配置pytest,该项目已经有很多测试不是用pytest编写的(用unittest编写),但我试图让它们用pytest运行,这样我就可以编写pytest测试并让它与旧测试一起工作.
我知道 pytest-django 检查 django 项目的根目录中的 manage.py 文件,但该项目的 manage.py 文件不在根目录中,所以我想这就是我运行时抛出以下错误的原因然而,运行 pytest 并提供特定文件是可行的。如何指定manage.py在哪里?因为我在documentation中找不到这个
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.
最佳答案
就我而言,我在 tests
文件夹中创建了一个名为 pytest.ini
的配置文件,其中包含以下内容:
[pytest]
pythonpath = ../bitpin
DJANGO_SETTINGS_MODULE = bitpin.settings
python_files = tests.py test_*.py *_tests.py
存储库树:
bitpin-repo
├── bitpin
│ ├── bitpin
│ │ ├── asgi.py
│ │ ├── __init__.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── __init__.py
│ └── manage.py
├── tests
│ ├── test_models.py
│ ├── pytest.ini
│ └── conftest.py
└── setup.py
<小时/>
[注意]
- 如果
tests/
位于项目旁边,请将bitpin
替换为。
:
[pytest]
pythonpath = .
关于python - pytest-django 找不到 Django 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50370312/