Jenkins运行pytest加载conftest遇ImportError,本地正常如何解决?
Jenkins执行pytest测试时出现ModuleNotFoundError: 'chromedriver_py'
错误信息
C:\Users\user\PycharmProjects\Project1>pytest -s -v test_cases\ ImportError while loading conftest 'C:\Users\user\PycharmProjects\Project1\test_cases\conftest.py'. test_cases\conftest.py:4: in <module> from chromedriver_py import binary_path E ModuleNotFoundError: No module named 'chromedriver_py'
注意:本地运行所有测试均正常。
项目结构
ProjectName configurations logs page_objects tc_1_page_obj.py tc_2_page_obj.py test_cases test_case1.py test_case2.py conftest.py utilities testdata
conftest.py内容
import pytest from selenium import webdriver from selenium.webdriver.chrome.service import Service from chromedriver_py import binary_path service_object = Service(executable_path=binary_path) options = webdriver.ChromeOptions() options.add_experimental_option("detach", True) @pytest.fixture(scope='module') def driver(): driver = webdriver.Chrome(service=service_object, options=options) driver.maximize_window() return driver
Jenkins流水线当前命令
pipenv shell pytest -s -v test_cases\
解决方法
- 确保项目依赖文件包含
chromedriver_py:
本地执行pipenv install chromedriver_py,将更新后的Pipfile和Pipfile.lock提交到代码仓库。 - 修改Jenkins流水线命令,先安装依赖再执行测试:
用pipenv install pipenv run pytest -s -v test_cases\pipenv run直接在虚拟环境中执行命令,避免pipenv shell的环境激活问题。 - 确认Jenkins任务的工作目录为项目根目录,保证命令执行路径正确。
内容的提问来源于stack exchange,提问作者kishore sai anil
相关产品推荐
相关产品推荐

