pytest执行用例时从conftest.py抛出ModuleNotFoundError:No module named ‘XXX‘异常的解决办法

问题描述

本地运行正常,集成到Jenkins后使用执行 Windows 批处理命令运行测试用例时报错:

bash 复制代码
D:\PycharmProject\ZeppAndroid>pytest -vs  testcase\test_login.py --alluredir=report/allure_json --clean-alluredir 
ImportError while loading conftest 'D:\PycharmProject\ZeppAndroid\testcase\conftest.py'.
testcase\conftest.py:2: in <module>
    from po.prepare import *
E   ModuleNotFoundError: No module named 'po'

解决办法

经排查确认模块导入的方式是正确的,conftest.py却没有成功地引入这个模块,这里就涉及到执行路径的关系,testcase目录在项目根目录下,是pytest的覆盖范围,我们只需要改变conftest.py的执行路径就可以了,在conftest.py模块中添加以下代码

bash 复制代码
import sys, os

sys.path.append((os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))))
相关推荐
努力搬砖的咸鱼12 小时前
从零开始搭建 Pytest 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
FINE!(正在努力!)3 天前
PyTest框架学习
学习·pytest
程序员杰哥3 天前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
测试老哥3 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
水银嘻嘻4 天前
07 APP 自动化- appium+pytest+allure框架封装
python·appium·自动化·pytest
天才测试猿4 天前
接口自动化测试之pytest接口关联框架封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
not coder5 天前
Pytest Fixture 详解
数据库·pytest
not coder5 天前
pytest 常见问题解答 (FAQ)
开发语言·python·pytest
程序员的世界你不懂5 天前
(1)pytest简介和环境准备
pytest
not coder5 天前
Pytest Fixture 是什么?
数据库·oracle·pytest