python 接口数据驱动_python接口测试实例--数据驱动(程序与数据分离)

#encoding=utf-8

import requests

import json

import os

import hashlib

import pickle

from conf import *

static_data = {}

def send_request(interface,value):       #将请求封装成函数send_request

r = requests.post(interface, data= value)

return r

def get_response_info(response_obj):      #将响应封装成函数get_response_info

print ("-----------------------------接口响应-------------------------")

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_response

assert assert_word in str(response_obj.json())

#需要默认文件里面写个初始化值1

with open("e:\\data.txt","r+") as fp:        #打开data.txt文件

unique_number = fp.readline().strip()      #读取data.txt文件的参数(多行的话要加for循环)

fp.seek(0,0)                  #游标回到初始位置,为修改参数做准备

fp.write(str(int(unique_number)+10))

with open("e:\\test_data.txt","r+") as fp:        #打开文件e:\\test_data.txt,文件内参数格式parameter1|parameter2|parameter3

line=fp.readline()

interface=eval(line.split("|")[0])            #切片取【0】位置的参数register(具体值由from conf import *导入)作为请求的url

value=json.dumps(eval(line.split("|")[1]))        #读取数据后切片取【1】位置的参数作为请求的参数

assert_word=line.split("|")[2]              #读取数据后切片取【2】位置的参数作为响应的断言值

#print(interface)

#print(type(value))

r=send_request(interface,value)          #调用函数send_request,请求接口,并把响应赋值给r

get_response_info(r)                #调用响应内容函数get_response_info,打印请求的响应信息

assert_response(r,assert_word)            #调用断言函数assert_response,判断用例是否通过

static_data["username"]=eval(line.split("|")[1])["username"]          #将username存到全局变量中,下次有关联时使用

print(static_data["username"])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值