python:单元测试框架pytest的一个简单例子
生活随笔
收集整理的這篇文章主要介紹了
python:单元测试框架pytest的一个简单例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
之前一般做自動化測試用的是unitest框架,發現pytest同樣不錯,寫一個例子感受一下
test_sample.py
import cx_Oracle import config from send_message import send_message from insert_cainiao_oracle import insert_cainiao_oracledef 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報告中
?
?
轉載于:https://www.cnblogs.com/gcgc/p/11512733.html
總結
以上是生活随笔為你收集整理的python:单元测试框架pytest的一个简单例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 经常梦到自己结婚什么预兆
- 下一篇: python:pytest中的setup