前言:要有遥不可及的梦想,也要有脚踏实地的本事
此篇主要介绍:接口自动化测试框架中,如何生成测试报告,操作步骤如下:
1、测试报告保存路径——自建一个文件夹,且配置文件中做好配置
[path] # 测试报告存放路径 REPORT_PATH = ./test_reports # 测试用例存放路径 CASE_PATH = ./api_testcase
import os import configparser current_path = os.path.dirname(__file__) #当前文件路径 config_path = os.path.join( current_path , '..' ,'conf/config.ini' ) #配置文件路径 class LocalconfigUtils(): def __init__(self, config_path=config_path): self.cfg = configparser.ConfigParser() self.cfg.read(config_path) @property def REPORT_PATH(self): #测试报告路径 report_path_value = self.cfg.get('path','REPORT_PATH') return report_path_value @property def CASE_PATH(self): #测试用例路径 case_path_value = self.cfg.get('path','CASE_PATH') return case_path_value
2、使用paramunittest模块实现参数化,使用功能详情可参考:https://www.cnblogs.com/dream66/p/13286046.html
import warnings import unittest import paramunittest from common.testdata_utils import TestdataUtils from common.requests_utils import RequestsUtils case_infos = TestdataUtils().def_testcase_data_list() @paramunittest.parametrized( *case_infos ) class APITest(paramunittest.ParametrizedTestCase): def setUp(self) -> None: warnings.simplefilter('ignore', ResourceWarning) def setParameters(self, case_id, case_info): self.case_id = case_id self.case_info = case_info def test_api_common_function(self): '''测试描述''' self._testMethodName = self.case_info[0].get("测试用例编号") self._testMethodDoc = self.case_info[0].get("测试用例名称") actual_result = RequestsUtils().request_by_step(self.case_info) self.assertTrue( actual_result.get('check_result'),actual_result.get('message') ) if __name__ == '__main__': unittest.main()
3、新增一个能加载并运行所有用例的模块
import os import unittest from common.localconfig_utils import local_config #配置文件 from common import HTMLTestReportCN #生成测试报告工具,可查看附件 from common.email_utils import EmailUtils #发送email的模块 current_path = os.path.dirname(__file__) test_case_path = os.path.join( current_path ,'..',local_config.CASE_PATH ) test_report_path = os.path.join( current_path ,'..',local_config.REPORT_PATH ) class RunCase(): def __init__(self): self.test_case_path = test_case_path self.report_path = test_report_path self.title = '接口自动化测试报告' self.description = '自动化接口测试框架描述' self.tester = '测试人员' def load_test_suite(self): #加载所有测试用例 discover = unittest.defaultTestLoader.discover(start_dir=self.test_case_path, pattern='api_test.py', top_level_dir=self.test_case_path) all_suite = unittest.TestSuite() all_suite.addTest( discover ) return all_suite def run(self): report_dir = HTMLTestReportCN.ReportDirectory(self.report_path) #创建测试报告对象 report_dir.create_dir(self.title) report_file_path = HTMLTestReportCN.GlobalMsg.get_value('report_path') fp = open( report_file_path ,'wb' ) runner = HTMLTestReportCN.HTMLTestRunner(stream=fp, title=self.title, description=self.description, tester=self.tester) runner.run( self.load_test_suite() ) fp.close() return report_file_path if __name__=='__main__': report_path = RunCase().run() #生成测试报告 EmailUtils( open(report_path,'rb').read() ,report_path).send_mail() #生成测试报告并发送邮件