1. 程式人生 > >單接口測試框架

單接口測試框架

ons val nbsp .post enc {} txt文件 post see

實現測試數據和代碼的分離。

#encoding=utf-8
import requests
import json
import os
import hashlib
import pickle
from conf import *
static_data = {}
def send_request(interface,value):
    r = requests.post(interface, data= value)
    return r

def get_response_info(response_obj):
    print (response_obj.status_code)
    
print (response_obj.text) print (type(response_obj.json())) print (str(response_obj.json())) print (response_obj.url) def assert_response(response_obj,assert_word): assert assert_word in str(response_obj.json()) #需要默認文件裏面寫個初始化值1 with open("E:\\workspace-python\\test\\test1.txt"
,"r+") as fp: unique_number = fp.readline().strip() fp.seek(0,0) fp.write(str(int(unique_number)+10)) with open("E:\\workspace-python\\test\\test_data.txt","r+") as fp: line=fp.readline() print("line*******",line) interface=eval(line.split("|")[0]) #等價於:interface=register.把字符串變成變量用eval
print("line*******",interface) value=json.dumps(eval(line.split("|")[1])) #有‘userdata‘+unique_number, 要把它變成變量 assert_word=line.split("|")[2] #print(interface) #print(type(value)) r=send_request(interface,value) get_response_info(r) try: assert_response(r,assert_word) except: print ("接口中斷") static_data["username"]=eval(line.split("|")[1])["username"] print(static_data["username"])

執行結果:

E:\workspace-python\test>py -3 b.py
line******* register|{username: userdata+unique_number, password: zita123
45, email: [email protected]}|{code: 00, userid:
line******* http://39.106.41.11:8080/register/
200
{"code": "00", "userid": 55157}
<class dict>
{code: 00, userid: 55157}
http://39.106.41.11:8080/register/
userdata188
test1.txt文件裏是初始值
test_data.txt
register|{username: userdata+unique_number, password: zita12345, email: [email protected]}|{code: 00, userid:



單接口測試框架