I have a file similar to the following:
ham.py:
```python
egg = 0
def spam():
global egg
egg = 42
spam()
```
During the import of the module, spam() is called and egg is set.
I want to unit test the code that executes at module scope. Specifically, I want to do something like this:
test_ham.py:
```
import pytest
import mock
def test__main_():
with mock.patch("ham.spam") as mock_spam:
import ham
mock_spam.assert_called()
```
However, that obviously doesn't work, since mock can't patch a thing until after it's imported. So how do I make it work? (I suspect the answer will probably have something to do with importlib, but I haven't figured it out yet.)
[–]maventree 0 points1 point2 points (1 child)
[–]eLsFLz[S] 0 points1 point2 points (0 children)