Trying to understand unittest.mock more, but not sure why its running the program twice. For simplicity, consider the code below in a file test.py
:
from unittest.mock import patch
class T():
def __init__(self):
self.setup()
def setup(self):
mock_testing = patch('test.testing').start()
mock_testing.return_value = "new testing"
def testing():
return "testing"
print("Hello")
t = T()
print("Setting up")
if testing() == "testing":
print("old style")
elif testing() == "new testing":
print("new style")
When I run the script with python test.py
, I get:
Hello
Hello
Setting up
new style
Setting up
old style
Why does it run the code twice? And even if it does run it twice, how come 'hello' is printed back to back, should it be printed like:
Hello
Setting up
new style
Hello
Setting up
old style
Also how can I make it so that it just runs the code once, with the mock value of 'new testing'?