python接口自动化系列(09):发送http请求

全栈测试笔记 / 2024-03-04 / 原文

 

本系列汇总,请查看这里:https://www.cnblogs.com/uncleyong/p/18033074

实现目标

发送http请求,获取服务器响应内容

 

关于被测试接口

 

安装模块

requests提供了方便易用的HTTP请求功能

pip install requests

 

修改测试类

添加发送http请求逻辑(建议大家封装工具类做优化)

        if method.upper() == 'GET':
            try:
                res = requests.get(url=url, headers=headers,  params=params_, timeout=10)
                logger.info("执行请求后,结果是:%s" % res.text)

            except Exception as e:
                logger.error('出错了,错误是%s' % e)
                raise e

        elif method.upper() == 'POST':
            # 执行请求
            try:
                res = requests.post(url=url, headers=headers, json=params_, timeout=10)
                logger.info("执行请求后,结果是:%s" % res.text)
            except Exception as e:
                logger.error('出错了,错误是%s' % e)
                raise e

  

修改用例数据

因为第三条用例依赖关联数据,我们还没保存关联数据,暂时先只保留case.yaml中的前两条用例数据

 

执行结果

可以看到,两个用例请求都成功了

 

【bak】