django - Pytest 不能与 Django 和 Docker 一起使用 - AssertionError : local ('/dev/console' ) is not a file

标签 django docker docker-compose pytest

我在 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/

相关文章:

python - 如何让 Django 的通用 View 使用带有初始值的表单?

django - Django 中的 MVC 模式

docker - 如何将镜像创建的卷与 docker-compose 指定的命名卷链接起来?

docker - :启动容器过程导致 “exec:\” sudo\“:找不到可执行文件

mysql - panic : dial tcp 127. 0.0.1:3306: 连接: 连接被拒绝

python - 在没有任何 python 子包的情况下创建 pip 轮

django - Django-获取外键模型

python - 是否需要在Dockerfile中复制.tar.gz?

docker - 带有自定义文件 redis.conf 的 docker 容器中的 Redis 不起作用

docker - 由于Docker永久卷,在GitLab顶部构建的管道失败