码迷,mamicode.com
首页 > 编程语言 > 详细

python入门(二十一):基于文件的接口自动化框架

时间:2019-07-10 11:08:29      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:map   time   提取   不同   word   用户名   global   令牌   随机   

1、接口测试过程中,最耗费时间的是什么?

组织测试用例、如果入参数量多的话,整理入参比较耗时、对接口文档、参数耗时、分析log定位问题、需要加解密参数、返回字段的验证、很多接口返回参数作为其他接口入参(关联)

 

2、安装pip

进入到D:\Python36\Scripts目录下,执行easy_install pip命令安装pip

安装成功后,输入pip,显示使用命令则为安装成功。

  

3、安装requests包

pip install requests安装最新版本的requests包

 

4、请求注册的接口:e1_send.py

#encoding=utf-8

import requests

import json

import os

import hashlib # hashlib模块提供了多个不同的安全哈希算法的通用接口,可实现加密解密

 

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

data = json.dumps({‘username‘: ‘wulaoshi‘, ‘password‘: ‘wulaoshi12345‘, ‘email‘: ‘wulaoshi@qq.com‘})                  #将字典类型的参数转换为json串

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

#接口文档中定义了http使用方法是post,只有使用post才能提交请求

#post方法需要两个参数,一个是发post请求的地址,即接口的提交地址

#‘http://39.106.41.11:8080/register/‘,是接口的提交地址,http://是协议,39.106.41.11是ip,

#8080是端口

#data= data,post要提交的数据放在这里面

print (r.status_code)          #打印一下http协议返回的状态码,只要请求成功,都是200

print (r.text)                 #响应体的body,text表示使用的是unicode编译法,返回的是一个 unicode 型的文本数据

print (type(r.json()))          #将返回的内容,转换为字典对象

print (str(r.json()))            #最后转换为字符串

 

#register------

#200

#{"username": "wulaoshi", "code": "01"}  #”code”:”01”,接口文档中说明,代表用户已存在

#<class ‘dict‘>

#{‘username‘: ‘wulaoshi‘, ‘code‘: ‘01‘}

 

更新一下username至”wulaoshi11”,后的运行结果是:

#register------

#200

#{"code": "00", "userid": 773}   #用户注册成功,当前用户在数据库中的id是773

#<class ‘dict‘>

#{‘code‘: ‘00‘, ‘userid‘: 773}

 

 

接口返回code说明:

‘00‘ : 成功

‘01‘:用户已存在

‘02‘:参数不合法

‘03‘:参数错误(1、用户信息错误 2、参数错误,数据库中不存在相应数据)

‘999‘:未知错误,看后台日志

 

postman工具,也可以执行。

 

5、登陆login

#encoding=utf-8

import requests

import json

import os

import hashlib

 

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

data = json.dumps({‘username‘: ‘wulaoshi111‘, ‘password‘: ‘wulaoshi123456‘, ‘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()))

 

m5 = hashlib.md5()  #创建对象

m5.update(‘wulaoshi123456‘.encode("utf-8"))  #将密码进行md5加密,添加对象时需要指定编码

pwd = m5.hexdigest()  #以十六进制格式显示加密信息(常用)

print (pwd)

 

print ("login------")

data = json.dumps({‘username‘: ‘wulaoshi111‘, ‘password‘: pwd}) #密码是经过加密的密码

r = requests.post(‘http://39.106.41.11:8080/login/‘, data = data)

print (r.status_code)

print (r.text)

print (type(r.json()))

print (str(r.json()))

#运行结果:

#register------

#200

#{"code": "00", "userid": 10}

#<class ‘dict‘>

#{‘code‘: ‘00‘, ‘userid‘: 10}

#e5c7fda86b0b918c683ff87bf4813ebc    #经过md5加密的密码

#login------

#200

#{"token": "6ed1c6292002b9dc927cfaad79783294", "code": "00", "userid": 10, "login_time": #"2019-06-11 20:03:26"}                #获取到的token值,说明注册成功

#<class ‘dict‘>

#{‘token‘: ‘6ed1c6292002b9dc927cfaad79783294‘, ‘code‘: ‘00‘, ‘userid‘: 10, ‘login_time‘: #‘2019-06-11 20:03:26‘}

 

#token:在计算机身份认证中是令牌(临时)的意思,在词法分析中是标记的意思。一般作为邀请、登录系统使用。

 

以上两个例子的用户名必须要一样,且用户名必须使用一次,第二次的在注册时,会提示code:01,用户名已存在

{"username": "liuyujing1", "code": "01"}

 

6、减少用户名更改的次数(使用变量)

ps: 上面的例子,注册与登陆使用的用户名是一致的,且都是固定的字符串,如果用户名发生变化,需要同时更改注册和登陆两处的用户名。

import requests

import json

import os

import hashlib

 

username = "sunshine2"                 #将用户名存到username的变量中

 

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

data = json.dumps({‘username‘: username, ‘password‘: ‘sunshine123‘, ‘email‘: ‘1915212539@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()))

 

 

import requests

import json

import os

import hashlib

 

m5 = hashlib.md5()

m5.update(‘sunshine123‘.encode("utf-8"))

pwd = m5.hexdigest()

print (pwd)

 

print ("login------")

data = json.dumps({‘username‘: username, ‘password‘: pwd}) #登陆时,也使用变量

r = requests.post(‘http://39.106.41.11:8080/login/‘, data = data)

print (r.status_code)

print (r.text)

print (type(r.json()))

print (str(r.json()))

运行结果:

#register------                     #依然可以成功运行,且减少了更改用户名的地方

#200

#{"code": "00", "userid": 12}

#<class ‘dict‘>

#{‘code‘: ‘00‘, ‘userid‘: 12}

#e5c7fda86b0b918c683ff87bf4813ebc

#login------

#200

#{"token": "863e15be7f53e11711b55dab0d2c4d7e", "code": "00", "userid": 12, "login_time": "2019-06-11 20:21:23"}            #token登陆也是没有问题

#<class ‘dict‘>

#{‘token‘: ‘863e15be7f53e11711b55dab0d2c4d7e‘, ‘code‘: ‘00‘, ‘userid‘: 12, ‘login_time‘: ‘2019-06-11 20:21:23‘}

 

7、随机生成用户名,解决程序运行第二次时,提示用户名已存在

import requests

import json

import os

import hashlib

import random

 

username = "sunshine" + str(random.randint(1,99999))    #随机生成用户名

 

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

data = json.dumps({‘username‘: username, ‘password‘: ‘liuyujing123‘, ‘email‘: ‘1915212539@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()))

 

m5 = hashlib.md5()

m5.update(‘sunshine123‘.encode("utf-8"))

pwd = m5.hexdigest()

print (pwd)

 

print ("login------")

data = json.dumps({‘username‘: username, ‘password‘: pwd})

r = requests.post(‘http://39.106.41.11:8080/login/‘, data = data)

print (r.status_code)

print (r.text)

print (type(r.json()))

print (str(r.json()))

print (username)

 

8、使用assert断言

import requests

import json

import os

import hashlib

import random

 

username = "sunshine" + str(random.randint(1,99999))

 

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

data = json.dumps({‘username‘: username, ‘password‘: ‘sunshine123‘, ‘email‘: ‘1915212539@qq.com‘})

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

print (r.text)

assert ‘"code": "00"‘ in r.text   #断言在r.text中存在"code": "00","code": "00"外需加’ ’

 

m5 = hashlib.md5()

m5.update(‘sunshine123‘.encode("utf-8"))

pwd = m5.hexdigest()

 

print ("login------")

data = json.dumps({‘username‘: username, ‘password‘: pwd})

r = requests.post(‘http://39.106.41.11:8080/login/‘, data = data)

print (r.text)

assert ‘"token":‘ in r.text        #断言在r.text中存在"token":,"token":外需加’ ’

 

 

9、注册唯一的用户名

import requests

import json

import os

import hashlib

import random

 

with open("uniquenumber.txt","r+") as fp:  #使用gbk编码的方式打开文件,省略编码方式

    uniquenumber = int(fp.read().strip())    #去掉文件中的空格

    fp.seek(0,0)                           #游标回到开始的地方

    fp.write(str(uniquenumber+1))          #将以前读到的数字加1

 

username = "sunshine0612" + str(uniquenumber)  #生成唯一的注册用户名

print(username)

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

data = json.dumps({‘username‘: username, ‘password‘: ‘sunshine123‘, ‘email‘: ‘1915212539@qq.com‘})

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

print (r.text)

assert ‘"code": "00"‘ in r.text

 

m5 = hashlib.md5()

m5.update(‘sunshine123‘.encode("utf-8"))

pwd = m5.hexdigest()

 

print ("login------")

data = json.dumps({‘username‘: username, ‘password‘: pwd})

r = requests.post(‘http://39.106.41.11:8080/login/‘, data = data)

print (r.text)

assert ‘"token":‘ in r.text

运行结果:

#lsunshine06126

#register------

#{"code": "00", "userid": 426}

#login------

#{"token": "d191a34781d6d6dc5d83f80bb6c54a7a", "code": "00", "userid": 426, #"login_time": "2019-06-12 22:09:33"}

 

10、断言失败的情况下,继续执行

import requests

import json

import os

import hashlib

import random

 

with open("uniquenumber.txt","r+") as fp:

    uniquenumber = int(fp.read().strip())

    fp.seek(0,0)

    fp.write(str(uniquenumber+1))

 

username = "sunshine0612" + str(uniquenumber)

print(username)

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

data = json.dumps({‘username‘: username, ‘password‘: ‘sunshine123‘, ‘email‘: ‘1915212539@qq.com‘})

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

print (r.text)

 

try:

    assert ‘"code": "01"‘ in r.text            #当存在"code": "01"时,正常运行

except AssertionError as e:                 #当不存在"code": "01"时,提示断言失败

    print("断言失败")

 

m5 = hashlib.md5()

m5.update(‘sunshine123‘.encode("utf-8"))

pwd = m5.hexdigest()

 

print ("login------")

data = json.dumps({‘username‘: username, ‘password‘: pwd})

r = requests.post(‘http://39.106.41.11:8080/login/‘, data = data)

print (r.text)

assert ‘"token":‘ in r.text

#运行结果:

#sunshine06127

#register------

#{"code": "00", "userid": 427}

#断言失败

#login------

#{"token": "644820468f35f2a31daf3177dda9046f", "code": "00", "userid": 427, "login_time": #"2019-06-12 22:27:00"}

 

 

#sunshine06127

#register------

#{"username": "sunshine06127", "code": "01"}   #存在"code": "01",正常执行程序

#login------

#{"token": "644820468f35f2a31daf3177dda9046f", "code": "00", "userid": 427, "login_time": #"2019-06-12 22:34:36"}

 

11、生成简单测试报告

import requests

import json

import os

import hashlib

import random

 

testcase_count = 0

success_testcase_count = 0

fail_testcase_count = 0

 

with open("uniquenumber.txt","r+") as fp:

    uniquenumber = int(fp.read().strip())

    fp.seek(0,0)

    fp.write(str(uniquenumber+1))

 

username = "sunshine0612" + str(uniquenumber)

print(username)

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

data = json.dumps({‘username‘: username, ‘password‘: ‘sunshine123‘, ‘email‘: ‘1915212539@qq.com‘})

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

testcase_count += 1

print (r.text)

 

try:

    assert ‘"code": "01"‘ in r.text

    success_testcase_count += 1

except AssertionError as e:

    print("断言失败")

    fail_testcase_count += 1

except Exception as e:

    print("未知错误")

    fail_testcase_count += 1

 

m5 = hashlib.md5()

m5.update(‘sunshine123‘.encode("utf-8"))

pwd = m5.hexdigest()

 

print ("login------")

data = json.dumps({‘username‘: username, ‘password‘: pwd})

r = requests.post(‘http://39.106.41.11:8080/login/‘, data = data)

testcase_count += 1

print (r.text)

try:

    assert ‘"token":‘ in r.text

    success_testcase_count += 1

except AssertionError as e:

    print("断言失败")

    fail_testcase_count += 1

except Exception as e:

    print("未知错误")

    fail_testcase_count += 1

 

print("测试报告-------")

print("一共执行了%s个测试用例"%testcase_count)

print("成功执行了%s个测试用例"%success_testcase_count)

print("成功执行了%s个测试用例"%fail_testcase_count)

运行结果:

#sunshine06129

#register------

#{"code": "00", "userid": 437}

#断言失败

#login------

#{"token": "f9571721f7042edd2ed4dbbf94b6ab8b", "code": "00", "userid": 437, #"login_time": "2019-06-12 22:51:48"}

#测试报告-------

#一共执行了2个测试用例

#成功执行了1个测试用例

#成功执行了1个测试用例

 

12、做封装

import requests

import json

import os

import hashlib

import random

 

testcase_count = 0

success_testcase_count = 0

fail_testcase_count = 0

 

def get_uniquenumber(data_file):   #做封装的公共函数可以提取出去,可以不放在测试

    with open(data_file,"r+") as fp:  #文件的主框架里面

        uniquenumber = int(fp.read().strip())

        fp.seek(0,0)

        fp.write(str(uniquenumber+1))

        return str(uniquenumber)

 

def md5(data):                 #做封装的公共函数可以提取出去,可以不放在测试

    m5 = hashlib.md5()         #文件的主框架里面

    m5.update(data.encode("utf-8"))

    pwd = m5.hexdigest()

    return pwd

 

def send_request(url,data):    #做封装的公共函数可以提取出去,可以不放在测试

    if isinstance(data,dict):    #文件的主框架里面

        data = json.dumps(data)

    r = requests.post(url,data = data)

    return(r)

 

def assert_result(r,key_word):

    global success_testcase_count  #设置全局变量

    global fail_testcase_count      #设置全局变量

    try:

        assert key_word in r

        success_testcase_count += 1

    except AssertionError as e:

        print("断言失败")

        fail_testcase_count += 1

    except Exception as e:

        print("未知错误")

        fail_testcase_count += 1

 

username = "sunshine0612" + get_uniquenumber("e:\\uniquenumber.txt")

print(username)

 

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

url = ‘http://39.106.41.11:8080/register/‘

data = {‘username‘: username, ‘password‘: "sunshine123", ‘email‘: ‘1915212539@qq.com‘}

r = send_request(url,data)

testcase_count += 1

print(r.text)

print(assert_result(r.text,‘"code": 01‘))

 

print ("login------")

url = ‘http://39.106.41.11:8080/login/‘

data ={‘username‘: username, ‘password‘: md5("sunshine123")}

r = send_request(url,data)

testcase_count += 1

print(r.text)

print(assert_result(r.text,‘"token"‘))

 

print("测试报告-------")

print("一共执行了%s个测试用例"%testcase_count)

print("成功执行了%s个测试用例"%success_testcase_count)

print("成功执行了%s个测试用例"%fail_testcase_count)

#运行结果:

#sunshine061224

#register------

#{"code": "00", "userid": 2448}

#断言失败

#None

#login------

#{"token": "13db11b40f19345ffe652507d6f36f4d", "code": "00", "userid": 2448, #"login_time": "2019-06-16 14:35:32"}

#None

#测试报告-------

#一共执行了2个测试用例

#成功执行了1个测试用例

#成功执行了1个测试用例

 

 

13、ip和端口可用变量来维护

import requests

import json

import os

import hashlib

import random

 

testcase_count = 0

success_testcase_count = 0

fail_testcase_count = 0

ip = "39.106.41.11"                  #ip和端口提取为变量

port = "8080"

 

def get_uniquenumber(data_file):

    with open(data_file,"r+") as fp:

        uniquenumber = int(fp.read().strip())

        fp.seek(0,0)

        fp.write(str(uniquenumber+1))

        return str(uniquenumber)

 

def md5(data):

    m5 = hashlib.md5()

    m5.update(data.encode("utf-8"))

    pwd = m5.hexdigest()

    return pwd

 

def send_request(url,data):

    if isinstance(data,dict):

        data = json.dumps(data)

    r = requests.post(url,data = data)

    return(r)

 

def assert_result(r,key_word):

    global success_testcase_count

    global fail_testcase_count

    try:

        assert key_word in r

        success_testcase_count += 1

    except AssertionError as e:

        print("断言失败")

        fail_testcase_count += 1

    except Exception as e:

        print("未知错误")

        fail_testcase_count += 1

 

username = "sunshine0612" + get_uniquenumber("e:\\uniquenumber.txt")

print(username)

 

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

url = ‘http://%s:%s/register/‘%(ip,port)    #模板字符串

data = {‘username‘: username, ‘password‘: "sunshine123", ‘email‘: ‘1915212539@qq.com‘}

r = send_request(url,data)

testcase_count += 1

print(r.text)

print(assert_result(r.text,‘"code": 01‘))

 

print ("login------")

url = ‘http://%s:%s/login/‘%(ip,port)     #模板字符串

data ={‘username‘: username, ‘password‘: md5("sunshine123")}

r = send_request(url,data)

testcase_count += 1

print(r.text)

print(assert_result(r.text,‘"token"‘))

 

print("测试报告-------")

print("一共执行了%s个测试用例"%testcase_count)

print("成功执行了%s个测试用例"%success_testcase_count)

print("成功执行了%s个测试用例"%fail_testcase_count)

 

14、配置和程序的分离

server_info.py:

ip = "39.106.41.11"

port = "8080"

 

e1_send.py:

import requests

import json

import os

import hashlib

import random

from server_info import ip,port    #引入ip和端口变量,使主程序不用更改配置

 

testcase_count = 0

success_testcase_count = 0

fail_testcase_count = 0

 

def get_uniquenumber(data_file):

    with open(data_file,"r+") as fp:

        uniquenumber = int(fp.read().strip())

        fp.seek(0,0)

        fp.write(str(uniquenumber+1))

        return str(uniquenumber)

 

def md5(data):

    m5 = hashlib.md5()

    m5.update(data.encode("utf-8"))

    pwd = m5.hexdigest()

    return pwd

 

def send_request(url,data):

    if isinstance(data,dict):

        data = json.dumps(data)

    r = requests.post(url,data = data)

    return(r)

 

def assert_result(r,key_word):

    global success_testcase_count

    global fail_testcase_count

    try:

        assert key_word in r

        success_testcase_count += 1

    except AssertionError as e:

        print("断言失败")

        fail_testcase_count += 1

    except Exception as e:

        print("未知错误")

        fail_testcase_count += 1

 

username = "sunshine0612" + get_uniquenumber("e:\\uniquenumber.txt")

print(username)

 

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

url = ‘http://%s:%s/register/‘%(ip,port)

data = {‘username‘: username, ‘password‘: "sunshine123", ‘email‘: ‘1915212539@qq.com‘}

r = send_request(url,data)

testcase_count += 1

print(r.text)

print(assert_result(r.text,‘"code": 01‘))

 

print ("login------")

url = ‘http://%s:%s/login/‘%(ip,port)

data ={‘username‘: username, ‘password‘: md5("sunshine123")}

r = send_request(url,data)

testcase_count += 1

print(r.text)

print(assert_result(r.text,‘"token"‘))

 

print("测试报告-------")

print("一共执行了%s个测试用例"%testcase_count)

print("成功执行了%s个测试用例"%success_testcase_count)

print("成功执行了%s个测试用例"%fail_testcase_count

 

 

15、测试数据与程序的分离

1) test_data.txt

register||{‘username‘: username, ‘password‘: "sunshine123", ‘email‘: ‘1915212539@qq.com‘}

login||{‘username‘: username, ‘password‘: md5("sunshine123")}

#该文件中的username,在test_data文件中并不是字符串,需要变成变量

#处理方法:${username}

register||{‘username‘: ${username}, ‘password‘: "sunshine123", ‘email‘: ‘1915212539@qq.com‘}

login||{‘username‘: ${username}, ‘password‘: md5("sunshine123")}

 

 

 

with open("test_data.txt") as fp:     #test_data.txt与e1_send.py,在同一目录下,所以不

     test_cases = fp.readlines()    #用绝对路径,且是gbk编码,所以不用写编码方式

 

print(test_cases)

运行结果:

[‘register||{\‘username\‘: ${username}, \‘password\‘: "sunshine123", \‘email\‘: \‘1915212539@qq.com\‘}\n‘, ‘login||{\‘username\‘: ${username}, \‘password\‘: md5("sunshine123")}‘]               #运行结果中有换行符

 

with open("test_data.txt") as fp:

     test_cases = fp.readlines()

test_cases = list(map(lambda x:x.strip(),test_cases))

print(test_cases)

#运行结果:

[‘register||{\‘username\‘: ${username}, \‘password\‘: "sunshine123", \‘email\‘: \‘1915212539@qq.com\‘}‘, ‘login||{\‘username\‘: ${username}, \‘password\‘: md5("sunshine123")}‘]               #去掉回车

#但是上面的结果中有\

#解决方法:

#将test_data.txt文件中的变量都以”${}”的形式写

#更改后的test_data.txt文件

register||{"username": "${username}", "password": "sunshine123", "email": "1915212539@qq.com"}

login||{"username": "${username}", "password": "${md5(sunshine123)}"

运行下面代码:

with open("test_data.txt") as fp:

     test_cases = fp.readlines()

test_cases = list(map(lambda x:x.strip(),test_cases))

print(test_cases)

结果:

[‘register||{"username": "${username}", "password": "sunshine123", "email": "1915212539@qq.com"}‘, ‘login||{"username": "${username}", "password": "${md5(sunshine123)}"‘]              #结果正确

 

 

尝试引入register具体的url

with open("test_data.txt") as fp:

     test_cases = fp.readlines()

 

test_cases = list(map(lambda x:x.strip(),test_cases))

 

print(test_cases[0])

url,data = test_cases[0].split("||")

print(url)

运行结果:

register||{"username": "${username}", "password": "sunshine123", "email": "1915212539@qq.com"}

register               #只是打印了register,但是并没有打印具体的url

 

test_data.txt:

register||{"username": "${username}", "password": "sunshine123", "email": "1915212539@qq.com"}

login||{"username": "${username}", "password": "${md5(sunshine123)}"

 

 

server_info.py

ip = "39.106.41.11"

port = "8080"

register= ‘http://%s:%s/register/‘%(ip,port)

login = ‘http://%s:%s/login/‘%(ip,port)

 

 

e1_send.py:

import requests

import json

import os

import hashlib

import random

from server_info import *

 

with open("test_data.txt") as fp:

     test_cases = fp.readlines()

test_cases = list(map(lambda x:x.strip(),test_cases))

print(test_cases[0])

api_name,data = test_cases[0].split("||")

print(api_name)

exec("url="+api_name )          #接口名字和请求地址的映射

print(url)

运行结果:

register||{"username": "${username}", "password": "sunshine123", "email": "1915212539@qq.com"}           #但是运行结果中的username并没有成功引入

register

http://39.106.41.11:8080/register/    #引入register具体的url成功

 

 

引入username变量,进行请求,用到正则。

server_info.py:

ip = "39.106.41.11"

port = "8080"

register = ‘http://%s:%s/register/‘%(ip,port)

login = ‘http://%s:%s/login/‘%(ip,port)

 

test_data.txt

register||{"username": "${username}", "password": "sunshine123", "email": "1915212539@qq.com"}

login||{"username": "${username}", "password": "${md5(sunshine123)}"

 

e1_send.py:

import requests

import json

import os

import hashlib

import random

from server_info import *

import re

 

with open("test_data.txt") as fp:

     test_cases = fp.readlines()

 

test_cases = list(map(lambda x:x.strip(),test_cases))

 

print(test_cases[0])

api_name,data = test_cases[0].split("||")

print(api_name)

exec("url="+api_name )

print(url)

 

testcase_count = 0

success_testcase_count = 0

fail_testcase_count = 0

 

def get_uniquenumber(data_file):

    with open(data_file,"r+") as fp:

        uniquenumber = int(fp.read().strip())

        fp.seek(0,0)

        fp.write(str(uniquenumber+1))

        return str(uniquenumber)

 

def md5(data):

    m5 = hashlib.md5()

    m5.update(data.encode("utf-8"))

    pwd = m5.hexdigest()

    return pwd

 

def send_request(url,data):

    if isinstance(data,dict):

        data = json.dumps(data)

    r = requests.post(url,data = data)

    return(r)

 

def assert_result(r,key_word):

    global success_testcase_count

    global fail_testcase_count

    try:

        assert key_word in r

        success_testcase_count += 1

    except AssertionError as e:

        print("断言失败")

        fail_testcase_count += 1

    except Exception as e:

        print("未知错误")

        fail_testcase_count += 1

 

 

username = "sunshine0612" + get_uniquenumber("e:\\uniquenumber.txt")

print(data)

if re.search(r"\$\{\w+\}",data):

    var_name = re.search(r"\$\{(\w+)\}",data).group(1)

    data = re.sub(r"\$\{(\w+)\}",eval(var_name),data)

print(data)

r = send_request(url,data)

print(r.text)

运行结果:

register||{"username": "${username}", "password": "sunshine123", "email": "1915212539@qq.com"}

register

http://39.106.41.11:8080/register/

{"username": "${username}", "password": "sunshine123", "email": "1915212539@qq.com"}

{"username": "liuyujing061232", "password": "sunshine123", "email": "1915212539@qq.com"}

{"code": "00", "userid": 2456}

 

16、eval和exec的区别

>>> exec("a=1")   #exec()执行程序

>>> a

1

>>> eval("a")      #eval(str),从一个表达式获取一个返回值

1

 

17、接口测试框架思想:

配置和程序分离

公共函数和主程序分离

测试数据和程序的分离

有测试报告

有日志

python入门(二十一):基于文件的接口自动化框架

标签:map   time   提取   不同   word   用户名   global   令牌   随机   

原文地址:https://www.cnblogs.com/suitcases/p/11152625.html

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