Last active
October 1, 2021 19:13
-
-
Save ryanermita/0201f5a2b04953f3995714893200fa78 to your computer and use it in GitHub Desktop.
unit test a function with pymysql query.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unittest import TestCase, mock | |
import simple | |
class TestSimple(TestCase): | |
@mock.patch('simple.pymysql', autospec=True) | |
def test_get-data(self, mock_pymysql): | |
mock_cursor = mock.MagicMock() | |
test_data = [{'password': 'secret', 'id': 1}] | |
mock_cursor.fetchall.return_value = test_data | |
mock_pymysql.connect.return_value.cursor.return_value.__enter__.return_value = mock_cursor | |
self.assertEqual(test_data, simple.get_user_data()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment