pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
12.09k stars 2.68k forks source link

How do I convince pytest to import the conftest.py at the project root? #6275

Closed jgonggrijp closed 4 years ago

jgonggrijp commented 4 years ago

I'm testing a Django project with pytest and I'm struggling with a conftest import issue. This is a simplified version of my directory layout:

backend/
├── conftest.py
├── items/
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── conftest.py
│   ├── migrations/
│   ├── models.py
│   ├── models_test.py
│   ├── urls.py
│   ├── views.py
│   └── views_test.py
├── manage.py*
├── readit/
│   ├── __init__.py
│   ├── index.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── requirements.in
└── requirements.txt

I tried running pytest and python -m pytest from within the backend directory as well as pytest backend and python -m pytest backend from its parent directory. In all cases, pytest does not recognize backend/conftest.py as something that should be imported. It will simply tell me that the fixtures in that file don't exist.

Package                  Version  
------------------------ ---------
apipkg                   1.5      
atomicwrites             1.3.0    
attrs                    19.1.0   
backports-abc            0.5      
beautifulsoup4           4.8.0    
certifi                  2019.6.16
chardet                  3.0.4    
Click                    7.0      
Django                   2.0.13   
django-cors-headers      3.0.2    
django-livereload-server 0.3.2    
django-proxy             1.2.1    
django-rest-auth         0.9.5    
djangorestframework      3.9.4    
execnet                  1.7.1    
idna                     2.8      
importlib-metadata       0.20     
isodate                  0.6.0    
more-itertools           7.2.0    
packaging                19.1     
pathlib2                 2.3.3    
pip                      19.2.3   
pip-tools                4.1.0    
pluggy                   0.12.0   
psycopg2                 2.8.3    
psycopg2-binary          2.8.3    
py                       1.8.0    
pyparsing                2.4.2    
pytest                   4.6.6    
pytest-django            3.5.1    
pytest-forked            1.0.2    
pytest-xdist             1.29.0   
pytz                     2019.2   
rdflib                   4.2.2    
rdflib-django3           0.2.4    
rdflib-jsonld            0.4.0    
requests                 2.21.0   
scandir                  1.9.0    
selenium                 3.141.0  
setuptools               41.0.1   
six                      1.12.0   
soupsieve                1.9.3    
sqlparse                 0.3.0    
tornado                  5.1.1    
urllib3                  1.24.3   
wcwidth                  0.1.7    
wheel                    0.33.4   
zipp                     0.6.0    

pytest 4.6.6, macOS 10.14.6

Sorry, not at this time.

symonk commented 4 years ago

@jgonggrijp can you paste the output of pytest --trace-config please?

jgonggrijp commented 4 years ago

I was going to do that and was censoring the filesystem paths in preparation. While scrolling through the output, I noticed that backend/conftest.py was actually mentioned. This prompted me to check whether the missing fixture was really in that file. It was. But then I realized I didn't decorate it with @pytest.fixture. Despite having imported pytest for this purpose. 🙄 Adding that line solved the problem, i.e., it confronted me with a failing test rather than a missing fixture.

Sorry for the noise. 🤐

RonnyPfannschmidt commented 4 years ago

@jgonggrijp thanks for the follow-up, such brain-blips are a pain to note on your own (been there, blinked at it, face-palmed after someone else pointed it out)