[TIP] mock, nosetests, and third-party base classes

Felix Yen fyen at narrativescience.com
Mon Jun 20 15:14:41 PDT 2011

hi, i'm new here and hope my question wasn't answered earlier. (i did look
at a bunch of archive threads ...) i derived a class from a third-party
class; it looks a bit like this:

class Connection(pymongo.Connection):
    def __init__(self, spam):
        # translate spam ...
        pymongo.Connection.__init__(self, host, port)

using mock 0.7.2, i wrote a unit test that validates the host and port
values passed to the base class constructor. however, this test fails when
nosetests runs it. without nose (1.0.0), i can get the test to succeed in
two ways:

1) assigning pymongo.Connection to a stub connection class, or
2) modifying sys.path and importing a fake pymongo package.

mock's documentation mentions that "nosetests does some manipulation of
sys.modules (along with sys.path manipulation)." so i think this
"manipulation" is preventing me from patching my connection.

has anyone seen this problem before? more generally/importantly, what's a
good way of mocking third-party base classes?? i'm using Python 2.7 and mock
usage is optional (though it's hard to imagine *not* using it), but nose
usage is mandatory.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20110620/da3b50e4/attachment.htm>

More information about the testing-in-python mailing list