没有这样的文件或目录:在 Docker 中的 pytest 收集期间“/dev/fd/11”

No such file or directory: '/dev/fd/11' during pytest collection in Docker

我有一个带有 Python 和 NodeJS 的简单 Dockerfile。我安装 pytest,一个本地库和 运行 测试:

FROM nikolaik/python-nodejs:latest

ADD . .

RUN pip3 install --upgrade pip
RUN pip3  install -e .
RUN pip3 install pytest

CMD ["pytest"]

然而,pytest 收集失败:

============================= test session starts ==============================
platform linux -- Python 3.10.2, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /
collected 0 items / 1 error

==================================== ERRORS ====================================
________________________ ERROR collecting test session _________________________
usr/local/lib/python3.10/site-packages/_pytest/runner.py:311: in from_call
    result: Optional[TResult] = func()
usr/local/lib/python3.10/site-packages/_pytest/runner.py:341: in <lambda>
    call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
usr/local/lib/python3.10/site-packages/_pytest/main.py:690: in collect
    for direntry in visit(str(argpath), self._recurse):
usr/local/lib/python3.10/site-packages/_pytest/pathlib.py:606: in visit
    yield from visit(entry.path, recurse)
usr/local/lib/python3.10/site-packages/_pytest/pathlib.py:606: in visit
    yield from visit(entry.path, recurse)
usr/local/lib/python3.10/site-packages/_pytest/pathlib.py:606: in visit
    yield from visit(entry.path, recurse)
usr/local/lib/python3.10/site-packages/_pytest/pathlib.py:591: in visit
    for entry in os.scandir(path):
E   FileNotFoundError: [Errno 2] No such file or directory: '/dev/fd/11'
=========================== short test summary info ============================
ERROR  - FileNotFoundError: [Errno 2] No such file or directory: '/dev/fd/11'
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.25s ===============================

我在 pytest GitHub 上找到了解决方案:https://github.com/pytest-dev/pytest/issues/8960

your working directory is / so pytest is attempting to recurse through everything in the filesystem (probably not what you want!)

已将 WORKDIR /tests/ 添加到 Dockerfile,问题已解决。