[TIP] Mock and function namespace

Ben Finney ben+python at benfinney.id.au
Thu Mar 15 16:53:58 PDT 2012

Mathieu Drapeau <matdrapeau at gmail.com> writes:

> This is exactly what I wanted to highlight, is how can I also mock
> functions that have been previously defined?

Mocking is done by re-binding names.

> I wanted to skim off the code but sometimes in libs, a function can be
> namespaced or not... and I would like to trap every possible uses of
> such function.

Every object accessible by name is in some namespace. I don't know what
you mean by “can be namespaced or not”.

If you want to trap every possible use of a function, you need to know
every possible name by which it will be accessed.

So, probably, you would be better served by mocking some small set of
known names by which the function will be accessed, and discouraging use
of the function by other names.

 \        “Our wines leave you nothing to hope for.” —restaurant menu, |
  `\                                                       Switzerland |
_o__)                                                                  |
Ben Finney

More information about the testing-in-python mailing list