第17次预习课-接口测试-20181031

10-31 接口测试

 

pip install requests

 

http协议

客户端(浏览器、使用cmd):发送请求包-》网站服务器,返回给客户服务器一个响应包

 

请求包:

首部:header(可为空)

主体:body(可为空)

 

响应包:

首部:header(可为空)

主体:body(可为空)

 

>>> import requests

>>> r=requests.get("http://www.baidu.com")

>>> r.url

'http://www.baidu.com/'

>>> r.text[:20]

'<!DOCTYPE html>\r\n<!-'

>>> r.headers  #响应头信息

{'Server': 'bfe/1.0.8.18', 'Date': 'Tue, 13 Nov 2018 03:33:13 GMT', 'Content-Typ

e': 'text/html', 'Last-Modified': 'Mon, 23 Jan 2017 13:27:36 GMT', 'Transfer-Enc

oding': 'chunked', 'Connection': 'Keep-Alive', 'Cache-Control': 'private, no-cac

he, no-store, proxy-revalidate, no-transform', 'Pragma': 'no-cache', 'Set-Cookie

': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Content-Encoding':

'gzip'}

>>> r.content

 

给get请求,加参数

url_params = {“q”:”gloryroad”}  #字典传递被添加到url中

r = requests.get(“https://cn.bing.com/search”, params=url_params)

print(r.url)

 

https://www4.bing.com/search?q=gloryroad

 

响应的内容

>>> type(r.content)

<class 'bytes'>  字节,如果有中文,会是unicode的编码

>>> type(r.text)

<class 'str'>   源码,显示中文

>>> r.encoding

'utf-8'

>>> r.status_code   #返回的状态码

200

>>> r.headers  #响应体的响应头信息

>>> r.raw    #返回原始响应体信息

<urllib3.response.HTTPResponse object at 0x000000000352A2B0>

r.json()   #将json串(本身是字符串)转为字典

r.raise_for_status()   #失败请求(非200响应)抛出异常

 

第一个接口程序:(每次需手动修改用户名,避免用户名重复)

#encoding=utf-8

import requests

import json

import os

import hashlib

 

print ("register------")

data = json.dumps({'username': 'wulaoshi27', 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}) #将字典转为str类型

r = requests.post('http://39.106.41.11:8080/register/', data= data)

print (r.status_code)

print (r.text)

print (type(r.json()))

print (r.json())

 

第二个接口程序(用户名随机,重复的几率小,但是还是有重复的可能)

#encoding=utf-8

import requests

import json

import os

import hashlib

import random

import string

username="".join([string.ascii_lowercase[random.randint(0,25)] for i in range(10)])

print (username)

 

print ("register------")

data = json.dumps({'username': username, 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}) #

r = requests.post('http://39.106.41.11:8080/register/', data= data)

print (r.status_code)

print (r.text)

print (type(r.json()))

print (str(r.json()))

 

assert "{'code': '00', 'userid':" in str(r.json())   #断言返回结果是否正确

如果断言出现错误:

 

第三个接口程序(每次执行一次之后,unique_number加1,可以生成唯一的用户名)

 

有一个data.txt文件,存初始的unique_number值

 

import requests

import json

import os

import hashlib

import pickle

 

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

with open("D:\\up\\1130\\data.txt") as fp:

    unique_number = fp.readline().strip()

 

print ("register------")

data = json.dumps({'username': 'xuefeifei'+unique_number, 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}) #

print('xuefeifei'+unique_number)

r = requests.post('http://39.106.41.11:8080/register/', data= data)

print (r.status_code)

print (r.text)

print (type(r.json()))

print (str(r.json()))

 

assert "{'code': '00', 'userid':" in str(r.json())

 

with open("D:\\up\\1130\\data.txt",'w') as fp:

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

 

 

第四个接口程序(将常用的封装,然后文件写成读写一次,不分两次,如果中间失败了data也累加)

 

有一个data.txt文件,存初始的unique_number值

#encoding=utf-8

import requests

import json

import os

import hashlib

import pickle

 

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("D:\\up\\1130\\data.txt","r+") as fp:

    unique_number = fp.readline().strip()

    fp.seek(0,0)

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

    

interface='http://39.106.41.11:8080/register/'

value = json.dumps({'username': 'userdata'+unique_number, 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}) #

 

r=send_request(interface,value)

get_response_info(r)

assert_response(r,"{'code': '00', 'userid':")

 

第五个接口程序(将数据和程序分离出)

 

1)conf.py

ip="39.106.41.11"

port="8080"

 

register="http://"+ip+":"+port+"/register/"

 

2)程序

#encoding=utf-8

import requests

import json

import os

import hashlib

import pickle

from conf import *

 

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("D:\\up\\1130\\data.txt","r+") as fp:

    unique_number = fp.readline().strip()

    fp.seek(0,0)

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

    

interface=register

value = json.dumps({'username': 'xuefeifei'+unique_number, 'password':

 

'wulaoshi12345', 'email': 'wulaoshi@qq.com'}) #

 

r=send_request(interface,value)

get_response_info(r)

assert_response(r,"{'code': '00', 'userid':")

 

第六个接口程序(测试数据和程序分离)

 

test_data.txt

register|{'username': 'userdata'+unique_number, 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}|{'code': '00', 'userid':

 

 

测试程序

#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("D:\\up\\1130\\data.txt","r+") as fp:

    unique_number = fp.readline().strip()

    fp.seek(0,0)

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

 

with open("D:\\up\\1130\\test_data.txt","r+") as fp:  

    line=fp.readline()

    interface=eval(line.split("|")[0])

    value=json.dumps(eval(line.split("|")[1]))

    assert_word=line.split("|")[2]

 

#print(interface)

#print(type(value))

r=send_request(interface,value)

get_response_info(r)

assert_response(r,assert_word)   #可以try一下,让程序断言失败不中断

 

static_data["username"]=eval(line.split("|")[1])["username"]

print(static_data["username"])

 

posted @ 2018-12-05 11:16  feifei_tian  阅读(126)  评论(0编辑  收藏  举报