Python+Django接口自動化
?引言:
? ? ? ? ?最近被幾個公司實習(xí)生整自閉了,沒有基礎(chǔ),想學(xué)自動化又不知道怎么去學(xué),沒有方向沒有頭緒,說白了其實就是學(xué)習(xí)過程中沒有成就感,所以學(xué)不下去。出于各種花里胡哨的原因,今天給大家整一個簡單又有成就感的接口自動化學(xué)習(xí)吧。
? ? ? ? ?不皮了,進(jìn)入正題。本文中用到的技術(shù)點有:Python基礎(chǔ)、Django基礎(chǔ)、Request庫、一丟丟前端基礎(chǔ)。(考慮到大家零基礎(chǔ),所以文中代碼編寫使用純新手手法)
1、先創(chuàng)建一個Django項目(具體請參考Django基礎(chǔ)入門教程)2、創(chuàng)建一個模板,新增一個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>參數(shù)類型:</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>測試數(shù)據(jù):</td>
<td>
<textarea name="testdate" style="with:200px;height:60px;"></textarea>
</td>
</tr>
<tr>
<td>預(yù)期結(jié)果:</td>
<td>
<input type="text" name="exr" /> <br/>
</td>
</tr>
<tr>
<td>實際結(jié)果:</td>
<td>
<label>{{ data1 }}</label><br/>
</td>
</tr>
<tr>
<td>測試結(jié)果:</td>
<td>
<label>{{ data }}</label><br/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="執(zhí)行測試" />
</td>
</tr>
</table>
</form>
</body>
</html>
3、在項目包中新建py文件,封裝接口調(diào)用類
import requests
import json
class cls_api:
def post(self,url,par):
a_url=url
a_par=par
res=requests.post(a_url,a_par)
return res
def get(self,url,par):
a_url=url
a_par=par
res=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 postapi
def 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+y
def 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']=res
d=json.dumps(d)
return HttpResponse(d)
else:
return HttpResponse(u'輸入錯誤')
else:
return HttpResponse(u'輸入為空')
else:
return HttpResponse(u'方法錯誤')
add_args函數(shù)處理加法運算,index函數(shù)接收前端POST數(shù)據(jù),處理數(shù)據(jù)并返回結(jié)果,post函數(shù)處理接口請求并返回結(jié)果
記得添加Django路由
在urls中加入:
運行項目
輸入?yún)?shù):
執(zhí)行測試:
好了,以上就是一個簡單的Django接口測試開發(fā),比較適合基礎(chǔ)差的同學(xué)入門學(xué)習(xí)
學(xué)習(xí)最好的老師是興趣,興趣最好的培養(yǎng)就是成就感,希望大家在學(xué)習(xí)的過程中都能找到成就感。文章來源:http://www.zghlxwxcb.cn/news/detail-606287.html
【B站最全最易學(xué)】十年大佬終于將測試開發(fā)路線整理出來了,小白一學(xué)就會,拿走不謝,允許白嫖!!文章來源地址http://www.zghlxwxcb.cn/news/detail-606287.html
到了這里,關(guān)于【測試開發(fā)】Python+Django實現(xiàn)接口測試工具的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!