首页 > 解决方案 > pytest-django 找不到 Django 项目

问题描述

尝试用 django 配置 pytest,该项目已经有很多不是用 pytest 编写的测试(用 unittest 编写),但我试图让它们用 pytest 运行,这样我就可以编写 pytest 测试并让它与旧测试一起使用。

我知道 pytest-django 在 django 项目的根目录中检查 manage.py 文件,但是这个项目的 manage.py 文件不在根目录中,所以我想这就是为什么当我运行pytest但运行 pytest时抛出下面的错误并提供特定文件的作品。如何指定 manage.py 在哪里?因为我在文档中找不到这个

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.

标签: pythondjangopytestpytest-django

解决方案


您可以定义要运行的 python 命令的 python 路径:

PYTHONPATH=/your/path/to/your/django/project/ pytest

或在运行 pytest 命令之前导出您的 pythonpath:

export PYTHONPATH=/your/path/to/your/django/project/

pytest

推荐阅读