网站建设资讯

NEWS

网站建设资讯

接口测试的几种组织形式-创新互联

注册接口测试

创新互联是专业的安徽网站建设公司,安徽接单;提供网站制作、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行安徽网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
1、单个接口测试
#encoding=utf-8
import requests
import json

data = {'username': 'test001', 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}

data = json.dumps(data)

res =  requests.post('http://39.106.41.11:8080/register/',data)
print(res.text)
print(res.status_code)
print(res.json())

2、单个接口带断言
#encoding=utf-8
import requests
import json
import re

pattern = re.compile(r"{'code': '00', 'userid': \d+}")
data = {'username': 'test003', 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}

data = json.dumps(data)

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

print(res.text)
print(res.status_code)
print(res.json())
print(str(res.json()))
assert pattern.match(str(res.json()))

3、使用随机参数
#encoding=utf-8
import requests
import json,random
import re,string

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

pattern = re.compile(r"{'code': '00', 'userid': \d+}")
data = {'username': username, 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}

data = json.dumps(data)

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

print(res.text)
print(res.status_code)
print(res.json())
print(str(res.json()))
assert pattern.match(str(res.json()))

4、使用文件的的唯一数字参数使用户唯一
#encoding=utf-8
import requests
import json,random
import re,string

with open("e:\\python\\c.txt","r+") as file_obj:
    username = file_obj.read().strip()
    print(username)
    file_obj.seek(0,0)
    file_obj.write(str(int(username) + 1))

username = "hhq" + username

pattern = re.compile(r"{'code': '00', 'userid': \d+}")
data = {'username': username, 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}

data = json.dumps(data)

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

print(res.text)
print(res.status_code)
print(res.json())
print(str(res.json()))
assert pattern.match(str(res.json()))

5、封装常用函数
#encoding=utf-8
import requests
import json,random
import re,string

def post_request(url,data):
    res =  requests.post(url,data)
    return res

def get_response(res):
    print(res.text)
    print(res.status_code)
    print(res.json())
    print(str(res.json()))

def assert_response(res):
    pattern = re.compile(r"{'code': '00', 'userid': \d+}")

    assert pattern.match(str(res.json()))

with open("e:\\python\\c.txt","r+") as file_obj:
    username = file_obj.read().strip()
    print(username)
    file_obj.seek(0,0)
    file_obj.write(str(int(username) + 1))

username = "hhq" + username

url = 'http://39.106.41.11:8080/register/'
data = {'username': username, 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}
data = json.dumps(data)

res = post_request(url,data)

get_response(res)

assert_response(res)

6、配置数据和程序的分离
Conf.py

url = 'http://39.106.41.11'
port = 8080
path = "register"

request_url = url + ":" + str(port) + "/" + path + "/"

#encoding=utf-8
import requests
import json,random
import re,string
from conf import *

def post_request(url,data):
    res =  requests.post(url,data)
    return res

def get_response(res):
    print(res.text)
    print(res.status_code)
    print(res.json())
    print(str(res.json()))

def assert_response(res):
    pattern = re.compile(r"{'code': '00', 'userid': \d+}")

    assert pattern.match(str(res.json()))

with open("e:\\python\\c.txt","r+") as file_obj:
    username = file_obj.read().strip()
    print(username)
    file_obj.seek(0,0)
    file_obj.write(str(int(username) + 1))

username = "hhq" + username

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

res = post_request(request_url,data)

get_response(res)

assert_response(res)

7、配置数据、测试数据和程序的分离
Conf.py

url = 'http://39.106.41.11'
port = 8080
path = "register"

request_url = url + ":" + str(port) + "/" + path + "/"

Data.txt

{'username': username, 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}|r"{'code': '00', 'userid': \d+}"

#encoding=utf-8
import requests
import json,random
import re,string
from conf import *

def post_request(url,data):
    res =  requests.post(url,data)
    return res

def get_response(res):
    print(res.text)
    print(res.status_code)
    print(res.json())
    print(str(res.json()))

def assert_response(res,reg_pattern):
    pattern = re.compile(reg_pattern)
    assert pattern.match(str(res.json()))

with open("e:\\python\\c.txt","r+") as file_obj:
    uniquenumber= file_obj.read().strip()
    print(username)
    file_obj.seek(0,0)
    file_obj.write(str(int(username) + 1))

username = "hhq" + uniquenumber

with open("e:\\python\\data.txt","r") as fp:
     line = fp.readline().strip()
     #数据从文件读出全部是字符串,需要获取原类型
     data = eval(line.split("|")[0])
     #这里是把{'username': username, 'password': 'wulaoshi12345', 、
     #'email': 'wulaoshi@qq.com'}赋值给data,以上的username会自动传入
     reg_pattern = eval(line.split("|")[1])

data = json.dumps(data)
print(data)

res = post_request(request_url,data)

get_response(res)

assert_response(res,reg_pattern)

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页名称:接口测试的几种组织形式-创新互联
文章路径:http://njwzjz.com/article/ccdopo.html