码迷,mamicode.com
首页 > 其他好文 > 详细

单接口测试框架

时间:2018-12-01 17:03:01      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:coding   hash   res   line   变量   enc   word   pytho   eva   

实现测试数据和代码的分离。

#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: 109828@qq.com}|{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: 109828@qq.com}|{code: 00, userid:

 



单接口测试框架

标签:coding   hash   res   line   变量   enc   word   pytho   eva   

原文地址:https://www.cnblogs.com/huiziz/p/10049789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!