基于django rest framework的mock server实践

网上找了一下mock server的实现,发现python的基本都是基于flask来实现的,因最近在学django,就尝试用drf实现了下:

A brief introduction of sui_mock

sui mock server is an attempt that aim at dealing some issues of the third part service,get it from https://github.com/chengtch/sui-mock

Reference resources:
simple mock from testerhome with flask https://testerhome.com/topics/10238
ali mock platform https://yq.aliyun.com/articles/236198

1) Why we need mock

1.1 To simulate unstable services

    Because there is no business logic in mock server,that makes it stable enough

2.2 To quickly construct complex data

    Because we can customize a response

2.3 To rapidly construct abnormal scenes

    Because we can return any response code we want

2.4 To help the coders impove coding efficiency

    Because they don't need to wait too long for debugging

2.5 To help the testers get better working

    As a tester ,you should know what i am trying to say

2) Design logic

    Django + rest framework + mysql
CA6XB4N.png

3) Steps to complete

  • 3.1 create new django project named sui_mock
  • 3.2 create new app named app_mock
  • 3.3 design the models with all the api params
  • 3.4 make configrations of the settings about db,app,language
  • 3.5 makemigrations\migrate\createsuperuser: admin/your_password
  • 3.6 register the model class in the admin.py
  • 3.7 edit the serializer and views to implement the function logic
  • 3.8 config the urls.py to match the request path
  • 3.9 install all the dependency (get it from the requirement.txt)
  • 3.10 start the mockserver by python manage.py runserver 0.0.0.0:8000

4) Examples

4.1 Start the server and add an api in the backstage management system

NeX4Ql1.png
j4fy8Gf.png

4.2 Call the api by postman

J6hqWXD.png

5) Run server by https

first install some thirdpart libs:

pip install django-extensions
pip install django-werkzeug-debugger-runserver
pip install pyOpenSSL

then run the server:

python manage.py runserver_plus --cert server.crt 0.0.0.0:8000

finally visit the https server:

eg: https://192.168.254.1:8000/cnp/auth

转载于:https://www.cnblogs.com/chengtch/p/9570587.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值