python中mock的使用
from mock import Mock, callclass Order(object):# instance properties_orderItem = "None"_orderAmount = 0_orderFilled = -1# Constructordef __init__(self, argItem, argAmount):print "Order:__init__"# set the order itemif (isinstance(argItem, str)):if (len(argItem) > 0):self._orderItem = argItem# set the order amountif (argAmount > 0):self._orderAmount = argAmount# Magic methodsdef __repr__(self):# assemble the dictionarylocOrder = {'item':self._orderItem, 'amount':self._orderAmount}return repr(locOrder)# Instance methods# attempt to fill the orderdef fill(self, argSrc):print "Order:fill_"try:# does the warehouse has the item in stock?if (argSrc is not None):if (argSrc.hasInventory(self._orderItem)):# get the itemlocCount = argSrc.getInventory(self._orderItem, self._orderAmount)# update the following propertyself._orderFilled = locCountelse:print "Inventory item not available"else:print "Warehouse not available"except TypeError:print "Invalid warehouse"# check if the order has been filleddef isFilled(self):print "Order:isFilled_"return (self._orderAmount == self._orderFilled)class Warehouse(object): # private properties_houseName = None_houseList = None# accessorsdef warehouseName(self):return (self._houseName)def inventory(self):return (self._houseList)# -- INVENTORY ACTIONS# set up the warehousedef setup(self, argName, argList):pass# check for an inventory itemdef hasInventory(self, argItem):pass# retrieve an inventory itemdef getInventory(self, argItem, argCount):pass# add an inventory itemdef addInventory(self, argItem, argCount):passclass Test_Order(object):# preparing to test# objective: creating an orderdef test_orderCheck(self):"""Test routine C"""# creating a test ordertestOrder = Order("mushrooms", 10)print repr(testOrder)fooSource = Mock(spec = Warehouse)fooSource.hasInventory.return_value = TruefooSource.getInventory.return_value = 0# perform the testassert fooSource != NonetestOrder.fill(fooSource)# perform the checksassert testOrder.isFilled() == Falseassert testOrder._orderFilled == 0fooSource.hasInventory.assert_called_once_with("mushrooms")print fooSource.mock_calls
参考文献:1.http://www.oschina.net/translate/unit-testing-with-the-python-mock-class
python中mock的使用相关推荐
- Python 中 Mock 到底该怎么玩?一篇文章告诉你(超全)
1. 前言 微服务架构下,由于各类服务开发进度的不一致,导致联调工作经常会存在不确定性,进而导致项目延期 在实际工作中,为了保证项目进度,我们经常需要针对部分未完成模块及不稳定模块采用 Mock 方式 ...
- python中mock的基本使用
什么是mock? mock在翻译过来有模拟的意思.这里要介绍的mock是辅助单元测试的一个模块.它允许您用模拟对象替换您的系统的部分,并对它们已使用的方式进行断言. 在Python2.x 中 mock ...
- Python——单元测试中mock原理和使用
摘要 mock主要是的为了提供开发程序员的做一个的单元测试而使用的.假设你开发一个项目,里面包含了一个登录模块,登录模块需要调用身份证验证模块中的认证函数,该认证函数会进行值的返回,然后系统根据这个返 ...
- python中装饰器的作用_Python装饰器详解,详细介绍它的应用场景
装饰器的应用场景附加功能 数据的清理或添加:函数参数类型验证 @require_ints 类似请求前拦截数据格式转换 将函数返回字典改为 JSON/YAML 类似响应后篡改为函数提供额外的数据 moc ...
- python实现简单的api接口-python中接口的实现实例
接口基础知识: 简单说下接口测试,现在常用的2种接口就是http api和rpc协议的接口,今天主要说:http api接口是走http协议通过路径来区分调用的方法,请求报文格式都是key-value ...
- 关于python中lambda函数的描述_关于Python中的lambda函数
lambda是Python编程语言中使用频率较高的一个关键字.那么,什么是lambda?它有哪些用法?网上的文章汗牛充栋,可是把这个讲透的文章却不多.这里,我们通过阅读各方资料,总结了关于Python ...
- python中常见的15中面试题
下面这些问题涉及了与Python相关的许多技能,问题的关注点主要是语言本身,不是某个特定的包或模块.每一个问题都可以扩充为一个教程,如果可能的话.某些问题甚至会涉及多个领域. 我之前还没有出过和这些题 ...
- utittest和pytest中mock的使用详细介绍
Mock是Python中一个用于支持单元测试的库,它的主要功能是使用mock对象替代掉指定的Python对象,以达到模拟对象的行为. python3.3 以前,mock是第三方库,需要安装之后才能使用 ...
- 没有时间进行测试? —有关在Python中对AWS Lambda进行单元测试的12条建议
AWS Lambda -无服务器- Python - DEVOPS (AWS Lambda - serverless - Python - DEVOPS) Much like software app ...
最新文章
- bartender一行打印两个二次开发_C++ 智能指针和二叉树:图解层序遍历和逐层打印二叉树...
- Linux中如何使用Htop监控工具?【网络安全】
- GDCM:gdcm::DefinedTerms的测试程序
- Java / JVM是如何构建的? 采用OpenJDK是您的答案!
- linux运行级别与服务
- openstreetmap_OpenStreetMap成立10周年,开源医疗设备等等
- 【kafka】Kafka管理与监控
- Sentinel降级_异常比例_分布式系统集群限流_线程数隔离_削峰填谷_流量控制_速率控制_服务熔断_服务降级---微服务升级_SpringCloud Alibaba工作笔记0040
- Java统计文件行数
- 建设银行房贷没扣成功会补扣吗?
- centos7 刷新dns
- python题目58:工厂流水线调度
- 流光快门Matlab,打开手机中的流光快门,教你拍出制霸朋友圈的特效照片
- oeasy教您玩转vim - 48 - # ed由来
- 云服务器保存文件,云服务器保存文件命令
- 《游戏设计艺术(第2版)》——学习笔记(13)第13章 游戏机制必须平衡
- 马尔科夫蒙特卡罗方法
- 一个方便的大文件分割web工具
- python 的 *args和 **kwargs 分别是什么意思
- 转发-【分享】思科无线控制器HA模式升级
热门文章
- 《游戏人工智能》学习笔记1——4 行为选择算法一览
- 图解BUCK开关电源——工作原理
- 技术分享 | 接口测试中,请求超时该怎么办?
- 计算机是如何做减法的?
- 记录导出el表格为Excel文件
- A档博士45万安家费!配偶校内事业编,进校即为“长江学者”,湖北某高校引才!...
- 网易mr time android,统计时间消耗是时间管理的基础,几款手机APP可以完美实现此功能...
- 设计变更时,如何直接在工程图中添加修订信息?
- C语言--面向对象编程之多态
- Raspberry Pi 4B(4GB版)上搭建Donkeycar开发环境