1. 程式人生 > >介面測試指令碼實踐記錄(六)

介面測試指令碼實踐記錄(六)

六 與資料庫對比

import pymssql

def compare_expected_vs_db():
       
        diff_list = []  # 儲存不一致的程式碼

        with pymssql.connect(server='192.168.1.1', user='test', password='123456',database='db') as myconnect:
            with  myconnect.cursor(as_dict=True) as cursor:
                cursor.execute(
"SELECT top 10 code,content FROM [db].[dbo].[table] where isvalid = 1 and IsDeleted =0") for row in cursor: code, actual = row['code'], row['content'] expected = result_of_3api(stockcode) # 資料來源拼接結果 if actual != expected: #
預期實際對比 print('程式碼:%s\n實際結果:%s\n預期結果:%s' % (code, actual , expected)) diff_list.append(code) else: print(code, '一致')     if diff_list:     print('不一致的列表:', diff_list)     else:     print('對比結果:資料全部一致
')