title: How to write unittest cases for checking database connection with SQL server in Python?
body: <p>I have just started using unittest in Python for writing test cases, I have a function that makes the connection with SQL server. </p>
  
  <p><strong>sql_connection.py</strong></p>
  
  <pre><code>def getConnection():
      connection = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"
              "Server="+appConfig['sql_server']['server']+";"
              "Database="+appConfig['sql_server']['database']+";"
              "UID="+appConfig['sql_server']['uid']+";"
              "PWD="+appConfig['sql_server']['password']+";"
              "Trusted_Connection=no;",
              )
      return connection
  </code></pre>
  
  <p>I have tried below test case for checking database connect or not.</p>
  
  <p><strong>test_connection.py</strong></p>
  
  <pre><code>import pyodbc
  
  getConnection1=getConnection()
  
  class TestDatabseConnection(unittest.TestCase):
      def test_getConnection(self):
          try:
              db_connection = getConnection1.connection
          except pyodbc.Error as ex:
              sqlstate = ex.args[1]
              print(sqlstate)
              self.fail(
                  "getConnection() raised pyodbc.OperationalError. " +
                  "Connection to database failed. Detailed error message: " + sqlstate)
          self.assertIsNone(db_connection)
  </code></pre>
  
  <p>But still not able to get succeed.</p>
  
  <pre><code>======================================================================
  ERROR: test_getConnection (__main__.TestDatabseConnection)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "test_connection.py", line 23, in test_getConnection
      db_connection = getConnection1.connection
  AttributeError: 'pyodbc.Connection' object has no attribute 'connection'
  
  ----------------------------------------------------------------------
  Ran 1 test in 0.001s
  
  FAILED (errors=1)
  </code></pre>
  
  <p>Please help me out in this.</p>