【測試開發天花板】Django+Vue+PyTest打造企業級自動化平臺|能寫進簡歷的硬核項目
?最近被幾個公司實習生整自閉了,沒有基礎,想學自動化又不知道怎么去學,沒有方向沒有頭緒,說白了其實就是學習過程中沒有成就感,所以學不下去。出于各種花里胡哨的原因,今天給大家整一個簡單又有成就感的接口自動化學習吧。
? ? ? ? ?不皮了,進入正題。本文中用到的技術點有:Python基礎、Django基礎、Request庫、一丟丟前端基礎。(考慮到大家零基礎,所以文中代碼編寫使用純新手手法)
1、先創建一個Django項目(具體請參考Django基礎入門教程)
2、創建一個模板,新增一個index.html頁面
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Parker接口測試</title> </head> <body><h3 style="...">接口測試</h3><form action="/index/" method="post"><table><tr><td>接口地址:</td><td><input type="text" name="url" /> <br/></td></tr><tr><td>選擇方法:</td><td><input type="radio" name="fun" value="post" checked/>POST<input type="radio" name="fun" value="get" />GET</td></tr><tr><td>參數類型:</td><td><select name="leixin"><option value="json">Json</option><option value="xml">XML</option><option value="date">Data</option></select></td></tr><tr><td>測試數據:</td><td><textarea name="testdate" style="with:200px;height:60px;"></textarea></td></tr><tr><td>預期結果:</td><td><input type="text" name="exr" /> <br/></td></tr><tr><td>實際結果:</td><td> <label>{{ data1 }}</label><br/></td></tr><tr><td>測試結果:</td><td><label>{{ data }}</label><br/></td></tr><tr><td><input type="submit" value="執行測試" /> </td></tr></table></form> </body> </html>
3、在項目包中新建py文件,封裝接口調用類
import requests import json class cls_api:def post(self,url,par):a_url=urla_par=parres=requests.post(a_url,a_par)return resdef get(self,url,par):a_url=urla_par=parres=requests.get(a_url,a_par)return res
4、在views文件中添加如下代碼
#-*- coding:utf-8 -*- from django.shortcuts import render from django.http import HttpResponse import json from parkerapi import postapidef index(request):pt=postapi.cls_api()exr=request.POST.get('exr',None)data=""data1=""if request.method=='POST':data=pt.post(request.POST.get('url',None), json.loads(request.POST.get('testdate',None)))result=data.json()data1=result['message']if int(result['message']==int(exr)):data=u'測試通過' else:data=u'測試失敗' return render(request,"index.html",{"data":data,"data1":data1})def add_args(a,b):x=int(a)y=int(b)return x+ydef post(request):if request.method=='POST':d={}if request.POST:a=request.POST.get('a',None)b=request.POST.get('b',None)if a and b:res=add_args(a, b)d['message']=resd=json.dumps(d)return HttpResponse(d)else:return HttpResponse(u'輸入錯誤')else:return HttpResponse(u'輸入為空')else:return HttpResponse(u'方法錯誤')
add_args函數處理加法運算,index函數接收前端POST數據,處理數據并返回結果,post函數處理接口請求并返回結果
記得添加Django路由
在urls中加入:
運行項目
輸入參數:
執行測試:
好了,以上就是一個簡單的Django接口測試開發,比較適合基礎差的同學入門學習
學習最好的老師是興趣,興趣最好的培養就是成就感,希望大家在學習的過程中都能找到成就感。
【測試開發天花板】Django+Vue+PyTest打造企業級自動化平臺|能寫進簡歷的硬核項目