单元测试Pyodbc数据库连接

时间:2018-10-29 17:37:58

标签: python pyodbc python-unittest

我编写了以下单元测试,以测试连接是否已成功建立。

import unittest
from databse_access_pyodbc import *
from pyodbc import OperationalError


class TestDatabseConnection(unittest.TestCase):

    def test_connection_db(self):
        try:
            db_connection = get_db_connection()
        except OperationalError as err:
            self.fail(
                "get_db_connection() raised pyodbc.OperationalError. " +
                "Connection to database failed. Detailed error message: " + err)
        self.assertIsNone(db_connection)


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

如果可以建立连接,则测试将显示:

Ran 1 test in 0.001s

OK

Process finished with exit code 0

如果无法建立连接,我将得到以下信息:

Traceback (most recent call last):
  File "xxx\PyCharm-P\ch-0\182.4505.26\helpers\pycharm\_jb_unittest_runner.py", line 35, in <module>
    main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
  File "xxx\Python36\lib\unittest\main.py", line 94, in __init__
    self.parseArgs(argv)
  File "xxx\Python36\lib\unittest\main.py", line 141, in parseArgs
    self.createTests()
  File "xxx\Python36\lib\unittest\main.py", line 148, in createTests
    self.module)
  File "xxx\Python36\lib\unittest\loader.py", line 219, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "xxx\Python36\lib\unittest\loader.py", line 219, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "xxx\Python36\lib\unittest\loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
  File "xxx\tests\test_database_access_pyodbc.py", line 2, in <module>
    from databse_access_pyodbc import *
  File "xxx\databse_access_pyodbc.py", line 40, in <module>
    get_db_connection()
  File "xxx\databse_access_pyodbc.py", line 36, in get_db_connection
    autocommit=True)
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes-Anbieter: Es konnte keine Verbindung zu SQL Server hergestellt werden [53].  (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 11 for SQL Server]Anmeldungstimeout abgelaufen (0); [08001] [Microsoft][ODBC Driver 11 for SQL Server]Netzwerkbezogener oder instanzspezifischer Fehler beim Herstellen einer Verbindung mit SQL Server. Der Server wurde nicht gefunden, oder auf ihn kann nicht zugegriffen werden. Überprüfen Sie, ob der Instanzname richtig ist und ob SQL Server Remoteverbindungen zulässt. Weitere Informationen erhalten Sie in der SQL Server-Onlinedokumentation. (53)')

Process finished with exit code 1
Empty test suite.

为什么测试案例没有捕获到异常OperationalError,为什么它说:Empty test suite

1 个答案:

答案 0 :(得分:3)

在测试可以运行之前,您正在连接到测试的PyODBC

您正在databse_access_pyodbc模块中这样做。追溯显示给您;我只在此处包含来自追溯的注释的有趣行:

  1. PyCharm使用其自己的测试运行程序,该运行程序在此处调用unittest.main()

    main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
    
  2. unittest.main()代码在此处开始加载测试模块

    File "xxx\Python36\lib\unittest\loader.py", line 153, in loadTestsFromName
      module = __import__(module_name)
    
  3. 这是您在问题中发布的测试模块,测试模块的第二行导入了databse_access_pyodbc模块

    File "xxx\tests\test_database_access_pyodbc.py", line 2, in <module>
      from databse_access_pyodbc import *
    
  4. 您没有发布databse_access_pyodbc的代码,但是在模块级代码的第40行中,它调用了get_db_connection()

    File "xxx\databse_access_pyodbc.py", line 40, in <module>
      get_db_connection()
    

然后该呼叫无法连接,并引发异常。由于尝试加载测试模块时会引发异常,因此该模块中的其余代码将永远不会执行,class TestDatabseConnection的定义也不会发生,因此找不到测试。 / p>

如果定义函数的模块也在模块的顶层调用了该函数,则无法独立测试get_db_connection()。在那里删除该函数调用。