一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python中unittest框架應(yīng)用詳解

python中unittest框架應(yīng)用詳解

2022-01-07 10:27小木可菜鳥測(cè)試一枚 Python

這篇文章主要介紹了Python中Unittest框架的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1、Unittest為Python內(nèi)嵌的測(cè)試框架,不需要特殊配置

2、編寫規(guī)范

需要導(dǎo)入 import unittest

測(cè)試類必須繼承unittest.TestCase

測(cè)試方法以 test_開頭

模塊和類名沒有要求

TestCase 理解為寫測(cè)試用例

TestSuite 理解為測(cè)試用例的集合

TestLoader 理解為的測(cè)試用例加載

TestRunner 執(zhí)行測(cè)試用例,并輸出報(bào)告

?
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
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í)行一:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)行。

?
1
2
3
4
5
6
7
8
9
10
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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方式一

?
1
2
3
4
5
6
7
8
9
10
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方式二

?
1
2
3
4
5
6
7
8
9
10
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)注服務(wù)器之家的更多內(nèi)容!

原文鏈接:https://blog.csdn.net/m0_51709670/article/details/120335995

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: free白嫩性hd | 禁忌4中文 | 亚洲狠狠婷婷综合久久久久网站 | 日本一区二区免费在线 | 欧美三级不卡在线观线看高清 | 精品午夜寂寞影院在线观看 | 亚洲AV无码专区国产精品麻豆 | 欧美男男gaygaysxxx | 黑人群性xxx | 日本高清有码视频 | 日韩r| 波多野结衣黑人系列在线观看 | 国产欧美日韩精品一区二区三区 | 99久久综合九九亚洲 | 精品国产福利片在线观看 | 国产在线观看网站 | 亚洲精品福利一区二区在线观看 | 9久热这里只有精品视频在线观看 | 国产性片在线观看 | 特黄视频 | 国产日韩一区二区 | 国产亚洲女在线精品 | 国产乱子伦真实china | 亚洲欧美日韩成人 | 精品精品国产自在久久高清 | 日本片免费观看一区二区 | 亚洲热在线视频 | 日韩妹妹 | 日本黄色一区 | 热久久天天拍天天拍热久久2018 | 99精品视频在线观看免费播放 | 欧美人畜 | 成年人视频在线 | 久久囯产精品777蜜桃传媒 | 操儿媳小说| 午夜国产 | 关晓彤一级做a爰片性色毛片 | 亚洲国产成人精品无码区5566 | 国产伦码精品一区二区 | 公交车高h| 91久久精品视频 |