本文摘自php中文网,作者不言,侵删。
这篇文章主要为大家详细介绍了python如何使用unittest测试接口,具有一定的参考价值,感兴趣的小伙伴们可以参考一下本文实例为大家分享了python使用unittest 测试接口的具体代码,供大家参考,具体内容如下
1.首先使用 python 的requests 对接口进行测试
1 2 3 4 5 6 7 8 9 10 | 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 | import unittest
import TestInface
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
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 | import unittest
import TestCase
def test_InterFace():
suite = unittest.TestSuite()
suite.addTest(TestCase( "test_Case1" ))
runner = unittest.TextTestRunner()
runner.run(suite)
if __name__ = = '__main__' :
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 | import time
import TestSuite
import send_email
import TestCase
class Test_Calss():
def census( self ):
text = ''
start_time = time.time()
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()
|
相关推荐:
Python中unittest用法实例
Python单元测试框架unittest简明使用实例
Python中unittest模块做UT(单元测试)使用实例
以上就是python如何使用unittest测试接口_python的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
为什么人工智能要学Python
Python归一化多维数组的方法
常用Python解释器有哪些
Python中subprocess库的用法介绍
Python实现校园网自动登录
Python中常见数据库有哪些
Python numpy中nonzero()应该如何使用
Python中if语句用法
Python编程中notimplementederror的使用方法_Python
什么是Python import语句?在Python中的import语句作用有哪些?
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python如何使用unittest测试接口_python