0

I have a problem when mocking in unittest.

#!/usr/bin/env python
import sys
sys.modules["foo.Bar"] = __import__("mock_bar")
import foo.Bar
print foo.Bar.__name__

I've got an ImportError exception in line 4. I don't know why since I have do some mock at line 3. There is a reference of how to mock import here. Here's the error message:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    import foo.Bar
ImportError: No module named foo.Bar

"import foo.Bar" should equal to "__import__('foo.Bar')", and before that I've hacked sys.modules to pretend module 'foo.Bar' has been already imported. Why python still try to import foo.Bar and complain?

Community
  • 1
  • 1
Meng
  • 747
  • 1
  • 6
  • 5

1 Answers1

0

Try doing import foo before your __import__ line: I think it could help.

user1735594
  • 437
  • 1
  • 6
  • 9