mock-import icon indicating copy to clipboard operation
mock-import copied to clipboard

Add mock for hiding existing import

Open jayvdb opened this issue 6 years ago • 4 comments

If an library has already been imported, or does exist and can be imported, it can be hard to test what happens when the import fails.

This is commonly needed for optional imports, where the code runs slightly differently if the import fails, or for platform specific imports, where the functionality can be tested for a different platform is able to be tested if the import fails. e.g. "what happens if pyreadline was not installed because the platform is not Windows."

jayvdb avatar Jul 20 '18 12:07 jayvdb

Hi, I didn't understand specifically what you mean. Maybe you can provide an example for what is missing for you? Thanks

posener avatar Jul 20 '18 13:07 posener

The opposite of what you are doing in this library ;-)

import argcomplete  # only to show it works when already imported
from mock import Mock, patch

def foo():
    import argcomplete
    argcomplete.autocomplete()

fake_importer = Mock(side_effect=ImportError('fake import missing'))
with patch('builtins.__import__', new=fake_importer):
     foo()

jayvdb avatar Jul 21 '18 02:07 jayvdb

Nice! Sounds like a cool feature to develop! You want to code it?

posener avatar Jul 22 '18 11:07 posener

Sure.

On Sun, Jul 22, 2018, 18:10 Eyal Posener [email protected] wrote:

Nice! Sounds like a cool feature to develop! You want to code it?

— You are receiving this because you authored the thread.

Reply to this email directly, view it on GitHub https://github.com/posener/mock-import/issues/17#issuecomment-406858021, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA69GYjW4aOEyafuHcgKBFo21NPwuZrks5uJF2QgaJpZM4VX7e- .

jayvdb avatar Jul 22 '18 13:07 jayvdb