之前一般做自動化測試用的是unitest框架,發現pytest同樣不錯,寫一個例子感受一下
test_sample.py
import cx_Oracle import config from send_message import send_message from insert_cainiao_oracle import insert_cainiao_oracle def test_cainiao_monitor(): """ 查詢數據庫信息對比數據是否滿足要求,如不滿足則發送短信通知,並寫入數據庫。 :return: """ sql = "select COUNT(*) from AVGINDEX t WHERE t.STATDATE = '2019-09-11'" conn = cx_Oracle.connect(config.name, config.password, config.host_port_sid) cursor = conn.cursor() cursor.execute(sql) data = cursor.fetchall() print(data) print(data[0][0]) conn.commit() cursor.close() # 關閉游標 conn.close() # 關閉數據庫連接 try: assert data[0][0] == 18 # 如斷言失敗,則會拋出AssertionError異常,將此異常捕獲,繼續執行下面的發送短信和插入數據庫操作, # 如果不捕獲則斷言失敗后不繼續執行下方代碼 except AssertionError as e: print('斷言失敗了') print(e) content = '查詢結果為%s條,不等於18條!' % data[0][0] # 發短信方法 send_message(content, [18*********]) # 信息入庫方法 insert_cainiao_oracle(1, content)
執行命令:
pytest test_sample.py --html=report.html
執行test_sample.py這個文件中的所有測試函數,並將執行結果輸出到report.html報告中