[py-dev] how to test fallbacks for missing modules?

holger krekel holger at merlinux.eu
Sat Sep 26 17:02:59 CEST 2009


Hi Ronny,

> i wonder what would be a good way to test the handling of unimportable
> modules or fallbacks for missing modules.
> i don't have any initial idea on how to get started with that.

what i usually do is to provide the to-be-unimportable module 
but put e.g. "raise ImportError()" into it.  Using the 'tmpdir' 
and 'monkeypatch' funcargs makes it easy to do this, e.g.:

    def test_unimportable(tmpdir, monkeypatch):
        tmpdir.join("docutils.py").write("raise ImportError()")
        monkeypatch.setattr(sys, 'path', [str(tmpdir)] + sys.path)

        ..call-and-assert..


hth,
holger



More information about the Pytest-dev mailing list