Tools for replacing objects with stubs.

There is not very much here yet because Python allows you to do a lot of stubbing naturally. The Stub class provides a few shortcuts though.


a StringTypes

(<type 'str'>, <type 'unicode'>)


f mkinterface(cls, template=<class testtools.stubs.stub_template at 0x277ab0>) ...

make a class having the same interface as cls.

every method accepts any input but does nothing. returns a class object.


C NoValue(...) ...

C Stub(...) ...

Replace an object with a stub object

Let's face it, creating stub objects is dead simple in Python. For example, a simple way to create a stub of foo is :

>>> def foo():
...     return 'foo'
>>> def newfoo():
...     return 'bar'
>>> oldfoo = foo
>>> try:
...     foo = newfoo
...     foo()
... finally:
...     foo = oldfoo

With Stub , this is still possible, like so, and it doesn't save you much coding either:

>>> def foo():
...     return 'foo'
>>> def newfoo():
...     return 'bar'
>>> try:
...     foo = Stub(foo, replace=newfoo)
...     foo()
... finally:
...     foo = foo.restore()

However, you can do more complex things too, like mimic sequential calls:

>>> def next_ingredient():
...     for i in ('eggs', 'flour'):
...         yield i
>>> orig_id = id(next_ingredient)
>>> next_ingredient = Stub(next_ingredient)
>>> def yeast():
...     return 'yeast'
>>> def butter():
...     return 'butter'
>>> try:
...     next_ingredient.replace([yeast, butter])
...     assert next_ingredient() == 'yeast'
...     assert next_ingredient() == 'butter'
...     next_ingredient()
... finally:
...     next_ingredient = next_ingredient.restore()
Traceback (most recent call last):
>>> assert id(next_ingredient) == orig_id

To accomplish this without Stub you would have to proxy a generator, which would be just a bit more coding.

In closing ... you probably don't need stub for simple stubbing but it might come in handy. The only reason I added it to testtools is because I had some tests that needed urlopen() to send a sequence of responses back in a test.

This class contains 5 members.

See the source for more information.