On Fri, 28 Apr 2017 at 02:19 Michael Foord <michael@voidspace.org.uk> wrote:


On 28/04/17 01:49, Terry Reedy wrote:
> On 4/27/2017 3:44 PM, Brett Cannon wrote:
>>
>>
>> On Wed, 26 Apr 2017 at 22:36 Terry Reedy <tjreedy@udel.edu
>> <mailto:tjreedy@udel.edu>> wrote:
>>
>>     On 4/26/2017 1:45 PM, Brett Cannon wrote:
>
>>      > E.g. I don't expect
>>      > test_importlib to be directly responsible for exercising all
>> code in
>>      > importlib, just that Python's entire test suite exercise
>> importlib as
>>      > much as possible as a whole.
>>
>>     The advantage for importlib in this respect is that import
>> statements
>>     cannot be mocked; only the objects imported, after importlib is
>>     finished.
>>
>>
>> Oh, you can mock import statements. :)
>
> Other than by pre-loading a mock module into sys.modules?
> If so, please give a hint, as this could be useful to me.

https://docs.python.org/3/library/unittest.mock-examples.html#mocking-imports-with-patch-dict

The other option is to stub out __import__() itself.