Python搭建接口自动化测试框架
作者:互联网
项目介绍
AutoApiTest
基于python的接口自动化测试框架
Test部分基于yingoja开源的DemoApi优化修改而来
API部分将继续完善,提供基于C#,Go,Java,Python版本的Api服务程序,目的是为了学习接口测试的同学不需要去搭建其他语言的运行环境,顺便我也复习一下这几门语言的基础语法。
项目仓库
https://github.com/dwBurning/AutoApiTest.git
API部分-Python-Django
django之前写过一系列的博文,其实也就是入个门吧!好比考驾照,考了驾照好些年没有摸过车,但是如果买了车,还是可以直接上手,就是这么回事,入过门,如果项目需要,不至于白手起家
创建django
django-admin startproject projectname
python manage.py startapp appname
运行
Microsoft Windows [版本 6.3.9600]
(c) 2013 Microsoft Corporation。保留所有权利。D:\项目\github\AutoApiTest\Api\Python>cd AutoApiTest
D:\项目\github\AutoApiTest\Api\Python\AutoApiTest>python manage.py runserver 127.0.0.1:5000
Watching for file changes with StatReloader
Performing system checks...System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
July 25, 2021 - 21:48:51
Django version 2.2.5, using settings 'AutoApiTest.settings'
Starting development server at http://127.0.0.1:5000/
Quit the server with CTRL-BREAK.
核心代码:
import json
from django.http import HttpResponse, JsonResponse
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Person
from .serializers import PersonSerializers
class Contact():
def __init__(self):
self.contacts = []
def add(self, person):
if person not in self.contacts:
self.contacts.append(person)
else:
print("exists")
def get(self, person):
try:
pos = self.contacts.index(person)
return self.contacts[pos]
except:
return None
def get_all(self):
return self.contacts
def delete(self, person):
self.contacts.remove(person)
contact = Contact()
class PersonController(APIView):
def get(self, request, id=0):
if id:
p = Person(id)
if contact.get(p):
serializer = PersonSerializers(instance=contact.get(p))
return JsonResponse({'code': 0, 'message': '获取成功', 'data': serializer.data}, safe=False, json_dumps_params={'ensure_ascii': False})
else:
return JsonResponse({'code': -1, 'message': '人事资料不存在', 'data': None}, safe=False, json_dumps_params={'ensure_ascii': False})
else:
serializer = PersonSerializers(
instance=contact.get_all(), many=True)
return JsonResponse({'code': 0, 'message': '获取成功', 'data': serializer.data}, safe=False, json_dumps_params={'ensure_ascii': False})
def delete(self, request, id=0):
p = Person(id)
if contact.get(p):
contact.delete(p)
return JsonResponse({'code': 0, 'message': '删除成功', 'data': None}, safe=False, json_dumps_params={'ensure_ascii': False})
else:
return JsonResponse({'code': -1, 'message': '人事资料不存在', 'data': None}, safe=False, json_dumps_params={'ensure_ascii': False})
def patch(self, request, id):
data = json.loads(request.body)
p = contact.get(Person(id))
if p:
for key, value in data.items():
setattr(p, key, value)
return JsonResponse({'code': 0, 'message': '修改成功', 'data': None}, safe=False, json_dumps_params={'ensure_ascii': False})
else:
return JsonResponse({'code': -1, 'message': '人事资料不存在', 'data': None}, safe=False, json_dumps_params={'ensure_ascii': False})
def post(self, request):
data = json.loads(request.body)
p = Person()
for key, value in data.items():
setattr(p, key, value)
person = contact.get(p)
if person:
return JsonResponse({'code': -1, 'message': '人事资料已存在', 'data': None}, safe=False, json_dumps_params={'ensure_ascii': False})
else:
contact.add(p)
return JsonResponse({'code': 0, 'message': '添加成功', 'data': None}, safe=False, json_dumps_params={'ensure_ascii': False})
标签:False,Python,self,JsonResponse,接口,json,return,data,搭建 来源: https://www.cnblogs.com/dwBurning/p/autoapitest-python.html