unittest单元测试框架—加载测试用例的3种方法以及测试报告存储管理
生活随笔
收集整理的這篇文章主要介紹了
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Flask项目2】生成token和验证
- 下一篇: 关于浮点数在计算机内存中的存储