• 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')
  • 相关阅读:
    luoguP4389 付公主的背包 多项式exp
    bzoj3456 城市规划 多项式求In
    luoguP4491 [HAOI2018]染色 广义容斥原理 + FFT
    计蒜之道2019复赛题解
    Educational Codeforces Round 66 (Rated for Div. 2)
    [CodeChef-ANUDTQ] Dynamic Trees and Queries
    M-SOLUTIONS Programming Contest
    Codeforces Global Round 3
    PKUSC2019题解
    [LOJ#3120][Luogu5401][CTS2019]珍珠(容斥+生成函数)
  • 原文地址:https://www.cnblogs.com/sprouts/p/7685698.html
Copyright © 2020-2023  润新知