[英]Running py.test inside Dockerfile
I have this Dockerfile that contains a line RUN py.test -vv
.我有这个 Dockerfile 包含一行
RUN py.test -vv
。
FROM bitnami/python:3.6-prod
#MORE DIRECTIVES
RUN py.test -vv
COPY . /files
WORKDIR /files
EXPOSE 8080
When I run docker-compose build
, I am getting this error.当我运行
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
I have tried adding pip install matplotlib
in the Dockerfile but I am still getting the same error.我尝试在 Dockerfile 中添加
pip install matplotlib
,但我仍然遇到同样的错误。
I have a NodeJS app before that was also Dockerized that has some tests on it using mocha
and putting RUN mocha
inside the Dockerfile works fine.我之前有一个 NodeJS 应用程序,它也是 Dockerized 的,它使用
mocha
对其进行了一些测试,并将RUN mocha
放入 Dockerfile 工作正常。 I'm not sure what's the issue here in Python.我不确定 Python 中的问题是什么。
I feel that the issue here is because pytest is not pre-installed in python.我觉得这里的问题是因为python中没有预装pytest。 Hence you have to add steps for pytest installation in the docker container.
因此,您必须在 docker 容器中添加 pytest 安装步骤。 Personally I have been running this by using a seperate dockerfile for pytest, which is using for installing and setting the ENTRYPOINT as pytest.
就我个人而言,我一直在为 pytest 使用单独的 dockerfile 来运行它,它用于安装和设置入口点为 Z5A748C120135ECA0627C0C3B4985E917。
I have attached the docker-compose.yaml, dockerfile and pytest.dockerfile for you reference. I have attached the docker-compose.yaml, dockerfile and pytest.dockerfile for you reference. Alternatively you can directly mention the pytest installation steps in the.yaml file/dockerfile itself as you are not having any other services to be added and to avoid the additional dockerfile.
或者,您可以在.yaml 文件/dockerfile 本身中直接提及 pytest 安装步骤,因为您没有要添加的任何其他服务并避免额外的 Z60F5AD192689E6AD807E86EB0E2B6E53 This set-up is runing perfectly for me for running Selenium-pytest test automation using docker containers.
这个设置非常适合我使用 docker 容器运行 Selenium-pytest 测试自动化。 Please try this and let us know the feedback.
请试试这个,让我们知道反馈。
version: '3.7'
services:
test:
volumes:
- .:/files
build:
context: .
dockerfile: pytest.dockerfile
docker-compose.yaml docker-compose.yaml
FROM python:3.7-alpine
MAINTAINER xyz
ADD . /files
WORKDIR /files
ENV PYTHONDONTWRITEBYTECODE=true
EXPOSE 4444
dockerfile. dockerfile。 The option to expose the port is again optional here.
公开端口的选项在这里再次是可选的。
FROM python:3.7-alpine
MAINTAINER xyz
RUN pip install pytest
ENTRYPOINT [ "pytest" ]
pytest.dockerfile pytest.dockerfile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.