一、发送请求 注册
e1_send.py
#encoding=utf-8 import requests import json import os import hashlib print ("register------") data = json.dumps({'username': 'wulaoshis1121', 'password': 'wulaoshi123452','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()))
C:\Users\dell>py -3 C:\Users\dell\Desktop\练习\5\0530.py
register------
200
{"code": "00", "userid": 2660}
<class 'dict'>
{'code': '00', 'userid': 2660}
二、登录
#encoding=utf-8 import requests import json import os import hashlib print ("register------") data = json.dumps({'username': 'testmanwu12', 'password': 'wulaoshi1234521','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('wulaoshi1234521'.encode("utf-8")) pwd = m5.hexdigest() print (pwd) print ("login------") data = json.dumps({'username': 'testmanwu12', '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()))
C:\Users\dell>py -3 C:\Users\dell\Desktop\练习\5\0530.py
register------
200
{"code": "00", "userid": 2661}
<class 'dict'>
{'code': '00', 'userid': 2661}
a9a5f12ba3bf64941546e7974f21c4e3
login------
200
{"token": "c07b859d4f3e9bd73bf984660b8efa3d", "code": "00", "userid": 2661, "login_time": "2019-05-30 20:36:12"}
<class 'dict'>
{'token': 'c07b859d4f3e9bd73bf984660b8efa3d', 'code': '00', 'userid': 2661, 'login_time': '2019-05-30 20:36:12'}
三 使用变量 :由于注册和登录接口会多次使用username,把username提取出来放到变量里,这样每次都只需要修改一个地方即可。
e2_use_var.py
#encoding=utf-8 import requests import json import os import hashlib username = "testmanwu112" print ("register------") data = json.dumps({'username': username, 'password': 'wulaoshi1234521','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('wulaoshi1234521'.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()))
C:\Users\dell>py -3 C:\Users\dell\Desktop\练习\5\0530.py
register------
200
{"code": "00", "userid": 2662}
<class 'dict'>
{'code': '00', 'userid': 2662}
a9a5f12ba3bf64941546e7974f21c4e3
login------
200
{"token": "b996dbae1785cdc70d3b87a1721c2c08", "code": "00", "userid": 2662, "login_time": "2019-05-30 20:42:13"}
<class 'dict'>
{'token': 'b996dbae1785cdc70d3b87a1721c2c08', 'code': '00', 'userid': 2662, 'login_time': '2019-05-30 20:42:13'}
四 由于注册用户名不能重复,每次执行接口用例都要手动改数据很麻烦
用random生成随机数解决(也有可能会生成重复的数)
#encoding=utf-8 import requests import json import os import hashlib import random username = "testmanwu"+str(random.randint(1,99999999)) print ("register------") data = json.dumps({'username': username, 'password': 'wulaoshi1234521','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('wulaoshi1234521'.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()))
五 另一种方式创建唯一用户名
#写一个uniquenumber.txt文件,存任意数字,每次读取,后+1保证每次取到的用户名绝对不会重复
C:\\Users\\dell\\Desktop\\练习\\5\\uniquenumber.txt
1 |
e4_uniquenumber.py
#encoding=utf-8 import requests import json import os import hashlib import random with open("C:\\Users\\dell\\Desktop\\练习\\5\\uniquenumber.txt","r+") as fp: uniquenumber = int(fp.read().strip()) #去掉前后的空格 fp.seek(0,0) #游标回到最开始的位置 fp.write(str(uniquenumber+1)) #每次+1写入 username = "testmanxx"+str(uniquenumber) print(username) print ("register------") data = json.dumps({'username': username, 'password': 'wulaoshi1234521','email': 'wulaoshi@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('wulaoshi1234521'.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
六 断言
e3_assert.py
#encoding=utf-8
import requests
import json
import os
import hashlib
import random
username = "testmanwu"+str(random.randint(1,99999999))
print ("register------")
data = json.dumps({'username': username, 'password': 'wulaoshi1234521','email': 'wulaoshi@qq.com'}) #
r = requests.post('http://39.106.41.11:8080/register/', data= data)
print (r.text)
assert '"code": "01"' in r.text
m5 = hashlib.md5()
m5.update('wulaoshi1234521'.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
C:\Users\dell>py -3 C:\Users\dell\Desktop\练习\5\0530.py
register------
{"code": "00", "userid": 2666}
Traceback (most recent call last):
File "C:\Users\dell\Desktop\练习\5\0530.py", line 15, in <module>
assert '"code": "01"' in r.text
AssertionError
七 如果断言失败时,想要不影响后面的用例执行,用try...except...
#encoding=utf-8 import requests import json import os import hashlib import random with open("C:\\Users\\dell\\Desktop\\练习\\5\\uniquenumber.txt","r+") as fp: uniquenumber = int(fp.read().strip()) fp.seek(0,0) fp.write(str(uniquenumber+1)) username = "testmanxx"+str(uniquenumber) print(username) print ("register------") data = json.dumps({'username': username, 'password': 'wulaoshi1234521','email': 'wulaoshi@qq.com'}) # r = requests.post('http://39.106.41.11:8080/register/', data= data) print (r.text) try: assert '"code": "xx"' in r.text except AssertionError as e: print("断言失败") m5 = hashlib.md5() m5.update('wulaoshi1234521'.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
C:\Users\dell>py -3 C:\Users\dell\Desktop\练习\5\0530.py
testmanxx8
register------
{"code": "00", "userid": 2672}
断言失败
login------
{"token": "1903299c6d10fe01b7e3ee5a2ea420d1", "code": "00", "userid": 2672, "login_time": "2019-05-30 21:12:08"}
八 统计用例执行数据,生成测试报告
e5_report.py
#encoding=utf-8 import requests import json import os import hashlib import random testcase_count =0 success_testcase_count = 0 fail__testcase_count = 0 with open("C:\\Users\\dell\\Desktop\\练习\\5\\uniquenumber.txt","r+") as fp: uniquenumber = int(fp.read().strip()) fp.seek(0,0) fp.write(str(uniquenumber+1)) username = "testmanxx"+str(uniquenumber) print(username) print ("register------") data = json.dumps({'username': username, 'password': 'wulaoshi1234521','email': 'wulaoshi@qq.com'}) # r = requests.post('http://39.106.41.11:8080/register/', data= data) testcase_count+=1 print (r.text) try: assert '"code": "xx"' 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('wulaoshi1234521'.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)
C:\Users\dell>py -3 C:\Users\dell\Desktop\练习\5\0530.py
testmanxx10
register------
{"code": "00", "userid": 2674}
断言失败
login------
{"token": "f96d3e43287c80cd35ef01c057e5e67a", "code": "00", "userid": 2674, "login_time": "2019-05-30 21:27:16"}
测试报告:
一共执行了2个用例
成功了1个用例
失败了1个用例
九 封装:
e6_encapsu.py
#encoding=utf-8 import requests import json import os import hashlib import random #获取唯一number def get_unique_number(data_file): with open(data_file,"r+") as fp: uniquenumber = int(fp.read().strip()) fp.seek(0,0) fp.write(str(uniquenumber+1)) return uniquenumber #密码加密 def md5(data): m5 = hashlib.md5() m5.update('wulaoshi1234521'.encode("utf-8")) md5_data = m5.hexdigest() return md5_data #发送请求 def send_request(url,data): if isinstance(data,dict): data = json.dumps(data) response = requests.post(url, data= data) return response #断言 def assert_result(response,key_word): global success_testcase_count global fail__testcase_count try: assert key_word 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 testcase_count =0 success_testcase_count = 0 fail__testcase_count = 0 username = "testmanwenx"+str(get_unique_number("C:\\Users\\dell\\Desktop\\练习\\5\\uniquenumber.txt")) print(username) print ("register------") url = 'http://39.106.41.11:8080/register/' data = {'username': username, 'password': 'wulaoshi1234521','email': 'wulaoshi@qq.com'} r = send_request(url,data) testcase_count+=1 print (r.text) assert_result(r,"userid") print ("login------") url = 'http://39.106.41.11:8080/login/' data = {'username': username, 'password': md5("wulaoshi1234521")} r = send_request(url,data) testcase_count+=1 print (r.text) assert_result(r,"token") print("测试报告:") print("一共执行了%s个用例" %testcase_count) print("成功了%s个用例" %success_testcase_count) print("失败了%s个用例" %fail__testcase_count)
十 如果接口地址变了,修改很麻烦,可以把地址分离到另一个文件,做到配置和程序的分离
创建一个C:\Users\dell\Desktop\练习\5\server_info.py文件
ip = "39.106.41.11"
port = "8080"
register = 'http://%s:%s/register/' %(ip,port)
login = 'http://%s:%s/login/' %(ip,port)
|
e6_encapsu.py
#encoding=utf-8 import requests import json import os import hashlib import random from server_info import ip,port def get_unique_number(data_file): with open(data_file,"r+") as fp: uniquenumber = int(fp.read().strip()) fp.seek(0,0) fp.write(str(uniquenumber+1)) return uniquenumber def md5(data): m5 = hashlib.md5() m5.update('wulaoshi1234521'.encode("utf-8")) md5_data = m5.hexdigest() return md5_data def send_request(url,data): if isinstance(data,dict): data = json.dumps(data) # response = requests.post(url, data= data) return response def assert_result(response,key_word): global success_testcase_count global fail__testcase_count try: assert key_word 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 testcase_count =0 success_testcase_count = 0 fail__testcase_count = 0 username = "testmanwenx"+str(get_unique_number("C:\\Users\\dell\\Desktop\\练习\\5\\uniquenumber.txt")) print(username) print ("register------") url = 'http://%s:%s/register/' %(ip,port) data = {'username': username, 'password': 'wulaoshi1234521','email': 'wulaoshi@qq.com'} r = send_request(url,data) testcase_count+=1 print (r.text) assert_result(r,"userid") print ("login------") url = 'http://%s:%s/login/' %(ip,port) data = {'username': username, 'password': md5("wulaoshi1234521")} r = send_request(url,data) testcase_count+=1 print (r.text) assert_result(r,"token") print("测试报告:") print("一共执行了%s个用例" %testcase_count) print("成功了%s个用例" %success_testcase_count) print("失败了%s个用例" %fail__testcase_count)
C:\Users\dell>py -3 C:\Users\dell\Desktop\练习\5\0530.py
testmanwenx20
register------
{"code": "00", "userid": 2679}
login------
{"token": "c3e3a8ba671fc2b4b8395ff16f1af448", "code": "00", "userid": 2679, "login_time": "2019-05-30 22:03:12"}
测试报告:
一共执行了2个用例
成功了2个用例
失败了0个用例
十一 测试数据分离 ,同时引用变量
创建一个C:\Users\dell\Desktop\练习\5\test_data.txt文件
注意:本次文件存的是ANSI模式,后面读取的时候也是用gbk模式,保持一致
register||{"username": "${username}", "password": "wulaoshi1234521","email": "wulaoshi@qq.com"}
login||{"username": "${username}", "password": "${md5('wulaoshi1234521'})"}
|
e8_data_driven.py
#encoding=utf-8 import requests import json import os import hashlib import random from server_info import * import re def get_unique_number(data_file): with open(data_file,"r+") as fp: uniquenumber = int(fp.read().strip()) fp.seek(0,0) fp.write(str(uniquenumber+1)) return uniquenumber def md5(data): m5 = hashlib.md5() m5.update('wulaoshi1234521'.encode("utf-8")) md5_data = m5.hexdigest() return md5_data def send_request(url,data): if isinstance(data,dict): data = json.dumps(data) # response = requests.post(url, data= data) return response def assert_result(response,key_word): global success_testcase_count global fail__testcase_count try: assert key_word 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 username = "testmanwenx"+str(get_unique_number(r"C:\Users\dell\Desktop\练习\5\uniquenumber.txt")) with open(r"C:\Users\dell\Desktop\练习\5\test_data.txt") as fp: #不指定encoding默认使用gbk读取 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("||") exec("url="+api_name) print(url) 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)
C:\Users\dell>py -3 C:\Users\dell\Desktop\练习\5\e8_data_driven.py
register||{"username": "${username}", "password": "wulaoshi1234521","email": "wulaoshi@qq.com"}
{"username": "${username}", "password": "wulaoshi1234521","email": "wulaoshi@qq.com"}
{"username": "testmanx32", "password": "wulaoshi1234521","email": "wulaoshi@qq.com"}