之前一般做自动化测试用的是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报告中