title: How to write unit test case for Python __del__ function
body: <p>I have the following code setup, and I have a test case to test the <code>Db.__init__</code> already, how do I test the <code>__del__</code>? Can you provide an example? </p>
  
  <pre><code>import psycopg2
  
  
  class Db(object):
      def __init__(self):
          import app
          conn_string = "host='{}' port='{}' dbname='{}' user='{}' password='{}'".format(app.app.config['DB_HOST'], \
                        app.app.config['DB_PORT'], app.app.config['DB_NAME'], app.app.config['DB_USER'], \
                        app.app.config['DB_PASSWORD'])
          self.conn = psycopg2.connect(conn_string)
  
      def __del__(self):
          self.conn.close()
  </code></pre>
  
  <p>Test Case</p>
  
  <pre><code>@ddt
  class TestDB(unittest.TestCase):
      @patch('psycopg2.connect')
      def test_db_constructor(self, mock_psycopg2_connect):
          mock_psycopg2_connect.returned_value = True
          db = Db()
  
          self.assertTrue(db.conn)
  </code></pre>