zoukankan      html  css  js  c++  java
  • Python requests模块做接口测试

    将接口相关信息写入到Excel中,然后用此脚本从Excel中读取相应的信息并组装成URL来发送接口并获取返回的结果,并将结果写入到对应的用例中.

    import requests
    import json
    import os, sys
    from openpyxl import load_workbook
    
    result_col_index = 6
    
    
    def runtest(url, data, head):
        r = requests.post(url=url, data=json.dumps(data), headers=head)
        return r.json()
    
    
    def get_testcase(env, testcasefile):
        testcasefile = os.path.join(os.getcwd(), testcasefile)
        if not os.path.exists(testcasefile):
            print('测试用例文件不存在!')
            sys.exit()
        wb = load_workbook(testcasefile)
        ws = wb.get_sheet_by_name('testcase')
        rows = ws.max_row
        print(rows)
        if rows < 2:
            print('测试用例文件中没有用例数据,请确认后再执行!')
            sys.exit()
        for i in range(rows - 1):
            col = str(i + 2)
            casename = ws['B' + col].value
            url = ws['C' + col].value
            data = ws['D' + col].value
            head = ws['E' + col].value
            result = ws['' + col].value
            url = r'http://haenkg-' + env + '.huawei.com' + url
            print(type(url), url)
            try:
                data = eval(data)
                head = eval(head)
            except SyntaxError as e:
                print(e)
                print('json格式不正确,请确认后再提交!!')
                sys.exit()
            actjson = runtest(url, data, head)
            result = eval(result)
            print('this is actjson:', type(actjson), actjson)
            print('this is result:', type(result), result)
            if actjson['result'] == result['result']:
                print(casename + ':成功')
                ws['G' + col] = '成功'
            else:
                print(casename + ':失败!!!')
                ws['G' + col] = '失败'
                wb.save(testcasefile)
    
    
    if __name__ == "__main__":
        if len(sys.argv) == 1:
            print("请输入要执行的环境!!")
            sys.exit()
        envs = ['dev9', 'sit2', 'sit3', 'uat2', 'uat3']
        if sys.argv[1] not in envs:
            print("请输入:['dev9','sit2','sit3','uat2','uat3']中的一个环境!!")
            sys.exit()
        get_testcase(sys.argv[1], 'testcases.xlsx')
  • 相关阅读:
    移动端获取屏幕的宽度,根据屏幕大小动态设置html的rem字体大小
    解析CSS3伪类选择器nth-of-type和nth-child的用法,以及两者的区别
    移动端的1px的解决方案
    Vue中import from的来源:省略后缀与加载文件夹
    flex布局
    前端开发人员快速创建本地服务器
    centos6.5Xen4.2安装
    centos6.5kvm虚拟化安装部署
    CentOS搭建svn服务器支持https访问
    CentOS6.5搭建LNMP
  • 原文地址:https://www.cnblogs.com/sprouts/p/7685698.html
Copyright © 2011-2022 走看看