I am trying to write unit test case to test the following method by mocking the database. How to mock the database connection without actually connecting to the real database server.I tried with sample test case. I am not sure if that is right way to do it. Please correct me if I am wrong.
//MySQL.py
class MySQL():
retry_interval = 20000
def link(self, server,port):
try:
return pymysql.connect(server, username='name', passwd='pwd', db='demo', int(port))
sys.stdout.write('Connected to database at {}:{}\n'.format(server,port))
except:
sys.stderr.write('Retry database connection in {} ms...\n'.format(self.retry_interval))
sleep(self.retry_interval/1000.0)
continue
//test.py
from unittest.mock import Mock, MagicMock
from unittest.mock import patch
import MySQL
@patch('MySQL.pymysql')
def testLink(self, mysql_mock):
mock_cursor = Mock.MagicMock()
mysql_mock.connect.return_value.cursor.return_value.__enter__.return_value = mock_cursor