python中unittest框架應(yīng)用詳解
1、Unittest為Python內(nèi)嵌的測試框架,不需要特殊配置
2、編寫規(guī)范
需要導(dǎo)入 import unittest
測試類必須繼承unittest.TestCase
測試方法以 test_開頭
模塊和類名沒有要求
TestCase 理解為寫測試用例
TestSuite 理解為測試用例的集合
TestLoader 理解為的測試用例加載
TestRunner 執(zhí)行測試用例,并輸出報(bào)告
import unittest
from class_api_login_topup.demo import http_request
from class_api_login_topup.http_attr import Get_Attr # 反射的值 獲取 cookies
# 這是文件http_attr中的Get_Attr類
class Get_Attr:
cookies = None
class Login_Http(unittest.TestCase):
def __init__(self, methodName, url, data, method, expected):
super(Login_Http, self).__init__(methodName) # 超繼承
self.url = url
self.data = data
self.expected = expected
self.method = method
def test_api(self): # 正常登錄
res = http_request().request(self.url, self.data, self.method, getattr(Get_Attr, 'cookies'))
if res.cookies:
setattr(Get_Attr, 'cookies', res.cookies)
try:
self.assertEqual(self.expected, res.json()['code'])
except AssertionError as e:
print("test_api's, error is {0}", format(e))
raise e
print(res.json())
if __name__ == '__main__':
unittest.main()
執(zhí)行一:
import unittest
from class_demo_login_topup.http_tools import Login_Http
suite = unittest.TestSuite()
loader = unittest.TestLoader()
test_data = [{'url': 'http://test.lemonban.com/futureloan/mvc/api/member/login',
'data': {'mobilephone': 'xxxx', 'pwd': '123456'}, 'expected': '10001', 'method': 'get'},
{'url': 'http://test.lemonban.com/futureloan/mvc/api/member/login',
'data': {'mobilephone': 'xxxx', 'pwd': '12345678'}, 'expected': '20111', 'method': 'get'},
{'url': 'http://test.lemonban.com/futureloan/mvc/api/member/recharge',
'data': {'mobilephone': 'xxxx', 'amount': '1000'}, 'expected': '10001', 'method': 'post'},
{'url': 'http://test.lemonban.com/futureloan/mvc/api/member/recharge',
'data': {'mobilephone': 'xxxx', 'amount': '-100'}, 'expected': '20117', 'method': 'post'}]
# 遍歷數(shù)據(jù),執(zhí)行腳本 addTest 單個(gè)執(zhí)行
for item in test_data:
suite.addTest(Login_Http('test_api', item['url'], item['data'], item['method'], item['expected']))
# 執(zhí)行
with open('http_TestCase.txt', 'w+', encoding='UTF-8') as file:
runner = unittest.TextTestRunner(stream=file, verbosity=2)
runner.run(suite)
# 運(yùn)行結(jié)果
{'status': 1, 'code': '10001', 'data': None, 'msg': '登錄成功'}
{'status': 0, 'code': '20111', 'data': None, 'msg': '用戶名或密碼錯(cuò)誤'}
{'status': 1, 'code': '10001', 'data': {'id': 10011655, 'regname': '小蜜蜂', 'pwd': 'E10ADC3949BA59ABBE56E057F20F883E', 'mobilephone': 'xxxx', 'leaveamount': '150000.00', 'type': '1', 'regtime': '2021-07-14 14:54:08.0'}, 'msg': '充值成功'}
{'status': 0, 'code': '20117', 'data': None, 'msg': '請(qǐng)輸入范圍在0到50萬之間的正數(shù)金額'}
執(zhí)行二:把test_data的數(shù)據(jù)放在EXCEL中運(yùn)行。
import unittest
from class_demo_login_topup.http_tools import Login_Http
suite = unittest.TestSuite()
loader = unittest.TestLoader()
test_data = HttpExcel('test_api.xlsx', 'python').real_excel()
for item in test_data:
suite.addTest(Login_Http('test_api', item['url'], eval(item['data']), item['method'], str(item['expected'])))
with open('http_TestCase.txt', 'w+', encoding='UTF-8') as file:
runner = unittest.TextTestRunner(stream=file, verbosity=2)
runner.run(suite)
執(zhí)行三、直接用裝飾器ddt
import unittest
from class_api_login_topup.demo import http_request
from class_api_login_topup.http_attr import Get_Attr # 反射的值
from ddt import ddt, data, unpack
from class_demo_login_topup.http_excel import HttpExcel
test_data = HttpExcel('test_api.xlsx', 'python').real_excel()
@ddt
class Login_Http(unittest.TestCase):
@data(*test_data)
def test_api(self, item): # 正常登錄
res = http_request().request(item['url'], eval(item['data']), item['method'], getattr(Get_Attr, 'cookies'))
if res.cookies:
setattr(Get_Attr, 'cookies', res.cookies)
try:
self.assertEqual(str(item['expected']), res.json()['code'])
except AssertionError as e:
print("test_api's, error is {0}", format(e))
raise e
print(res.json())
執(zhí)行ddt方式一
import unittest
from class_demo_login_topup.http_tools import Login_Http
from class_demo_login_topup.http_excel import HttpExcel
suite = unittest.TestSuite()
loader = unittest.TestLoader()
from class_demo_login_topup import http_tools_1
suite.addTest(loader.loadTestsFromModule(http_tools_1)) # 執(zhí)行整個(gè)文件
with open('http_TestCase.txt', 'w+', encoding='UTF-8') as file:
runner = unittest.TextTestRunner(stream=file, verbosity=2)
runner.run(suite)
執(zhí)行ddt方式二
import unittest
from class_demo_login_topup.http_tools import Login_Http # 不用ddt的方法
from class_demo_login_topup.http_excel import HttpExcel
suite = unittest.TestSuite()
loader = unittest.TestLoader()
from class_demo_login_topup.http_tools_1 import * # http_tools_1文件是用ddt的方法
suite.addTest(loader.loadTestsFromTestCase(Login_Http)) # 執(zhí)行http_tools_1 文件下的Login_Http類,按照類執(zhí)行
with open('http_TestCase.txt', 'w+', encoding='UTF-8') as file:
runner = unittest.TextTestRunner(stream=file, verbosity=2)
runner.run(suite)
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Python爬蟲之Selenium實(shí)現(xiàn)窗口截圖
這篇文章主要介紹了Python爬蟲之Selenium實(shí)現(xiàn)窗口截圖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python Pandas常用函數(shù)方法總結(jié)
今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識(shí),文章圍繞著Pandas常用函數(shù)方法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Python中plt.scatter()函數(shù)的常見用法小結(jié)
這篇文章主要介紹了Python中plt.scatter()函數(shù)的常見用法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
基于Python實(shí)現(xiàn)植物大戰(zhàn)僵尸游戲的示例代碼
植物大戰(zhàn)僵尸是一款經(jīng)典的塔防類游戲,玩家通過種植各種植物來抵御僵尸的攻擊,本文將詳細(xì)介紹如何使用Python和Pygame庫來實(shí)現(xiàn)一個(gè)簡單的植物大戰(zhàn)僵尸游戲,文中通過代碼示例講解的非常詳細(xì),感興趣的小伙伴跟著小編一起來看看吧2024-10-10
Pytorch+PyG實(shí)現(xiàn)GIN過程示例詳解
這篇文章主要為大家介紹了Pytorch+PyG實(shí)現(xiàn)GIN過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
python基礎(chǔ)知識(shí)之try...except...的詳細(xì)用法實(shí)例
在各種編程語言進(jìn)行工作和學(xué)習(xí)的過程中,都會(huì)有一些錯(cuò)誤異常,下面這篇文章主要給大家介紹了關(guān)于python基礎(chǔ)知識(shí)之try...except...的詳細(xì)用法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
YOLOv5中SPP/SPPF結(jié)構(gòu)源碼詳析(內(nèi)含注釋分析)
其實(shí)關(guān)于YOLOv5的網(wǎng)絡(luò)結(jié)構(gòu)其實(shí)網(wǎng)上相關(guān)的講解已經(jīng)有很多了,但是覺著還是有必要再給大家介紹下,下面這篇文章主要給大家介紹了關(guān)于YOLOv5中SPP/SPPF結(jié)構(gòu)源碼的相關(guān)資料,需要的朋友可以參考下2022-05-05
Appium+Python自動(dòng)化測試之運(yùn)行App程序示例
這篇文章主要介紹了Appium+Python自動(dòng)化測試之運(yùn)行App程序示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
Python數(shù)據(jù)結(jié)構(gòu)之圖的應(yīng)用示例
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之圖的應(yīng)用,結(jié)合實(shí)例形式分析了Python數(shù)據(jù)結(jié)構(gòu)中圖的定義與遍歷算法相關(guān)操作技巧,需要的朋友可以參考下2018-05-05

