You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have some tests where I need to patch importing of modules, to test e.g. if a module behaves properly when some optional dependency couldn't be imported.
I already have something to monkeypatch builtins.__import__ and importlib.import_module, in my testsuite, but I thought it might be a good addition to pytest as it's not trivial to get right, and there are differences between Python 2 and 3.
Proposed API:
New monkeypatch.import(name, obj, module=None) method
name: The name of the modified import
obj: The object which should get imported instead, or ImportError to raise an ImportError on import
module: If given (as a dotted string path), importlib.import_module and maybe others are patched as well for this module.
Caveats:
import is patched globally for the duration of the test, not only in the given module - the module parameter is only for importlib.import_module and similar things.
Making ImportError for obj special cased might be weird? But I can't think of a better API.
What do you think? Would this be something which is generally useful or should I keep it in my own testsuite?
Activity
RonnyPfannschmidt commentedon Aug 19, 2015
this one is tricky
RonnyPfannschmidt commentedon Jan 1, 2016
This should be a plugin
RonnyPfannschmidt commentedon Jun 23, 2017
@The-Compiler how about
Zac-HD commentedon Dec 7, 2018
Closing as "inactive and should probably be a plugin".