前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python最简单的mock方式,使用pook模拟 HTTP 请求结果的库

python最简单的mock方式,使用pook模拟 HTTP 请求结果的库

作者头像
胡八万
发布2022-05-16 11:59:52
8430
发布2022-05-16 11:59:52
举报
文章被收录于专栏:软件测试技术软件测试技术
开发还没写好接口,我应该怎么提前准备接口测试数据?这里提供了一种最简单的mock方式,可以提前根据接口文档,自行准备接口数据的方式。

pook官方文档[https://pook.readthedocs.io/en/latest/]

安装

pip install pook

Getting started

通过使用@pook.on装饰器的方式:

代码语言:javascript
复制
import pook
import requests

@pook.on
def test_my_api():
    mock = pook.get('http://baidu.com/api/1/foobar', reply=404, response_json={'error': 'not found'})

    resp = requests.get('http://baidu.com/api/1/foobar')
    print(resp.status_code)
    print(resp.json())
    print(mock.calls)

test_my_api()

通过使用@pook.get装饰器的方式:

代码语言:javascript
复制
import pook
import requests
import json

@pook.get('http://httpbin.org/status/500', reply=204, response_json={"1":"123"})
@pook.get('http://httpbin.org/status/400', reply=200,response_json={"1":"123"})
def fetch(url):
    return requests.get(url)

res = fetch('http://httpbin.org/status/400')
print('#1 status:', res.status_code)
print(res.json())

res = fetch('http://httpbin.org/status/500')
print('#2 status:', res.status_code)

使用请求的基本mock方式:

代码语言:javascript
复制
import pook
import requests


# Enable mock engine
pook.on()

pook.get('httpbin.org/ip',
         reply=403, response_type='json',
         response_headers={'server': 'pook'},
         response_json={'error': 'not found'})

pook.get('httpbin.org/headers',
         reply=404, response_type='json',
         response_headers={'server': 'pook'},
         response_json={'error': 'not found'})

res = requests.get('http://httpbin.org/ip')
print('Status:', res.status_code)
print('Headers:', res.headers)
print('Body:', res.json())

res = requests.get('http://httpbin.org/headers')
print('Status:', res.status_code)
print('Headers:', res.headers)
print('Body:', res.json())

print('Is done:', pook.isdone())
print('Pending mocks:', pook.pending_mocks())
print('Unmatched requests:', pook.unmatched_requests())

可链接API DSL,模拟访问post请求:

代码语言:javascript
复制
import json
import pook
import requests


# Enable mock engine
pook.on()

(pook.post('httpbin.org/post')
    .json({'foo': 'bar'})
    .type('json')
    .header('Client', 'requests')
    .reply(204)
    .headers({'server': 'pook'})
    .json({'error': 'simulated'}))

res = requests.post('http://httpbin.org/post',
                    data=json.dumps({'foo': 'bar'}),
                    headers={'Client': 'requests',
                             'Content-Type': 'application/json'})

print('Status:', res.status_code)
print('Body:', res.json())

print('Is done:', pook.isdone())
print('Pending mocks:', pook.pending_mocks())

更多API说明: https://pook.readthedocs.io/en/latest/api.html#core-api

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-05-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装
  • Getting started
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档