Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Dockerfile中运行py.test_Python_Python 3.x_Pytest_Django Unittest - Fatal编程技术网

Python 在Dockerfile中运行py.test

Python 在Dockerfile中运行py.test,python,python-3.x,pytest,django-unittest,Python,Python 3.x,Pytest,Django Unittest,我有一个Dockerfile,其中包含一行RUN py.test-vv FROM bitnami/python:3.6-prod #MORE DIRECTIVES RUN py.test -vv COPY . /files WORKDIR /files EXPOSE 8080 当我运行docker compose build时,我遇到了这个错误 Step 16/21 : RUN py.test -vv ---> Running in 5b3f55f10025 =============

我有一个Dockerfile,其中包含一行
RUN py.test-vv

FROM bitnami/python:3.6-prod
#MORE DIRECTIVES
RUN py.test -vv
COPY . /files
WORKDIR /files
EXPOSE 8080
当我运行
docker compose build
时,我遇到了这个错误

Step 16/21 : RUN py.test -vv
 ---> Running in 5b3f55f10025
============================= test session starts ==============================
platform linux -- Python 3.6.9, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- /opt/bitnami/python/bin/python
cachedir: .pytest_cache
rootdir: /
plugins: ordering-0.6, cov-2.8.1, docker-compose-3.1.2, celery-4.3.0
collecting ... collected 0 items / 1 errors

==================================== ERRORS ====================================
________________________ ERROR collecting test session _________________________
opt/bitnami/python/lib/python3.6/site-packages/_pytest/config/__init__.py:456: in _importconftest
    return self._conftestpath2mod[key]
E   KeyError: PosixPath('/opt/bitnami/python/lib/python3.6/site-packages/matplotlib/tests/conftest.py')

During handling of the above exception, another exception occurred:
opt/bitnami/python/lib/python3.6/site-packages/_pytest/config/__init__.py:462: in _importconftest
    mod = conftestpath.pyimport()
opt/bitnami/python/lib/python3.6/site-packages/py/_path/local.py:701: in pyimport
    __import__(modname)
opt/bitnami/python/lib/python3.6/site-packages/matplotlib/tests/__init__.py:16: in <module>
    'The baseline image directory does not exist. '
E   OSError: The baseline image directory does not exist. This is most likely because the test data is not installed. You may need to install matplotlib from source to get the test data.

During handling of the above exception, another exception occurred:
opt/bitnami/python/lib/python3.6/site-packages/py/_path/common.py:377: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
opt/bitnami/python/lib/python3.6/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
opt/bitnami/python/lib/python3.6/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
opt/bitnami/python/lib/python3.6/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
opt/bitnami/python/lib/python3.6/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
opt/bitnami/python/lib/python3.6/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
opt/bitnami/python/lib/python3.6/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
opt/bitnami/python/lib/python3.6/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
opt/bitnami/python/lib/python3.6/site-packages/py/_path/common.py:418: in gen
    dirs = self.optsort([p for p in entries
opt/bitnami/python/lib/python3.6/site-packages/py/_path/common.py:419: in <listcomp>
    if p.check(dir=1) and (rec is None or rec(p))])
opt/bitnami/python/lib/python3.6/site-packages/_pytest/main.py:606: in _recurse
    ihook = self.gethookproxy(dirpath)
opt/bitnami/python/lib/python3.6/site-packages/_pytest/main.py:424: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
opt/bitnami/python/lib/python3.6/site-packages/_pytest/config/__init__.py:434: in _getconftestmodules
    mod = self._importconftest(conftestpath)
opt/bitnami/python/lib/python3.6/site-packages/_pytest/config/__init__.py:470: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   _pytest.config.ConftestImportFailure: (local('/opt/bitnami/python/lib/python3.6/site-packages/matplotlib/tests/conftest.py'), (<class 'OSError'>, OSError('The baseline image directory does not exist. This is most likely because the test data is not installed. You may need to install matplotlib from source to get the test data.',), <traceback object at 0x7f814caaef88>))
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
============================== 1 error in 11.83s ===============================
ERROR: Service 'testproject' failed to build: The command '/bin/sh -c py.test -vv' returned a non-zero code: 2
步骤16/21:运行py.test-vv
--->在5b3f55f10025中运行
===========================================测试会话开始==============================
平台linux——Python 3.6.9、pytest-5.2.1、py-1.8.0、pluggy-0.13.0——/opt/bitnami/Python/bin/Python
cachedir:.pytest\u缓存
rootdir:/
插件:ordering-0.6,cov-2.8.1,docker-compose-3.1.2,芹菜-4.3.0
收集。。。已收集0项/1个错误
===================================================错误====================================
________________________收集测试会话时出错_________________________
opt/bitnami/python/lib/python3.6/site-packages/\u-pytest/config/\u\u-init\u.py:456:in\u-importconftest
返回self.\u conftestpath2mod[键]
E KeyError:PosixPath('/opt/bitnami/python/lib/python3.6/site packages/matplotlib/tests/conftest.py')
在处理上述异常期间,发生了另一个异常:
opt/bitnami/python/lib/python3.6/site-packages/_-pytest/config/__-init__;.py:462:in\u-importconftest
mod=conftestpath.pyimport()
opt/bitnami/python/lib/python3.6/site-packages/py/_-path/local.py:701:in-pyimport
__导入(modname)
opt/bitnami/python/lib/python3.6/site-packages/matplotlib/tests/__-init__;.py:16:in
'基线映像目录不存在。'
E OSError:基线映像目录不存在。这很可能是因为未安装测试数据。您可能需要从源代码处安装matplotlib以获取测试数据。
在处理上述异常期间,发生了另一个异常:
opt/bitnami/python/lib/python3.6/site packages/py/_path/common.py:377:访问中
对于访客中的x(fil、rec、ignore、bf、sort)。gen(self):
opt/bitnami/python/lib/python3.6/site-packages/py/_-path/common.py:429:in-gen
对于self.gen(subdir)中的p:
opt/bitnami/python/lib/python3.6/site-packages/py/_-path/common.py:429:in-gen
对于self.gen(subdir)中的p:
opt/bitnami/python/lib/python3.6/site-packages/py/_-path/common.py:429:in-gen
对于self.gen(subdir)中的p:
opt/bitnami/python/lib/python3.6/site-packages/py/_-path/common.py:429:in-gen
对于self.gen(subdir)中的p:
opt/bitnami/python/lib/python3.6/site-packages/py/_-path/common.py:429:in-gen
对于self.gen(subdir)中的p:
opt/bitnami/python/lib/python3.6/site-packages/py/_-path/common.py:429:in-gen
对于self.gen(subdir)中的p:
opt/bitnami/python/lib/python3.6/site-packages/py/_-path/common.py:429:in-gen
对于self.gen(subdir)中的p:
opt/bitnami/python/lib/python3.6/site-packages/py/_-path/common.py:418:in-gen
dirs=self.optsort([p代表条目中的p
opt/bitnami/python/lib/python3.6/site-packages/py/_-path/common.py:419:in
如果p.check(dir=1)和(rec为None或rec(p))])
opt/bitnami/python/lib/python3.6/site-packages/_-pytest/main.py:606:in\u-recurse
ihook=self.gethookproxy(dirpath)
opt/bitnami/python/lib/python3.6/site-packages/_-pytest/main.py:424:in-gethookproxy
my_conftestmodules=pm.\u getconftestmodules(fspath)
opt/bitnami/python/lib/python3.6/site-packages/\u-pytest/config/\u\u-init\u\u.py:434:in\u-getconftestmodules
mod=self.\u importconftest(conftestpath)
opt/bitnami/python/lib/python3.6/site-packages/_-pytest/config/__-init__;.py:470:in\u-importconftest
引发ConftestImportFailure(conftestpath,sys.exc_info())
E_pytest.config.ConftestImportFailure:(local('/opt/bitnami/python/lib/python3.6/site packages/matplotlib/tests/conftest.py'),(,OSError('基线映像目录不存在。这很可能是因为未安装测试数据。您可能需要从源代码安装matplotlib以获取测试数据',),)
!!!!!!!!!!!!!!!!!!! 中断:收集过程中出现1个错误!!!!!!!!!!!!!!!!!!!!
=================================================11.83秒内出现1个错误===============================
错误:无法生成服务“testproject”:命令“/bin/sh-c py.test-vv”返回非零代码:2
我曾尝试在Dockerfile中添加
pip install matplotlib
,但仍然收到相同的错误


我以前有一个NodeJS应用程序,它也被停靠在Dockerfile中,使用
mocha
对其进行了一些测试,并将
RUN mocha
放在Dockerfile中可以正常工作。我不确定Python中的问题是什么。

我觉得这里的问题是因为Python中没有预先安装pytest。因此,您必须在docker容器中添加pytest安装步骤。就我个人而言,我一直在使用一个单独的dockerfile for pytest来运行它,该文件用于安装入口点并将其设置为pytest

我已附上docker-compose.yaml、dockerfile和pytest.dockerfile供您参考。或者,您可以直接在.yaml文件/dockerfile本身中提到pytest安装步骤,因为您没有任何其他要添加的服务,并且可以避免额外的dockerfile。对于使用docker容器运行Selenium pytest测试自动化来说,这个设置运行得非常好。请尝试一下,让我们知道反馈

version: '3.7'
services:
  test:
    volumes:
      - .:/files 
    build: 
      context: .
      dockerfile: pytest.dockerfile
docker-compose.yaml

FROM python:3.7-alpine
MAINTAINER xyz

ADD . /files
WORKDIR /files

ENV PYTHONDONTWRITEBYTECODE=true
EXPOSE 4444
dockerfile。 在这里,公开端口的选项也是可选的

FROM python:3.7-alpine
MAINTAINER xyz

RUN pip install pytest

ENTRYPOINT [ "pytest" ]
pytest.dockerfile