Python搭建接口自动化测试框架
2021/7/25 22:06:14
本文主要是介绍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})
这篇关于Python搭建接口自动化测试框架的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型
- 2024-12-23使用python部署一个usdt合约,部署自己的usdt稳定币
- 2024-12-20Python编程入门指南
- 2024-12-20Python编程基础与进阶
- 2024-12-19Python基础编程教程
- 2024-12-19python 文件的后缀名是什么 怎么运行一个python文件?-icode9专业技术文章分享