[TIP] Patching __import__

Kumar McMillan kumar.mcmillan at gmail.com
Tue May 24 08:45:37 PDT 2011

On Tue, May 24, 2011 at 2:31 AM, Yoni Tsafir <yonix85 at gmail.com> wrote:
> Moving a discussion with Michael Foord to TIP so everyone can contribute:
> I suggested a feature to the mock framework in which you can mock non
> existing imports (useful when you are unit-testing platform dependent code).
>     @patch.dict('sys.modules', {'wmi': MagicMock()})
> On PyDev I can run the tests fine, but when I try running them with
> nosetests, nose doesn't recognize any tests. If I comment out the
> @patch.dict line, everything works fine.

Well, you are playing with fire here.  Nose already uses import hooks
to make test discovery possible within subdirectories that are not
__init__ modules.  Because of this, it has to alter / restore
sys.modules before some tests.

You could try:

nosetests --debug=nose.importer,nose.inspector,nose.selector

to see if that gives you some clues before / after commenting out the
@patch.dict line.  You could also try:

nosetests --no-path-adjustment

to turn off the import hooks.

> Tried to debug it a bit but no luck...
> Any ideas?
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python

More information about the testing-in-python mailing list