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__), '../'))))
相关推荐
bjwuzh21 小时前
使用pytest实现参数化后,控制台输出的日志是乱码
pytest
2025年一定要上岸2 天前
pytest框架 - 第二集 allure报告
pytest
头疼的程序员3 天前
allure生成测试报告(搭配Pytest、allure-pytest)
测试工具·pytest
文人sec3 天前
接口自动化测试设计思路--设计实战
python·https·单元测试·自动化·pytest
测试开发Kevin5 天前
从投入产出、效率、上手难易度等角度综合对比 pytest 和 unittest 框架
python·pytest
测试开发Kevin7 天前
以pytest_addoption 为例,讲解pytest框架中钩子函数的应用
python·pytest
川石教育13 天前
Pytest中的fixture装饰器详解
python自动化测试·pytest·pytest自动化测试框架·pytest测试框架·pytest单元测试框架
春风又。13 天前
接口自动化——参数化
python·测试工具·自动化·pytest
XTY0016 天前
mac电脑pytest生成测试报告
pytest
程序员的世界你不懂16 天前
pytest-前后置及fixture运用
pytest