我写了下面的单元测试来测试连接是否已成功建立。
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 "xxxPyCharm-Pch-0182.4505.26helperspycharm_jb_unittest_runner.py", line 35, in <module>
main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
File "xxxPython36libunittestmain.py", line 94, in __init__
self.parseArgs(argv)
File "xxxPython36libunittestmain.py", line 141, in parseArgs
self.createTests()
File "xxxPython36libunittestmain.py", line 148, in createTests
self.module)
File "xxxPython36libunittestloader.py", line 219, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "xxxPython36libunittestloader.py", line 219, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "xxxPython36libunittestloader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "xxxteststest_database_access_pyodbc.py", line 2, in <module>
from databse_access_pyodbc import *
File "xxxdatabse_access_pyodbc.py", line 40, in <module>
get_db_connection()
File "xxxdatabse_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
?
在测试可以运行之前,您将在测试外部连接到 PyODBC。
您将在databse_access_pyodbc
模块中执行此操作。回溯显示了这一点;我在这里只包含有趣的行和回溯中的注释:
-
PyCharm 使用自己的测试运行程序,它在此处调用
unittest.main()
:main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
-
unittest.main()
代码从此处开始加载测试模块File "xxxPython36libunittestloader.py", line 153, in loadTestsFromName module = __import__(module_name)
-
这是您在问题中发布的测试模块,测试模块的第 2 行导入您的
databse_access_pyodbc
模块File "xxxteststest_database_access_pyodbc.py", line 2, in <module> from databse_access_pyodbc import *
-
您没有发布
databse_access_pyodbc
的代码,但是在模块级代码的第 40 行上,它调用get_db_connection()
:File "xxxdatabse_access_pyodbc.py", line 40, in <module> get_db_connection()
然后,该调用无法连接并引发异常。由于在尝试加载测试模块时会引发异常,因此永远不会执行该模块中的其余代码,永远不会发生class TestDatabseConnection
定义,因此找不到任何测试。
如果定义函数的模块也在模块的顶层调用它,则无法独立测试get_db_connection()
。删除那里的函数调用。