Snippets

Frederik Banke MySQL example unit test

Created by Frederik Banke last modified
import unittest.mock
from unittest import TestCase
from unittest.mock import MagicMock
from mysql.connector.cursor import MySQLCursorPrepared


class TestMySQL(TestCase):
    def setUp(self):
        self.connection = unittest.mock.create_autospec(mysql.connector.MySQLConnection)
        cursor = MagicMock(MySQLCursorPrepared)
        cursor.fetchall = MagicMock(return_value=[[2], [3]])
        self.connection.cursor = MagicMock(return_value=cursor)


    def test_that_sum_is_calculated_correctly(self):
        self.assertEqual(5, get_data_from_mysql(self.connection, "testkey"))


if __name__ == '__main__':
    unittest.main()

Comments (0)