我在 Docker 中运行一个 Django 应用程序,一切正常,但是当我尝试运行测试时,它失败了,错误非常模糊。
运行 docker-compose run djangoapp coverage run -m pytest
结果:
Creating djangoapp_run ... done ================================================= test session starts ==================================================
platform linux -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /
collected 0 items / 1 error
======================================================== ERRORS ========================================================
____________________________________________ ERROR collecting test session _____________________________________________
usr/local/lib/python3.8/dist-packages/_pytest/runner.py:311: in from_call
result: Optional[TResult] = func()
usr/local/lib/python3.8/dist-packages/_pytest/runner.py:341: in <lambda>
call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
usr/local/lib/python3.8/dist-packages/_pytest/main.py:710: in collect
for x in self._collectfile(path):
usr/local/lib/python3.8/dist-packages/_pytest/main.py:546: in _collectfile
assert (
E AssertionError: local('/dev/console') is not a file (isdir=False, exists=True, islink=False)
=============================================== short test summary info ================================================
ERROR - AssertionError: local('/dev/console') is not a file (isdir=False, exists=True, islink=False)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=================================================== 1 error in 0.33s ===================================================
docker 组成:version: '3'
services:
djangoapp:
build: .
container_name: djangoapp
ports:
- '8000:80'
- '1433:1433'
volumes:
- ./djangoapp:/var/www/html/djangoapp
environment:
- PYTHONUNBUFFERED=0
最佳答案
pytest
递归遍历,docker 中的默认工作目录是 /
.将这些组合起来......设置直接正确工作!
...
environment:
- PYTHONUNBUFFERED=0
working_dir: /var/www/html/djangoapp
...
关于django - Pytest 不能与 Django 和 Docker 一起使用 - AssertionError : local ('/dev/console' ) is not a file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65394782/