fastapi-tdd-docker
fastapi-tdd-docker copied to clipboard
Pytest Setup Bug (seems duplicated test module filepath)
Hello Michael,
I met some problems when I executed docker-compose exec web python -m pytest to test the pytest app. (Session: Pytest Setup in part 1)
Error message:
$ winpty docker-compose exec web python -m pytest
======================================================== test session starts =========================================================
platform linux -- Python 3.10.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /usr/src/app
plugins: anyio-3.5.0
collected 79 items / 1 error / 78 selected
=============================================================== ERRORS ===============================================================
_______________________________ ERROR collecting env/Lib/site-packages/sniffio/_tests/test_sniffio.py ________________________________
import file mismatch:
imported module 'sniffio._tests.test_sniffio' has this __file__ attribute:
/usr/local/lib/python3.10/site-packages/sniffio/_tests/test_sniffio.py
which is not the same as the test file we want to collect:
/usr/src/app/env/Lib/site-packages/sniffio/_tests/test_sniffio.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
====================================================== short test summary info =======================================================
ERROR env/Lib/site-packages/sniffio/_tests/test_sniffio.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================== 1 error in 4.54s ==========================================================
Self-check
This error seems to have happened by the duplicate pytest module name; I checked that adding __init__.py in my tests dir should fix the bug (as the instruction did), but it still does not work. I can't figure out what other reasons might cause this. (I did also try to rebuild the container and then do a test command, still did not work out.)
So my current directory structure is: (as same as the course demo, except .pytest_cache which is automated popped out when the test error happens.
fastapi-tdd-docker
├─ .gitignore
├─ docker-compose.yml
├─ project
│ ├─ .dockerignore
│ ├─ .pytest_cache
│ │ ├─ .gitignore
│ │ ├─ CACHEDIR.TAG
│ │ ├─ README.md
│ │ └─ v
│ │ └─ cache
│ │ ├─ lastfailed
│ │ ├─ nodeids
│ │ └─ stepwise
│ ├─ app
│ │ ├─ config.py
│ │ ├─ db.py
│ │ ├─ main.py
│ │ ├─ models
│ │ │ ├─ tortoise.py
│ │ │ └─ __init__.py
│ │ └─ __init__.py
│ ├─ db
│ │ ├─ create.sql
│ │ └─ Dockerfile
│ ├─ Dockerfile
│ ├─ entrypoint.sh
│ ├─ migrations
│ │ └─ models
│ │ └─ 0_20220216230908_init.sql
│ ├─ pyproject.toml
│ ├─ requirements.txt
│ └─ tests
│ ├─ conftest.py
│ ├─ test_ping.py
│ └─ __init__.py
└─ README.md
Thanks in advance.
@phwangktw I was facing something similar, when I noticed that the number of items collected by pytest was way too high.
What worked for me was specifying the test directory in the command using docker-compose exec web python -m pytest tests/.
Hope that helps!