Snippets

Frederik Banke MySQL example unit test

You are viewing an old version of this snippet. View the current version.
Revised by Frederik Banke 01a987f
import unittest.mock
from unittest import TestCase
from unittest.mock import MagicMock
from mysql.connector.cursor import MySQLCursorPrepared


class TestMongoDBConsumer(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()
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.