线性编程07--zip函数

zip函数

将多个可迭代对象中的每个元素,一一对应的组装/打包成一个元组
    a = [1,2,3]
    b = [4,5,6]
    c = zip(a,b)
    c的结果是
        (1, 4) (2, 5) (3, 6)

解压与遍历

    print(*c) 解包时,只能解包1次
    for  i, j  in c:
    for  i, j  in  zip(a,b):
        i代表的是()中左边的数据
        j代表的是()中右边的数据

使用zip函数打包数据

"""
使用zip函数打包数据
"""
a = [1,2,3]
b = [4,5,6]
c = zip(a,b)
# print(*c)# 解包,只能解一次
# for i,j in c:
#     # print(i)
#     # print(j)
#     print(i,'\t',j)
"""处理测试用例数据和预期结果"""
data = [
    {'username':'test01','password':'123456'},
    {'username':'test02','password':'123456'},
    {'username':'test03','password':'123'}
]
expects =[
    '登录成功','登录成功','登录失败'
]
case = zip(data,expects)#打包
for i,j in case:#解包
    print(i,j)

测试登录接口

"""
    测试登录接口,发送请求,检查响应结果正确性,如果测试失败,输出实际响应内容
    登陆接口 login
    返回文本类型 html文本格式
    用例: test01登陆
    1.初始化数据库:写test01
    2.列表存用例(发送请求),存预期
    3.发送请求
    4.比对响应结果
"""
import requests, pymysql

try:
    # 数据库初始化
    sqls = ["delete from user where username = 'test01'",
            "insert into user(id,username,password) values(2,'test01','123456')"
            ]
    conn = pymysql.connect(host='192.168.139.137', user='root', password='123456', db='exam')
    cursor = conn.cursor()
    for sql in sqls:  # 遍历列表
        cursor.execute(sql)
    conn.commit()
    conn.close()
    # 执行用例 (发送请求)
    case_data = [
        {'username': 'test01', 'password': '123456'},
        {'username': 'test01', 'password': ''},
        {'username': '', 'password': '123456'},
        {'username': '', 'password': ''},
        {'username': 'test001', 'password': '123456'},
        {'username': 'test01', 'password': '123'},
        {'username': 'test001', 'password': '1236'},
    ]
    expects = ['登录验证成功', '用户名或密码为空', '用户名或密码为空', '用户名或密码为空', '用户名或密码错误', '用户名或密码错误', '用户名或密码错误']
    cases = zip(case_data, expects)  # 打包:用例数据 预期结果
    # [(用例数据1,预期1),(用例数据2,预期2)]
    address = 'http://192.168.139.137/exam/login/'
    for arg, expect in cases:  # arg表示某一个参数,expect表示某一个预期
        res = requests.post(address, arg)  # 响应对象
        actual = res.text  # 实际结果
        if expect in actual:
            print("测试通过")
        else:
            print("测试失败\n\t预期结果:", expect, '\n\t实际结果:', actual)
except Exception as e:
    print(e)

测试注册接口

"""
测试注册接口,发送请求,检查响应结果正确性,如果测试失败,输出实际响应内容
注册测试用例
接口地址 : http://192.168.175.128/exam/signup/
用户名	密码	    确认密码	预期
test02	123456	123456	{'Status': 1000, 'Result': 'Success', 'Message': '注册成功'},
test03	123456	123456	{'Status': 1003, 'Result': 'Username test03 is taken', 'Message': '用户名已被占用'}
test04	1234	123456	{'Status': 1002, 'Result': 'Password Not Compare', 'Message': '两次输入密码的不一致'}
''	123456	123456	{'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'}
test05	''	 123456	{'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'}
test06	123456	''	{'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'}
''	''	''	{'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'}
思路:
    1.数据库初始化(删除test02,添加test03)
    2.用例存列表,预期存入列表
    3.循环发送请求,获得响应结果,比对结果
"""
import requests, pymysql

try:
    # 数据库初始化
    sqls = ["delete from user where username = 'test02'",
            "delete from user where username = 'test03'",
            "insert into user(id,username,password) values(3,'test03','123456')"]
    conn = pymysql.connect(host='192.168.139.137', user='root', password='123456', db='exam')
    cursor = conn.cursor()
    for sql in sqls:  # 遍历列表
        cursor.execute(sql)
    conn.commit()
    conn.close()
    # 执行用例 (发送参数给接口)
    cases_data = [
        {'username': 'test02', 'password': '123456', 'confirm': '123456','name':'测试01'},
        {'username': 'test03', 'password': '123456', 'confirm': '123456','name':'测试03'},
        {'username': 'test04', 'password': '1234', 'confirm': '123456','name':'测试04'},
        {'username': '', 'password': '123456', 'confirm': '123456','name':'测试10'},
        {'username': 'test05', 'password': '', 'confirm': '123456','name':'测试05'},
        {'username': 'test06', 'password': '123456', 'confirm': '','name':'测试06'},
        {'username': '', 'password': '', 'confirm': '','name':''}
    ]
    expects = [
        {'Status': 1000, 'Result': 'Success', 'Message': '注册成功'},
        {'Status': 1003, 'Result': 'Username test03 is taken', 'Message': '用户名已被占用'},
        {'Status': 1002, 'Result': 'Password Not Compare', 'Message': '密码错误'},
        {'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'},
        {'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'},
        {'Status': 1003, 'Result': 'Username test06 is taken', 'Message': '用户名已被占用'},
        {'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'}
    ]
    cases = zip(cases_data,expects)
    url = 'http://192.168.139.137/exam/signup/'
    i = 0 # 表示第i个预期结果
    for case,expect in cases:#遍历测试用例 获取参数
        res = requests.post(url,case)
        actual = res.json()
        if expect == actual:
            print('测试通过')
        else:
            print('测试失败\n\t预期结果:',str(expect),'\n\t实际结果:',str(actual))
        i = i + 1
except Exception as e:
    print(e)

注册接口完成落库检查

"""
测试注册接口,发送请求,检查响应结果和落库结果正确性
sqls=[
         "select count(*) from exam.user where username='test02'",
         "select count(*) from exam.user where username='test03'",
         "select count(*) from exam.user where username='test04'",
         "select count(*) from exam.user where username=''",
         "select count(*) from exam.user where username='test05'",
         "select count(*) from exam.user where username='test06'",
         "select count(*) from exam.user where username=''"
   ]
    dbexpect=[1,1,0,0,0,0,0]
思路:
    1.数据库初始化
        sqls=[很多sql语句]
        循环执行sql语句
    2.自动化执行多条测试用例
        case_data=[很多用例数据(格式都是字典)]
        expects=[很多接口返回预期结果(格式是文本或字典)]
        expect_sqls=[落库检查的sql语句]
        expect_db_rows=[很多预期的数据库中的行数]
        循环发送数据,同时比对接口返回,同时比对落库
"""
try:
    #数据库初始化
    sqls=["delete from user where username = 'test02'",
          "delete from user where username = 'test03'",
          "insert into user(id,username,password) values(3,'test03','123456')"
          ]
    # 测试数据
    case_data=[
        {'username': 'test02', 'password': '123456', 'confirm': '123456','name':'测试02'},
        {'username': 'test03', 'password': '123456', 'confirm': '123456','name':'测试03'},
        {'username': 'test04', 'password': '1234', 'confirm': '123456','name':'测试04'},
        {'username': '', 'password': '123456', 'confirm': '123456','name':'测试10'},
        {'username': 'test05', 'password': '', 'confirm': '123456','name':'测试05'},
        {'username': 'test06', 'password': '123456', 'confirm': '','name':'测试06'},
        {'username': '', 'password': '', 'confirm': '','name':''}
    ]
    # 预期结果
    expects=[
        {'Status': 1000, 'Result': 'Success', 'Message': '注册成功'},
        {'Status': 1003, 'Result': 'Username test03 is taken', 'Message': '用户名已被占用'},
        {'Status': 1002, 'Result': 'Password Not Compare', 'Message': '两次输入密码的不一致'},
        {'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'},
        {'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'},
        {'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'},
        {'Status': 1001, 'Result': 'Input Incomplete', 'Message': '输入信息不完整'}
    ]
    expect_sqls=[
        "select count(*) from user where username='test02'",
        "select count(*) from user where username='test03'",
        "select count(*) from user where username='test04'",
        "select count(*) from user where username=''",
        "select count(*) from user where username='test05'",
        "select count(*) from user where username='test06'",
        "select count(*) from user where username=''",
    ]
    expect_db_rows = [1,1,0,0,0,0,0]
    # 数据库初始化
    import requests,pymysql
    conn = pymysql.connect(host='192.168.139.137', user='root', password='123456', db='exam')
    cursor = conn.cursor()
    for sql in sqls:  # 遍历列表 初始化sql语句列表
        cursor.execute(sql)
    conn.commit()
    conn.close()
    #执行用例(发送请求,比对接口返回,落库检查)
    cases = zip(case_data,expects,expect_sqls,expect_db_rows)
    url = 'http://192.168.139.137/exam/signup/'
    for arg,expect,sql,expect_rows in cases:
        res = requests.post(url,arg)
        actual = res.json()
        # 判断接口返回正确性
        if expect==actual:
            print('接口返回正确')
        else:
            print('接口返回错误\n\t预期结果:',str(expect),'\n\t实际结果:',str(actual))
        #落库检查
        conn = pymysql.connect(host='192.168.139.137', user='root', password='123456', db='exam')
        cursor = conn.cursor()
        cursor.execute(sql)
        rows = cursor.fetchone()
        actual_rows = rows[0]
        if actual_rows == expect_rows:
            print("落库正确")
        else:
            print("落库失败\n\t预期结果:",str(expect_rows),'实际结果:',str(actual_rows))
        conn.close()
except Exception as e:
    print(e)
posted @ 2021-11-05 17:17  暄总-tester  阅读(104)  评论(0)    收藏  举报