views:

48

answers:

1

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
+3  A: 

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.

scrible