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__), '../'))))
相关推荐
丿罗小黑1 天前
Pytest项目_day15(yaml)
pytest
丿罗小黑2 天前
Pytest项目_day16(yaml和parametrize结合)
pytest
代码小念2 天前
Pytest+selenium UI自动化测试实战实例(超详细)
selenium·ui·pytest
丿罗小黑3 天前
Pytest项目_day11(fixture、conftest)
pytest
测试开发技术7 天前
软件测试中,pytest 运行完成后,如何自动发送邮件?
开发语言·python·pytest·接口测试·面试题
丿罗小黑7 天前
Pytest项目_day08(setup、teardown前置后置操作)
pytest
测试19987 天前
Pytest中实现自动生成测试用例脚本代码
自动化测试·软件测试·python·测试工具·测试用例·pytest·接口测试
丿罗小黑8 天前
Pytest项目_day04(Python做接口请求)
pytest