【pytest学习11】mock函数pytest-mock
# weateher_r.py class Mock_weather(): def weather(self): '''天气接口''' pass def weather_result(self): '''模拟天气接口''' result = self.weather() if result['result'] == '雪': print('下雪了!!!') elif result['result'] == '雨': print('下雨了!!!') elif result['result'] == '晴天': print('晴天!!!!') else: print('返回值错误!') return result['status']
# test_01.py import pytest from test_01.weather_r import Mock_weather def test_01(mocker): # 实例化 p = Mock_weather() moke_value = {'result': "雪", 'status': '下雪了!'} # 通过object的方式进行查找需要mock的对象 p.weather = mocker.patch.object(Mock_weather, "weather", return_value=moke_value) result =p.weather_result() assert result=='下雪了!' def test_02(mocker): # 实例化 product = Mock_weather() # Mock的返回值 mock_value = {'result': "雨", 'status': '下雨了!'} # 第一个参数必须是模拟mock对象的完整路径 product.weather = mocker.patch('test_01.weather_r.Mock_weather.weather',return_value=mock_value) result = product.weather_result() assert result=='下雨了!' if __name__ == '__main__': pytest.main(['-vs'])
声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/
如出现转载未声明 将追究法律责任~谢谢合作
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具