I am using pymysql client library to connect to the real database. I have a function in module, where I connect to the database using pymysql and do only database insert operations.How to unit test this function in python without hitting the real database?
import pymysql
def connectDB(self):
# Connect to the database
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
db='db')
try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, ('[email protected]', 'newpassword'))
connection.commit()
My python version is 2.7.
Database access falls outside the scope of unit testing, so you would not write unit tests that include database access.
You can use patch
, like this:
from unittest.mock import patch, MagicMock
@patch('mypackage.mymodule.pymysql')
def test(self, mock_sql):
self.assertIs(mypackage.mymodule.pymysql, mock_sql)
conn = Mock()
mock_sql.connect.return_value = conn
cursor = MagicMock()
mock_result = MagicMock()
cursor.__enter__.return_value = mock_result
cursor.__exit___ = MagicMock()
conn.cursor.return_value = cursor
connectDB()
mock_sql.connect.assert_called_with(host='localhost',
user='user',
password='passwd',
db='db')
mock_result.execute.assert_called_with("sql request", ("user", "pass"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With