结果断言
一、断言介绍
(1)介绍
断言是自动化最终的目的,一个用例没有断言,就失去了自动化测试的意义了
断言用到的是 assert关键字。预期的结果和实际结果做对比,符合预期就是pass,不符合就fail
(2)常用断言
pytest里面的断言就是python里assert的断言方法
- assert xx 判断xx为真
- assert not xx 判断xx不为真
- assert a in b 判断b包含a
- assert a == b 判断a等于b
- assert a != b 判断a不等于b
(3)案例
def is_true(num): if num>0: return True else: return False def test_01(): """判断是不是为真""" a = 2 b = 0 assert is_true(a) assert not is_true(b) def test_02(): """判断b包含a""" a = "hello" b = "hello world" assert a in b def test_03(): """判断是否相等""" a = b = "hello" c = "hello world" assert a == b assert a != c
备注 unittest框架实现结果断言,pytest可以直接兼容运行 pytest.main()
二、结果断言验证
1、结果断言封装
from utils.LogUtil import my_log import json #1、定义封装类 class AssertUtil: #2、初始化数据,日志 def __init__(self): self.log = my_log("AssertUtil") #3、code相等 def assert_code(self,code,expected_code): """ 验证返回状态码 :param code: :param expected_code: :return: """ try: assert int(code) == int(expected_code) return True except: self.log.error("code error,code is %s,expected_code is %s"%(code,expected_code)) raise #4、body相等 def assert_body(self,body,expected_body): """ 验证返回结果内容相等 :param body: :param expected_body: :return: """ try : assert body == expected_body return True except: self.log.error("body error,body is %s,expected_body is %s"%(body,expected_body)) raise #5、body包含 def assert_in_body(self,body,expected_body): """ 验证返回结果是否包含期望的结果 :param body: :param expected_body: :return: """ try: body = json.dumps(body) print(body) assert expected_body in body return True except: self.log.error("不包含或者body是错误,body is %s,expected_body is %s"%(body,expected_body)) raise
2、结果验证
AssertUtil().assert_code(res["status_code"], status_code) AssertUtil().assert_in_body(str(res["result"]),expect)
三、数据库结果验证
PyMySQL是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中是使用mysqldb。
(1)PyMySQL安装
pip3 install pymysql
(2)PyMySQL简单使用
# 导入pymysql模块 import pymysql # 连接database conn = pymysql.connect( host=“数据库地址”, user=“用户名”, password=“密码”, database=“数据库名”, charset=“utf8”) # 获取执行SQL语句的光标对象 cursor = conn.cursor() # 结果集默认以元组显示 # 获取执行SQL语句,结果作为字典返回 #cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 定义要执行的SQL语句 sql = "select username,password from tb_users" # 执行SQL语句 cursor.execute(sql) # 执行 cursor.fetchone() # 关闭光标对象 cursor.close() # 关闭数据库连接 conn.close()
(3)PyMySQL封装
from utils.LogUtil import my_log import pymysql #1、创建封装类 class Mysql: #2、初始化数据,连接数据库,光标对象 def __init__(self,host,user,password,database,charset="utf8",port=3306): self.log = my_log() self.conn = pymysql.connect( host=host, user=user, password=password, database=database,charset=charset, port=port ) self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor) #3、创建查询、执行方法 def fetchone(self,sql): """ 单个查询 :param sql: :return: """ self.cursor.execute(sql) return self.cursor.fetchone() def fetchall(self,sql): """ 多个查询 :param sql: :return: """ self.cursor.execute(sql) return self.cursor.fetchall() def exec(self,sql): """ 执行 :return: """ try: if self.conn and self.cursor: self.cursor.execute(sql) self.conn.commit() except Exception as ex: self.conn.rollback() self.log.error("Mysql 执行失败") self.log.error(ex) return False return True #4、关闭对象 def __del__(self): #关闭光标对象 if self.cursor is not None: self.cursor.close() #关闭连接对象 if self.conn is not None: self.cursor.close() if __name__ == "__main__": mysql = Mysql("211.103.136.242", "test", "test123456","meiduo", charset="utf8", port=7090) #res = mysql.fetchall("select username,password from tb_users") res = mysql.exec("update tb_users set first_name='python' where username ='python'") print(res) #1、创建db_conf.yml, db1,db2 #2、编写数据库基本信息#3、重构Conf.py #4、执行
(4)数据库配置文件
db_1: db_host: "211.103.136.242" db_user: "test" db_password: "test123456" db_name: "meiduo" db_charset: "utf8" db_port: "7090" db_2: db_host: "111211.103.136.242" db_user: "test" db_password: "test123456" db_name: "meiduo" db_charset: "utf8" db_port: "7090" db_3: db_host: "333211.103.136.242" db_user: "test" db_password: "test123456" db_name: "meiduo" db_charset: "utf8" db_port: "7090"
(5)数据库初始化
#1、定义init_db def init_db(db_alias): #2、初始数据化信息,通过配置 db_info = ConfigYaml().get_db_conf_info(db_alias) host = db_info["db_host"] user = db_info["db_user"] password = db_info["db_password"] db_name = db_info["db_name"] charset = db_info["db_charset"] port = int(db_info["db_port"]) #3、初始化mysql对象 conn = Mysql(host,user,password,db_name,charset,port) print(conn) return conn
(6)数据库结果验证
def assert_db(db_name,result,db_verify): assert_util = AssertUtil() #sql = init_db("db_1") sql = init_db(db_name) # 2、查询sql,excel定义好的 db_res = sql.fetchone(db_verify) #log.debug("数据库查询结果:{}".format(str(db_res))) # 3、数据库的结果与接口返回的结果验证# 获取数据库结果的key verify_list = list(dict(db_res).keys()) # 根据key获取数据库结果,接口结果 for line in verify_list: #res_line = res["body"][line] res_line = result[line] res_db_line = dict(db_res)[line] # 验证 assert_util.assert_body(res_line, res_db_line)