unittest help
André Malo
auch-ich-m at g-kein-spam.com
Thu Mar 24 05:51:40 EST 2005
* "Qiangning Hong" <hongqn at gmail.com> wrote:
> I want to apply TDD (test driven development) on my project. I am
> working on a class like this (in plan):
>
> # file: myclass.py
> import _extmod
>
> class MyClass(object):
> def __init__(self):
> self.handle = _extmod.open()
>
> def __del__(self):
> _extmod.close(self.handle)
>
> def some_stuff(self):
> _extmod.foobar(self.handle)
>
> ...
>
> As you see, it is an OO wrapper on _extmod, which is a pyrex extension
> module. The question is: how to unittest this class? As the _extmod
> is hardware-dependent, I want to use a mock class to replace it in unit
> test. But how can I let myclass in unittest to import the mock class?
You need to design for testability, meaning in this case, that your class could
to do something like this:
class MyClass(object):
def __init__(self):
self._loadExtmod()
self.handle = self._extmod.open()
def __del__(self):
self._extmod.close(self.handle)
def _loadExtmod(self):
import _extmod
self._extmod = extmod
def some_stuff(self):
self._extmod.foobar(self.handle)
Now just overload _loadExtmod and provide the mock class there.
HTH, nd
More information about the Python-list
mailing list