我试图在Docker上运行pytest测试框架。但是在运行容器时遇到以下错误。
______________________________________________________________________________________ ERROR collecting test session _______________________________________________________________________________________
/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py:440: in _importconftest
return self._conftestpath2mod[conftestpath]
E KeyError: local('/myapp/Source/conftest.py')
During handling of the above exception, another exception occurred:
/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py:446: in _importconftest
mod = conftestpath.pyimport()
/usr/local/lib/python3.7/site-packages/py/_path/local.py:701: in pyimport
__import__(modname)
<frozen importlib._bootstrap>:983: in _find_and_load
???
<frozen importlib._bootstrap>:967: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:677: in _load_unlocked
???
/usr/local/lib/python3.7/site-packages/_pytest/assertion/rewrite.py:140: in exec_module
exec(co, module.__dict__)
Source/conftest.py:2: in <module>
from Drivers.chromedriver import driver
Drivers/chromedriver.py:5: in <module>
driver = webdriver.Chrome(chromedriver)
/usr/local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py:73: in __init__
self.service.start()
/usr/local/lib/python3.7/site-packages/selenium/webdriver/common/service.py:76: in start
stdin=PIPE)
/usr/local/lib/python3.7/subprocess.py:775: in __init__
restore_signals, start_new_session)
/usr/local/lib/python3.7/subprocess.py:1522: in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
E OSError: [Errno 8] Exec format error: '/usr/local/bin/chromedriver'
During handling of the above exception, another exception occurred:
/usr/local/lib/python3.7/site-packages/py/_path/common.py:377: in visit
for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
/usr/local/lib/python3.7/site-packages/py/_path/common.py:418: in gen
dirs = self.optsort([p for p in entries
/usr/local/lib/python3.7/site-packages/py/_path/common.py:419: in <listcomp>
if p.check(dir=1) and (rec is None or rec(p))])
/usr/local/lib/python3.7/site-packages/_pytest/main.py:606: in _recurse
ihook = self.gethookproxy(dirpath)
/usr/local/lib/python3.7/site-packages/_pytest/main.py:424: in gethookproxy
my_conftestmodules = pm._getconftestmodules(fspath)
/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py:420: in _getconftestmodules
mod = self._importconftest(conftestpath)
/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py:454: in _importconftest
raise ConftestImportFailure(conftestpath, sys.exc_info())
E _pytest.config.ConftestImportFailure: (local('/myapp/Source/conftest.py'), (<class 'OSError'>, OSError(8, 'Exec format error'), <traceback object at 0x7fec384f53c8>))
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================= 1 error in 0.37s =========================================
我的Dockerfile如下
FROM python:3.7.2
MAINTAINER arun
#RUN apt-get update
#RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
# Install Chrome for Selenium
RUN curl https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -o /chrome.deb
RUN dpkg -i /chrome.deb || apt-get install -yf
RUN rm /chrome.deb
# Install chromedriver for Selenium
RUN curl https://chromedriver.storage.googleapis.com/2.31/chromedriver_linux64.zip -o /usr/local/bin/chromedriver
RUN chmod +x /usr/local/bin/chromedriver
#install python dependencies
COPY requirements.txt requirements.txt
RUN pip install -r ./requirements.txt
ADD . /myapp
WORKDIR /myapp
CMD "pytest"
ENV PYTHONDONTWRITEBYTECODE=true
EXPOSE 8080
Docker-compose.yml文件如下
version: '3.1'
services:
test:
build: .
volumes:
- .:/myapp
stdin_open: true
tty: true
and requirements.txt如下
chromedriver==2.24.1
coverage==4.5.4
fixtures==3.0.0
html-testRunner==1.2
importlib-metadata==0.23
MarkupSafe==1.1.1
packaging==19.2
parse==1.11.1
parse-type==0.4.2
pbr==5.4.2
pi==0.1.2
pipenv==2018.11.26
pluggy==0.13.0
py==1.8.0
pyparsing==2.4.2
pyperclip==1.7.0
pytest==5.1.2
pytest-bdd==3.2.1
pytest-html==2.0.0
pytest-metadata==1.8.0
pytest-splinter==2.0.1
pytest-docker-fixtures
python-mimeparse==1.6.0
python-subunit==1.3.0
selenium==3.141.0
splinter==0.11.0
testtools==2.3.0
unittest2==1.1.0
urllib3==1.24.1
Flask==1.0.2
gunicorn==19.9.0
pytest-pep8
pytest-pythonpath
docker
我的main.py如下
from selenium import webdriver
chromedriver = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(chromedriver)
driver.maximize_window()
有人可以建议这里缺少什么吗?在Docker容器中运行Selenium-pytest UI自动化框架是否需要其他配置?
注意#为了创建图像,请先使用docker-compose up
,然后再创建容器docker-compose run test sh
答案 0 :(得分:0)
我通过使用docker-compose创建并联网多个容器解决了此问题。为此,我使用了docker-compose.yaml创建了容器,并分别使用dockerfile和pytest.dockerfile来管理本地文件和pytest
我已附上以下文件
version: '3.7'
services:
selenium:
image: selenium/hub
ports:
- 4444:4444
- 5901:5900
chrome:
image: selenium/node-chrome-debug
depends_on:
- selenium
environment:
- HUB_HOST=selenium
test:
volumes:
- .:/myapp
build:
context: .
dockerfile: pytest.dockerfile
docker-compose.yaml
FROM python:3.7-alpine
MAINTAINER arun
ADD . /myapp
WORKDIR /myapp
ENV PYTHONDONTWRITEBYTECODE=true
EXPOSE 4444
dockerfile
FROM python:3.7-alpine
MAINTAINER arun
RUN pip install pytest
RUN pip install pytest_bdd
ENTRYPOINT [ "pytest" ]
pytest.dockerfile