一、接口测试框架Requests

测试框架基本能力

  • 项目管理:pip、 virtualenv
  • 用例编写:pytest
  • 领域能力:app、 web、http
  • 执行调度:pytest、pycharm、shell、jenkins
  • 测试报告:allure2

HTTP测试能力

  • 请求方法构造:get、post、put、delete、head... ...
  • 请求体构造:form、json、xml、binary
  • 响应结果分析:status code、response body、json path、xpath

Requests框架特点

  • 功能全面:http/https支持全面
  • 使用简单:简单易用,不用关心底层细节
  • 定制性高:借助于hook机制完成通用处理
  • 官网:https://docs.python-requests.org/zh_CN/latest/
image.png

Requests常见http请求构造方法

import requests

r = requests.put('https://httpbin.org/put', data={'key':'value'})
r = requests.delete('https://httpbin.org/delete')
r = requests.head('https://httpbin.org/get')
r = requests.options('https://httpbin.org/get')
  • 演练环境:http://httpbin.testing-studio.com
    image.png

实战环节

  • 测试脚本
import requests


class TestDemo:
    def test_get(self):
        r = requests.get("http://httpbin.testing-studio.com/get")
        print(r.status_code)
        print(r.text)
        print(r.json())
        assert r.status_code == 200
  • 测试结果
test_requests.py::TestDemo::test_get 

============================== 1 passed in 0.76s ==============================

Process finished with exit code 0
PASSED                              [100%]200
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.testing-studio.com", 
    "User-Agent": "python-requests/2.25.0", 
    "X-Forwarded-Host": "httpbin.testing-studio.com", 
    "X-Scheme": "https"
  }, 
  "origin": "101.205.253.7", 
  "url": "https://httpbin.testing-studio.com/get"
}

{'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.testing-studio.com', 'User-Agent': 'python-requests/2.25.0', 'X-Forwarded-Host': 'httpbin.testing-studio.com', 'X-Scheme': 'https'}, 'origin': '101.205.253.7', 'url': 'https://httpbin.testing-studio.com/get'}

你可能感兴趣的:(一、接口测试框架Requests)