zoukankan      html  css  js  c++  java
  • 简单的测试登录接口的脚本

    脚本很简单,就是初级的只有用户名和密码就可以登录的接口测试

    需要先新建一个excel文件用来保存用例,以下是excel详情,就写了具体数据,其余字段没写

    具体代码如下:

    import requests, time
    import xlrd, xlwt
    from xlutils.copy import copy
    
    
    # 打开excel
    def read_excel(file_path):
        try:
            book = xlrd.open_workbook(file_path)  # 打开Excel
        except Exception as e:
            print('路径错误或者excel不正确
    ', e)
        else:
            sheet = book.sheet_by_index(0)  # 打开第一张表
            rows = sheet.nrows  # 文档中总行数
            case_list = []
            for row in range(rows):
                if row != 0:  # 出除表头
                    case_list.append(sheet.row_values(row))  # 把每一行数据组成一个大列表
            interface_test(case_list)
    
    
    # 请求接口
    def interface_test(case_list):
        url = '/apiauth/login/login'  # 接口地址
        res_list = []
        for row_list in case_list:
            json = {"userName": row_list[1],  # 列表中的第2个元素是用户名
                    "password": row_list[2]}  # 列表中的第3个元素是密码
            reponse = requests.request(url=url, method='post', json=json)
            res_list.append(reponse.text)  # 把请求结果放到一个list里面
        test_results(file_path, res_list)
      
        
    # 分析结果并保存
    def test_results(file_path, res_list):
        book = xlrd.open_workbook(file_path)  # 打开原来的excel,获取到这个book对象
        new_book = copy(book)  # 复制一个new_book,无法在原文件中写入,只能复制之后再写
        new_sheet = new_book.get_sheet(0)  # 打开第一张表
        i = 1  # 代表行数,从第2行还是写入
        for res in res_list:
            # 判断结果是否是登录成功,不成功就给字体标红,否则(即成功)不加字体样式,给默认黑色
            if '"errCode":0' not in res:
                '''
                给字体加颜色,固定写法
                '''
                font = xlwt.Font()
                font.colour_index = xlwt.Style.colour_map['red']
                style = xlwt.XFStyle()
                style.font = font
                
                # 将内容按样式填写到单元格中
                new_sheet.write(i, 3, res, style)
            else:
                new_sheet.write(i, 3, res)
            i += 1
        # 以当前时间命名并保存文件
        new_book.save('./%s_测试结果.xls' % time.strftime('%Y%m%d%H%M%S'))
    
    
    if __name__ == '__main__':
        file_path = './logintest.xlsx'  # 测试用例文件路径
        read_excel(file_path)
        print('执行完毕!!!')

    执行结果:

    1、在指定路径生成一个测试结果文档

     2、文档详情:

     登录不成功的结果标红显示

    。。。

    结束

  • 相关阅读:
    SpringBoot快速入门(三)
    分布式解决方案
    计算机网络-自顶向下方法第六章
    计算机网络-自顶向下方法第四章
    springcloud记录
    计算机网络-自顶向下方法第三章
    springboot整合kafka
    计算机网络-自顶向下方法第二章
    计算机网络-自顶向下方法第一章
    Spring常用注解【经典总结】
  • 原文地址:https://www.cnblogs.com/caoyinshan/p/12754856.html
Copyright © 2011-2022 走看看