python:单元测试框架pytest的一个简单例子
生活随笔
收集整理的這篇文章主要介紹了
python:单元测试框架pytest的一个简单例子
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
之前一般做自動化測試用的是unitest框架,發(fā)現(xiàn)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():"""查詢數(shù)據(jù)庫信息對比數(shù)據(jù)是否滿足要求,如不滿足則發(fā)送短信通知,并寫入數(shù)據(jù)庫。: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() # 關(guān)閉游標(biāo)conn.close() # 關(guān)閉數(shù)據(jù)庫連接try:assert data[0][0] == 18# 如斷言失敗,則會拋出AssertionError異常,將此異常捕獲,繼續(xù)執(zhí)行下面的發(fā)送短信和插入數(shù)據(jù)庫操作,# 如果不捕獲則斷言失敗后不繼續(xù)執(zhí)行下方代碼except AssertionError as e:print('斷言失敗了')print(e)content = '查詢結(jié)果為%s條,不等于18條!' % data[0][0]# 發(fā)短信方法send_message(content, [18*********])# 信息入庫方法insert_cainiao_oracle(1, content)執(zhí)行命令:
pytest test_sample.py --html=report.html執(zhí)行test_sample.py這個文件中的所有測試函數(shù),并將執(zhí)行結(jié)果輸出到report.html報告中
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/gcgc/p/11512733.html
總結(jié)
以上是生活随笔為你收集整理的python:单元测试框架pytest的一个简单例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 经常梦到自己结婚什么预兆
- 下一篇: python:pytest中的setup