本文摘自PHP中文网,作者php中世界最好的语言,侵删。
这次给大家带来python使用unittest测试接口步奏详解,python使用unittest测试接口的注意事项有哪些,下面就是实战案例,一起来看一下。1.首先使用 python 的requests 对接口进行测试
1 2 3 4 5 6 7 8 9 10 | # TestInface.py
import requests,json
url = visit.get_test_url()
news_url = url+ 'news.info'
headers = baseToken.basetoken_datas()[ 'headers' ]
def new_data(data):
r = requests.post(news_url,data=data,headers=headers)
cnn = json.loads(r.text)
return cnn
|
2.使用unittest调用接口,且对接口测试的结果进行统计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # TestCase.py
# -*- coding:utf-8 -*-
import unittest
import TestInface
# 对执行的 case 结果进行统计
# ---------------------------------------------------------------------------------------------------------------------
text = ""
num_success = 0
num_fail = 0
# 测试通过
def decide_success(joggle):
global num_success
num_success += 1
print_out(joggle + ":接口测试通过\n" )
return num_success
# 测试不通过
def decide_fail(txt, joggle):
global num_fail
num_fail += 1
print_out(joggle + ":接口测试未通过 \n错误信息: " + txt + "\n" )
return num_fail
# 邮件内容写入 & 客户端输出
def print_out(message):
global text
text += "\n" + message
return text
# 返回值判断
def decide_result(result, code, joggle):
if result[ 'code' ] == code:
decide_success(joggle)
return "pass"
else :
txt = u "期望返回值:" + str(code) + u " 实际返回值:" + str(result) + '\n' + result[ 'message' ]
decide_fail(txt, joggle)
return "fail"
def decide_Count():
data = {
'num_success' : num_success,
'num_fail' : num_fail,
'text' : text
}
return data
# --------------------------------------------------------------------------------------------------------------------
# 定义 unittest
class MyTestCase(unittest.TestCase):
# 初始化工作
def setUp(self):
pass
# 退出清理工作
def tearDown(self):
pass
def test_Case1(self):
id = 16
data = { 'id' :id}
a = TestInface.new_data(data)
decide_result(a,0, 'test_Case1' )
|
3.使用suite对case进行管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # TestSuite.py
# -*- coding:utf-8 -*-
import unittest
import TestCase
def test_InterFace():
# 构造测试集
suite = unittest.TestSuite()
suite.addTest(TestCase( "test_Case1" )) # unittest中的测试用例
runner = unittest.TextTestRunner()
runner.run(suite)
# 对测试集进行测试需要返回值
# return suite
if name == 'main' :
# unittest.main(defaultTest= 'test_InterFace' )
# 执行测试
runner = unittest.TextTestRunner()
runner.run(test_InterFace())
|
4.对接口的数据进行统计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # TestCensus.py
# -*- coding:utf-8 -*-
import time
import TestSuite
import send_email
import TestCase
class Test_Calss():
def census(self):
text = ''
# 初始化测试起始时间
start_time = time.time()
# 调用suite测试集
TestSuite.test_InterFace()
# 结束执行时间计算
end_time = time.time()
result = TestCase.decide_Count()
# 接口测试统计说明
total_use_case = u "执行用例总数:" + str(result[ 'num_success' ] + result[ 'num_fail' ]) + \
u "\t通过数:" + str(result[ 'num_success' ]) + \
u "\t不通过数:" + str(result[ 'num_fail' ])
total_time = u "\t总共耗时:" + str( round ((end_time - start_time), 3)) + u '秒'
text = result[ 'text' ] + total_use_case + total_time
print (text)
# 发生测试报告邮件
send_email.email_file(text)
if name == 'main' :
Test_Calss().census()
|
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
在python中列表,数组,矩阵互相转换的方法
Python中怎样把矩阵转换为列表
以上就是python使用unittest测试接口步奏详解的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
python如何操作mysql
有钱 python,没钱 php,编程语言也嫌贫爱富
分享一个纯 python 实现的 mysql 客户端操作库
python使用Unittest测试接口步奏详解
range()是什么?为什么不生产迭代器?
什么是dbms接口?浅谈dbms接口
python怎么统计字母出现的次数
mysql在cmd和python下的常用操作解析
django数据库自动重连的方法教程
python之mysqldb模块在windows下安装方法
更多相关阅读请进入《Unittest》频道 >>
机械工业出版社
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
转载请注明出处:木庄网络博客 » python使用unittest测试接口步奏详解