I am using the python mocking library and I am not sure why I get this result. Why only the second one got mocked and not the first one? What's the best way to do it?
import unittest
from unittest.mock import patch, Mock
import requests
from requests import Session
def my_func():
s = Session()
print('my_func', type(s))
def my_func2():
s = requests.Session()
print('my_func2', type(s))
class Test(unittest.TestCase):
@patch("requests.Session", new=Mock)
def test1(self, *args):
my_func()
@patch("requests.Session", new=Mock)
def test2(self, *args):
my_func2()
Output
my_func <class 'requests.sessions.Session'>
my_func2 <class 'unittest.mock.Mock'>