Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest-django could not find a Django project

Trying to configure pytest with django, the project already has a lot of test not written with pytest (written with unittest) but I am trying to get them run with pytest so I can write pytest tests and get it work with old tests.

I know pytest-django checks for the manage.py file in the root dir of a django project but this project the manage.py file is not in the root dir so I guess that's why the error below is thrown when I run pytest however running pytest and supplying a particular file works. How do I specify where manage.py is? As I can't find this in the 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.
like image 718
Fenn-CS Avatar asked Oct 18 '25 07:10

Fenn-CS


1 Answers

In my case, I created a configure file named pytest.ini in the tests folder with the following content:

[pytest]
pythonpath = ../bitpin
DJANGO_SETTINGS_MODULE = bitpin.settings
python_files = tests.py test_*.py *_tests.py

Repository tree:

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

[NOTE]

  • If the tests/ is next to the project, replace ../bitpin with .:
[pytest]
pythonpath = .
like image 166
Benyamin Jafari Avatar answered Oct 19 '25 22:10

Benyamin Jafari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!