'python unittests mocking mysql multiple times causing errors

I am writing unittests for a program, the majority of functions are all boilerplate code to do some mysql queries with no real return types, to test these I have written tests to check for the query in the cursor:

    @mock.patch('mysql.connector.connect')
    def test_query1(self, mock_conn):
        test_query_data = 100
        import app

        a = app.query1(test_query_data)
        mock_cursor = mock_conn.return_value.cursor.return_value
        self.assertEqual(mock_cursor.execute.call_args[0], ('SELECT id FROM table WHERE data=%s limit 1;', (100,)))

this test on its own works fine but when I have others structured the exact same way the patching of the mysql connection breaks causing an exception in the assert statement

Traceback (most recent call last):
  File "c:\users\sirwill\appdata\local\programs\python\python38\lib\site-packages\mock\mock.py", line 1346, in patched
    return func(*newargs, **newkeywargs)
  File "C:\Users\sirwill\python_project\tests.py", line 69, in test_insert_event
    self.assertEqual(mock_cursor.execute.call_args[0], ('SELECT id FROM table WHERE data=%s limit 1;', (100,)))
TypeError: 'NoneType' object is not subscriptable

I have tried to delete the module and reimport with no change in the result



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source