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')
  • 相关阅读:
    反向代理实例
    nginx常用命令和配置
    nginx的安装
    Can Live View boot up images acquired from 64bit OS evidence?
    What is the behavior of lnk files?
    EnCase v7 search hits in compound files?
    How to search compound files
    iOS 8.3 JB ready
    Sunglasses
    现代福尔摩斯
  • 原文地址:https://www.cnblogs.com/sprouts/p/7685698.html
Copyright © 2011-2022 走看看