[TIP] Struggling with pymock

Jean-Paul Calderone exarkun at divmod.com
Mon Sep 10 11:07:25 PDT 2007

On Mon, 10 Sep 2007 19:04:27 +0200, Tobias Roth <misc.lists at fsck.ch> wrote:
> [snip]
>2) How to run the tests? I am able to run it manually, but nosetest
>gives me an AttributeError: 'dict' object has no attribute 'open', no
>matter what I turn my code. This seems to be related to the stubbing of
>__builtins__.open() though, and might be an issue with nosetest.

The correct way to override a builtin is like this:

    import __builtin__
    __builtin__.open = customopen

__builtins__ is a CPython implementation detail which is sometimes bound
to the __builtin__ module object and sometimes bound to the __dict__ of
the __builtin__ module.

I'd suggest not doing this at all though, and instead parameterizing the
open function somehow, so that your tests can supply the mock directly
to the code being tested, intead of overriding it for the entire process.


More information about the testing-in-python mailing list