1. 程式人生 > >單介面測試框架

單介面測試框架

實現測試資料和程式碼的分離。

#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':