[英]Unit Test Pyodbc Database Connection
我編寫了以下單元測試,以測試連接是否已成功建立。
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
?
您正在連接到PyODBC測試之外 ,在測試前可以運行。
您正在databse_access_pyodbc
模塊中執行此databse_access_pyodbc
。 追溯顯示給您; 我僅在此處包含來自追溯的注釋的有趣行:
PyCharm使用自己的測試運行程序,在此處調用unittest.main()
:
main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
unittest.main()
代碼開始在此處加載測試模塊
File "xxx\\Python36\\lib\\unittest\\loader.py", line 153, in loadTestsFromName module = __import__(module_name)
這是您在問題中發布的測試模塊,測試模塊的第二行導入了databse_access_pyodbc
模塊
File "xxx\\tests\\test_database_access_pyodbc.py", line 2, in <module> from databse_access_pyodbc import *
您沒有發布databse_access_pyodbc
的代碼,但是在模塊級代碼的第40行,它調用get_db_connection()
:
File "xxx\\databse_access_pyodbc.py", line 40, in <module> get_db_connection()
然后,該呼叫無法連接,並引發異常。 由於嘗試加載測試模塊時會引發異常,因此該模塊中的其余代碼將永遠不會執行, class TestDatabseConnection
定義也不會發生,因此找不到測試。
如果定義函數的模塊也在模塊的頂層調用了該函數,則無法獨立測試get_db_connection()
。 在那里刪除該函數調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.