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

Ard*_*den 7 python pytest 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"]
Run Code Online (Sandbox Code Playgroud)

但是,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 ===============================
Run Code Online (Sandbox Code Playgroud)

Ard*_*den 9

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

你的工作目录是 / 所以 pytest 试图递归文件系统中的所有内容(可能不是你想要的!)

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