首页 > 解决方案 > pytest-cov ERROR 导入文件不匹配导致heroku存储库中的旧目录,无法删除目录

问题描述

在 testdriven.io 上使用 Django、Django REST 框架和 Docker 进行的测试驱动开发课程在 pytest-cov 部分之前进展顺利。我的错误是在开始之前我做了一个 Heroku 教程,目录被卡住了。

更新容器

$ docker-compose up -d --build

然后

$ docker-compose exec movies pytest -p no:warnings --cov=.
=========================================================================== test session starts ============================================================================
platform linux -- Python 3.8.2, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
django: settings: drf_project.settings (from ini)
rootdir: /usr/src/app, inifile: pytest.ini
plugins: django-3.9.0, cov-2.8.1
collected 11 items / 1 error / 10 selected                                                                                                                                 

================================================================================== ERRORS ==================================================================================
____________________________________________________________ ERROR collecting alpinehelloworld/webapp/tests.py _____________________________________________________________
import file mismatch:
imported module 'tests' has this __file__ attribute:
  /usr/src/app/tests
which is not the same as the test file we want to collect:
  /usr/src/app/alpinehelloworld/webapp/tests.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

----------- coverage: platform linux, python 3.8.2-final-0 -----------
Name                      Stmts   Miss Branch BrPart  Cover
-----------------------------------------------------------
drf_project/__init__.py       0      0      0      0   100%
drf_project/views.py          4      4      0      0     0%
movies/__init__.py            0      0      0      0   100%
movies/admin.py              11      0      4      0   100%
movies/models.py             12      1      0      0    92%
movies/serializers.py         7      0      0      0   100%
movies/views.py              27     27      2      0     0%
-----------------------------------------------------------
TOTAL                        61     32      6      0    49%

========================================================================= short test summary info ==========================================================================
ERROR alpinehelloworld/webapp/tests.py

然后我尝试删除 alpinehelloworld/

$ heroku run sh
Running sh on ⬢ infinite-meadow-89740... up, run.9167 (Free)
~ $ ls /usr/src/app/
alpinehelloworld  entrypoint.sh     movies            pytest.ini        staticfiles
drf_project       manage.py         movies.json       requirements.txt  tests
~ $ rm -rf /usr/src/app/alpinehelloworld/
~ $ ls /usr/src/app/
drf_project       entrypoint.sh     manage.py         movies            movies.json       pytest.ini        requirements.txt  staticfiles       tests
~ $ exit

然后

(base) mac01:app holmen1$ docker-compose exec movies pytest -p no:warnings --cov=.
=========================================================================== test session starts ============================================================================
platform linux -- Python 3.8.2, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
django: settings: drf_project.settings (from ini)
rootdir: /usr/src/app, inifile: pytest.ini
plugins: django-3.9.0, cov-2.8.1
collected 11 items / 1 error / 10 selected                                                                                                                                 

================================================================================== ERRORS ==================================================================================
____________________________________________________________ ERROR collecting alpinehelloworld/webapp/tests.py _____________________________________________________________

该目录仍然存在:(我如何删除/usr/src/app/alpinehelloworld/webapp/tests.py

标签: dockerherokupytestpytest-cov

解决方案


推荐阅读