The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Suppress Python MySQL Connector DeprecationWarning and ResourceWarnings In Unittests

TODO: Pull subtitle into page object

I ran into warnings using mysql.connector. The looked liked this:

code_start_default_section code_end_default_section

With that in place, the warnings were suppressed and I'd get just the results of the unittests.

Here's the post that gave me the solution.

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Suppress Python MySQL Connector DeprecationWarning and ResourceWarnings In Unittests

-- p

I ran into warnings using <<link|mysql.connector|https://dev.mysql.com/doc/connector-python/en/>>. The looked liked this:

-- code/

/Users/user/workshop/venv/lib/python3.9/site-packages/mysql/connector/connection_cext.py:513: DeprecationWarning: PY_SSIZE_T_CLEAN will be required for '#' formats
  self._cmysql.query(query,
../usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py:353: ResourceWarning: unclosed <ssl.SSLSocket fd=8, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('###.###.###.###', 676767), raddr=('###.###.###.###', 443)>
  obj, end = self.scan_once(s, idx)
ResourceWarning: Enable tracemalloc to get the object allocation traceback

-- /code

-- p

And sometimes:

-- code/

sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=8, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('###.###.###.###', 676767), raddr=('###.###.###.###', 443)>

-- /code

-- p

The fix was to import the warnings module, create a `ignore_warnings` function and add it as a decorator. Like this:

-- code/
-- python

#!/usr/bin/env python3

import keyring
import mysql.connector
import unittest
import warnings 

from thing_to_test import ThingToTest 

def ignore_warnings(test_method):
    def test_run(self, *args, **kwargs):
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')
            test_method(self, *args, **kwargs)
    return test_run


class GenrePopulatorTest(unittest.TestCase):

    @ignore_warnings
    def setUp(self):
 
        global cnx
        cnx = mysql.connector.connect(
            database=keyring.get_password('mysql-database', 'local_username'),
            host=keyring.get_password('mysql-host', 'local_username'),
            user=keyring.get_password('mysql-username', 'local_username'),
            password=keyring.get_password('mysql-password', 'local_username')
        )

        
    def tearDown(self):
        cnx.close()

    @ignore_warnings
    def test_something_here(self):
        expected = 3
        query = ("SELECT COUNT(*) FROM table")
        cursor = cnx.cursor()
        cursor.execute(query)
        result = cursor.fetchone()[0]
        self.assertEqual(expected, result)

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

-- /code

-- p

With that in place, the warnings were suppressed and I'd get just the results of the unittests.

-- p

Here's <<link|the post|https://www.neuraldump.net/2017/06/how-to-suppress-python-unittest-warnings/>> that gave me the solution.


-- categories
-- Python
-- MySQL

-- metadata
-- date: 2021-09-01 18:42:02
-- id: 20eops3y
-- status: published
-- type: post
-- SCRUBBED_NEO: false
-- site: aws