unittest单元测试总结

it2025-12-17  9

unittest 运行方式: 1, unittest 右键, pycharm 带的 2, python 代码 main : unittest.main(), 3, python -m unittest 断言方式: self.assertEqual(expected, actual) # 提示能够提示出预期结果和实际结果 self.assertTrue(表达式) # 预期结果和实际结果,没有具体的提示

条件准备:前置条件和后置条件 def setUp(self): def tearDown(self): 前置条件, 测试用例执行之前都会执行的代码 后置条件,测试用例执行之后会执行的代码。

测试用例执行流程: 1, 初始化加载器。 testloader = unittest.TestLoader() 2, 查找测试用例。 suite = testloader.discover(文件夹, ‘test_*.py’) 3, 打开一个 with open() as f: 4, 初始化运行器: runner = 运行器(f) 5, 运行测试用例 “ ” runner.run(suite) “”" 代码实现如下:

""" 收集测试用例: TestLoader, 加载器,加载测试用例 放到测试集合(测试套件) TestSuite 1, 初始化 testloader 2, suite = testloader.discover(文件夹路径, 'test_*.py') 发现测试用例 3, 如果你想运行的用例,放到指定的文件夹当中, # TestRunner, 1, 运行用例 2,生成测试报告 """ import os import unittest #1, 初始化 testloader对象 testloader = unittest.TestLoader() # 2, 查找测试用例,加载 # 获取根部文件夹的路径 dir_path = os.path.dirname(os.path.abspath(__file__)) # 拼接成test_cases的路径 case_path = os.path.join(dir_path, 'test_cases') # 查找测试用例 suite = testloader.discover(case_path) print(suite) # report # 测试报告路径 report_path = os.path.join(dir_path, 'report') # 测试报告路径要是不存在,就创建测试报告文件夹 if not os.path.exists(report_path): os.mkdir(report_path) # test_result.txt文件夹 file_path = os.path.join(report_path, 'test_result.txt') # text. with open(file_path,"w", encoding='utf-8') as f: # 初始化运行器, 是以普通文本生成测试报告 TextTestRunner runner = unittest.TextTestRunner(f, verbosity=2) # 运行测试用例 runner.run(suite)

完整的用例代码在git上 https://github.com/guiyin1150/qianchengdai

(二)还有一些场景,如果不想运行全部用例只想运行部分用例,例如只想做冒烟测试,这时候该怎么办呢? 下边介绍:

loadTestsFromTestCase (加载指定的测试类)loadTestsFromModule(加载模块中的测试用例)HTMLTestRunner (第三方工具),可用于生成HTML格式的测试报告 示例代码如下仅供参考: """ 收集测试用例: TestLoader, 加载器,加载测试用例 放到测试集合(测试套件) TestSuite 1, 初始化 testloader 2, suite = testloader.discover(文件夹路径, 'test_*.py') 发现测试用例 3, 如果你想运行的用例,放到指定的文件夹当中, # TestRunner, 1, 运行用例 2, 生成测试报告 几种加载测试用例的方式: 1, 用的最多,整个项目一起加载,使用:discover 2, 你想只测试某一个具体的某块,功能,使用 loadTestsFromTestCase 或者 loadTestsFromModule 3, pytest, HTMLTestRunner, 不是unittest 自带的。需要自己去安装。 1,安装方式不是通过 pip 2, 他是别人写好的一个模块,你可以直接下载下来的 .py 3, 复制到项目目录下,然后倒入。 4, 第二种方式:我们可以放到 python 的公共库当中 """ import os import unittest #1, 初始化 testloader import time from class_18_report.HTMLTestRunnerNew import HTMLTestRunner from class_18_report.test_cases import test_login, test_register from class_18_report.test_cases.test_login import TestLogin from class_18_report.test_cases.test_rechage import TestRecharge from class_18_report.test_cases.test_register import TestRegister testloader = unittest.TestLoader() # 2, 查找测试用例,加载 dir_path = os.path.dirname(os.path.abspath(__file__)) case_path = os.path.join(dir_path, 'test_cases') # suite = testloader.discover(case_path) # 加载两个模块当中的测试用例,保存到测试套件当中 # suite = testloader.loadTestsFromModule(test_login) # suite2 = testloader.loadTestsFromModule(test_register) # 添加指定的测试类 suite = testloader.loadTestsFromTestCase(TestLogin) suite2 = testloader.loadTestsFromTestCase(TestRecharge) # 讲这两个测试套件合并添加到一个总的测试套件套件 suite_total = unittest.TestSuite() suite_total.addTests(suite) suite_total.addTests(suite2) # suite = testloader.loadTestsFromName() print(suite) # report report_path = os.path.join(dir_path, 'report') if not os.path.exists(report_path): os.mkdir(report_path) # 什么格式??加进去时间戳,可以动态生成测试报告time.time()是float型 ts = str(int(time.time())) # test_result_234.56.html file_name = 'test_result_{}.html'.format(ts) file_path = os.path.join(report_path, file_name) # text. # TODO: 一定要使用二进制的方式打开 with open(file_path, 'wb') as f: # 使用 HTMLTestRunner runner = HTMLTestRunner(f, title="HTML格式的测试报告", description='自动化生成的HTML测试报告', tester='testing') runner.run(suite_total)

测试报告如下:

最新回复(0)