开发者

How to mock a free function in python?

I have a python program with a global function that is painful to test (it needs a large dataset to work properly). What is the best way to get around this while testing functions that call it?

I've found that the following works (but it make me feel dirty to use it).

module foo:

def PainLiesHere():
 开发者_运维知识库 return 4; #guaranteed to be random

module test

import foo

def BlissLiesHere():
  return 5

foo.PainLiesHere = BlissLiesHere

# test stuff


This is a perfectly fine way to do it. As long as you know that BlissLiesHere does not change the overall behavior of the unit you are testing...

EDIT:

This is what is being done, under all the nice extras they provide, by different kinds of mocking libraries, such as Mock, Mox, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜