欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年09月17日 16:32:08   作者:小木可菜鳥測試一枚  
這篇文章主要介紹了Python中Unittest框架的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

2、編寫規(guī)范

需要導入 import unittest

測試類必須繼承unittest.TestCase

測試方法以 test_開頭

模塊和類名沒有要求

TestCase 理解為寫測試用例

TestSuite 理解為測試用例的集合

TestLoader 理解為的測試用例加載

TestRunner 執(zhí)行測試用例,并輸出報告

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 單個執(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)
# 運行結(jié)果
{'status': 1, 'code': '10001', 'data': None, 'msg': '登錄成功'}
{'status': 0, 'code': '20111', 'data': None, 'msg': '用戶名或密碼錯誤'}
{'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': '請輸入范圍在0到50萬之間的正數(shù)金額'}

執(zhí)行二:把test_data的數(shù)據(jù)放在EXCEL中運行。

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í)行整個文件
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實現(xiàn)梯度下降算法的實例詳解

    python實現(xiàn)梯度下降算法的實例詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于python實現(xiàn)梯度下降算法的實例詳解內(nèi)容,需要的朋友們可以參考下。
    2020-08-08
  • 一些Python中的二維數(shù)組的操作方法

    一些Python中的二維數(shù)組的操作方法

    這篇文章主要介紹了一些Python中的二維數(shù)組的操作方法,是Python學習當中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • 用Python解數(shù)獨的方法示例

    用Python解數(shù)獨的方法示例

    這篇文章主要介紹了用Python解數(shù)獨的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • python中的十大%占位符對應(yīng)的格式化的使用方法

    python中的十大%占位符對應(yīng)的格式化的使用方法

    本文主要介紹了python中的十大%占位符對應(yīng)的格式化的使用方法,它可以很好的幫助我們解決一些字符串格式化的問題, 文中通過示例代碼介紹的非常詳細,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Python元組的定義及使用

    Python元組的定義及使用

    這篇文章主要介紹了Python元組的定義及使用,在Python中元組是一個和列表非常類似的數(shù)據(jù)類型,不同之處就是列表中的元素可以修改,而元組之中的元素不可以修改。想具體了解的下小伙伴請參考下面文章的具體內(nèi)容,希望對你有所幫助
    2021-11-11
  • 如何解決PyTorch程序占用較高CPU問題

    如何解決PyTorch程序占用較高CPU問題

    這篇文章主要介紹了如何解決PyTorch程序占用較高CPU問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 使用python怎樣產(chǎn)生10個不同的隨機數(shù)

    使用python怎樣產(chǎn)生10個不同的隨機數(shù)

    這篇文章主要介紹了使用python實現(xiàn)產(chǎn)生10個不同的隨機數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 探索Python?Furl高性能URL構(gòu)建解析和操作功能實例

    探索Python?Furl高性能URL構(gòu)建解析和操作功能實例

    本文將提供關(guān)于Python?Furl的全面指南,包括安裝和配置、基本概念、URL解析、URL構(gòu)建、查詢參數(shù)操作、片段處理、實際應(yīng)用場景以及豐富的示例代碼
    2024-01-01
  • Python中的異常處理相關(guān)語句基礎(chǔ)學習筆記

    Python中的異常處理相關(guān)語句基礎(chǔ)學習筆記

    這里我們簡單整理一下Python中的異常處理相關(guān)語句基礎(chǔ)學習筆記,包括try...except與assert等基本語句的用法講解:
    2016-07-07
  • python 執(zhí)行文件時額外參數(shù)獲取的實例

    python 執(zhí)行文件時額外參數(shù)獲取的實例

    今天小編就為大家分享一篇python 執(zhí)行文件時額外參數(shù)獲取的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12

最新評論