Recently, I am writing a test platform, and I need to implement an api of a node server. I am using django, and I am going to use the djangorestframework plugin to implement it.
need
Implement an interface, when calling, through the passed parameters, directly run the automated test of the corresponding project
environment
Python3.6 ,PyCharm,W7
Project structure
Function realization
Process All
we have to do is to implement the above process安装
pip install djangorestframework
pip install markdown
pip install django-filter # Filtering supportconfigure
INSTALLED_APPS = ( ... 'rest_framework', )
Write code (this code does not involve database operations, just simply write an api)
①: Open AutoApi/Api/views.py and write the following code
from django.http import JsonResponse, HttpResponseNotAllowed, HttpResponse from django.views.decorators.csrf import csrf_exempt from rest_framework.parsers import JSONParser from rest_framework import status @csrf_exempt def run_job(request): # 判断请求头是否为json if request.content_type != 'application/json': # 如果不是的话,返回405 return HttpResponse('only support json data', status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE) # 判断是否为post 请求 if request.method == 'POST': try: # 解析请求的json格式入参 data = JSONParser().parse(request) except Exception as why: print(why.args) else: content = {'msg': 'SUCCESS'} print(data) # 返回自定义请求内容content,200状态码 return JsonResponse(data=content, status=status.HTTP_200_OK) # 如果不是post 请求返回不支持的请求方法 return HttpResponseNotAllowed(permitted_methods=['POST'])
②: Open AutoApi/Api/urls.py and write the following code
from django.conf.urls import url from Api import views urlpatterns = [ url(r'^runJob/$',views.run_job), ]
③: Open AutoApi/AutoApi/urls.py and modify the following code
ALLOWED_HOSTS = '*' # 修改为* 代码允许任意host from django.conf.urls import url,include urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^',include('Api.urls')),# 新增 ]
④: Start the service
python manage.py runserver 0.0.0.0:8080
⑤: We request to try it out
The above is a simple implementation of an api. In fact, the development interface is so simple and not so mysterious.
接下来把post 的数据env ,project,cases 解析出来传给对应的自动化测试入口函数,就可以实现通过接口请求,启动自动化测试的目的。
follow-up
- Implement interface call automation test project
- implement asynchronous interface
- Implement timed tasks
.
.
.
The above is a summary of some recent work, come on!