【接口自动化】测试框架介绍

3/28/2023 接口自动化

# 测试框架基本能力


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

# HTTP测试能力


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

# 测试框架


Requests

# 常见http请求方法构造

  • 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

# 简单测试

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
1
2
3
4
5
6
7
8

# 接口测试断言-响应结果

  • 基本信息: r.url、r.status_code、r.headers、r.cookies
  • 响应结果
    • r.text = r.encoding + r.content
    • r.json() = r.encoding + r.content + content type json
    • r.raw.read(10)
  • 对应的请求内容:r.request
def test_header(self):
    r = requests.get("http://httpbin.testing-studio.com/get", headers={"h": "header demo"})
    print(r.status_code)
    print(r.text)
    print(r.json())
    assert r.json()['headers']['H'] == "header demo"
1
2
3
4
5
6
上次更新: 2025年02月04日 13:37:36