日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

unittest单元测试框架—加载测试用例的3种方法以及测试报告存储管理

發(fā)布時間:2025/3/21 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unittest单元测试框架—加载测试用例的3种方法以及测试报告存储管理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

項目結(jié)構(gòu)

測試用例

import unittestclass LoginTestCase(unittest.TestCase):def test_login_success(self):self.assertEqual({'code': 200, 'msg': '登錄成功'}, self.login('kobe', '666'))def test_login_fail(self):self.assertEqual({'code': 201, 'msg': '賬號或者密碼不正確'}, self.login('kobe', '888'))def test_not_username(self):self.assertEqual({'code': 201, 'msg': '賬號不能為空'}, self.login('', '666'))def test_not_password(self):self.assertEqual({'code': 201, 'msg': '密碼不能為空'}, self.login('kobe', ''))def test_not_username_password(self):self.assertEqual({'code': 201, 'msg': '賬號和密碼不能為空'}, self.login('', ''))def login(self, username, password):if username == 'kobe' and password == '666':return {'code': 200, 'msg': '登錄成功'}if username == 'kobe' and username != '' and password != '666' and password != '':return {'code': 201, 'msg': '賬號或者密碼不正確'}if username == 'kobe' and password == '':return {'code': 201, 'msg': '密碼不能為空'}if username == '' and password == '666':return {'code': 201, 'msg': '賬號不能為空'}if username == '' and password == '':return {'code': 201, 'msg': '賬號和密碼不能為空'}

方法1:從類中加載所有的case

suite.addTest(loader.loadTestsFromTestCase(LoginTestCase))

from test_unittest import LoginTestCase#創(chuàng)建測試套件對象 suite=unittest.TestSuite() #創(chuàng)建加載器 loader=unittest.TestLoader() #將測試用例加載到測試套件種 suite.addTest(loader.loadTestsFromTestCase(LoginTestCase))#運行測試用例 #創(chuàng)建報告對象 br = BeautifulReport(suite) test_name = '測試報告1.html'BASE_DIR = os.path.dirname(__file__) print(BASE_DIR)REPORT_DIR = os.path.join(BASE_DIR, 'report') print(REPORT_DIR)br.report(description='測試報告', filename=test_name, report_dir=REPORT_DIR)

方法2:從模塊中加載所有的測試用例

suite.addTest(loader.loadTestsFromModule(test_unittest))

import test_unittest# todo 創(chuàng)建測試套件對象 suite = unittest.TestSuite() # todo 創(chuàng)建加載器 loader = unittest.TestLoader() # todo 將測試用例加載到測試套件中 suite.addTest(loader.loadTestsFromModule(test_unittest)) # todo 運行測試用例 # todo 創(chuàng)建報告對象 br = BeautifulReport(suite) test_name = '測試報告.html'BASE_DIR = os.path.dirname(__file__) print(BASE_DIR)REPORT_DIR = os.path.join(BASE_DIR, 'report') print(REPORT_DIR)br.report(description='測試報告', filename=test_name, report_dir=REPORT_DIR)

方法3:從文件夾中加載所有的測試用例

suite.addTest(loader.discover(r'D:\second_hand_car\unittest_work'))

#創(chuàng)建suite套件對象 suite=unittest.TestSuite() #創(chuàng)建加載器 loader=unittest.TestLoader() #將測試用例加載到測試套件中 suite.addTest(loader.discover(r'D:\second_hand_car\unittest_work')) #創(chuàng)建報告對象 br=BeautifulReport(suite) test_name = '測試報告2.html'BASE_DIR = os.path.dirname(__file__) print(BASE_DIR)REPORT_DIR = os.path.join(BASE_DIR, 'report') print(REPORT_DIR) br.report(description='測試報告', filename=test_name, report_dir=REPORT_DIR)

測試報告

***關(guān)于報告存儲

BASE_DIR=os.path.dirname(__file__):當前項目的根路徑
BASE_DIR1=os.path.dirname(os.path.dirname(__file__)):當前項目的根路徑的上一級目錄
REPORT_DIR=os.path.join(BASE_DIR,'report'):項目目錄下創(chuàng)建report包,用于存放測試報告,將report和項目根路徑進行拼接,得到完整的包路徑

目錄結(jié)構(gòu)

如果根目錄和所拼接的包不存在,會自動生成所拼接的包

例如:當前report_dir目錄不存在,用根目錄和report_dir進行拼接,會生成report_dir包

REPORT_DIR = os.path.join(BASE_DIR, 'report_dir') print(REPORT_DIR)

總結(jié)

以上是生活随笔為你收集整理的unittest单元测试框架—加载测试用例的3种方法以及测试报告存储管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。