在Docker文件中运行py.test

我有这样一个Docker文件,其中有一行内容是 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

我已经尝试添加 pip install matplotlib 的Dockerfile中,但我还是得到了同样的错误。

我之前有一个NodeJS应用也是Docker化的,上面有一些测试,使用了 mocha 并把 RUN mocha 内的Docker文件工作正常。我不知道在Python中是什么问题。

解决方案:

我觉得这里的问题是由于pytest没有预装在python中。因此,你必须在docker容器中添加安装pytest的步骤。就我个人而言,我是通过使用单独的docker文件为pytest进行安装,并将ENTRYPOINT设置为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

给TA打赏
共{{data.count}}人
人已打赏
未分类

如何解决变量不等于用户输入的问题

2022-9-8 14:51:31

未分类

AWS Glue - 开发端点 - "userData.json "错误。

2022-9-8 15:02:39

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索