接口自动化框架,封装数据库断言:
def assert_sql(self, host, port, user, password, database, assert_sql):
# 数据断言
dql = assert_sql["dql"]
# dql中参数化数据替换为提取的数据
dql = Template(dql).safe_substitute(**self.__argument)
# 执行sql后获取查询数据
data = Sql(host, port, user, password, database).execute(dql).fetchone()
# 获取入库的数据
expect_data = data[0]
# 提取接口返回的数据
expect_value = jsonpath.jsonpath(self.respond.json(), assert_sql['r_jsonpath'])[0]
# 断言入库的数据和接口响应的数据相同
assert expect_data == expect_value, f"数据断言失败,实际拿到的结果{expect_value},预期拿到的结果{expect_data}"
# print("数据断言成功")
脚本中用到数据库断言,出现报错TypeError: 'NoneType' object is not subscriptable,意思是
返回的数据为空,数据类型为NoneType,不能切片。python连接数据库,查询后得到的数据是个元组类型,当用下标取值报错TypeError: 'NoneType' object is not subscriptable时。
可能的原因是:
1、查询的数据一直为空,考虑为查询语句错误或者系统数据问题
2、查询的数据暂时为空,但是过几秒或几分钟后查询的数据会有数据,只是在查询的时候数据为空。实际就是在查询的时候,系统还没有完成更新数据库。这种情况,需要增加时间等待,等数据更新完成后再去查。