[mock open]PyUnit执行单元测试时使用字符串模拟文件对象

def mock_open(data):
    from StringIO import StringIO
    stmp = StringIO(data)
    return stmp
_open = __builtins__.open
__builtins__.open = mock_open
# test case here
__builtins__.open = _open

 

posted @ 2017-03-20 15:51  鸪斑兔  阅读(313)  评论(0编辑  收藏  举报