【Python】 pytest 之Hook函数 (钩子函数)

Hook函数 (钩子函数)

钩子函数在 pytest 称之为 Hook 函数,它 pytest 框架的开发者,为了让用户更好的去扩展开发预留的一些函数。而预留的这些函数,在整个测试执行的生命周期中特定的阶段会自动去调用执行
关于 pytest 中的预留钩子,可以通过开发插件,和在 conftest.py 去实现这些钩子

官方文档:https://docs.pytest.org/en/latest/reference/reference.html#hooks

 

定义  

1、是个函数,在系统消息触发时被系统调用
2、不是用户自己触发的
3、使用时直接编写函数体
4、钩子函数的名称是确定,当系统消息触发,自动会调用。

 

 

pytest 中的钩子函数按功能一共分为 6 类: 引导钩子,初始化钩子、用例收集钩子、用例执行钩子、报告钩子、调试钩子

 

 

第一部分:引导钩子 setuptools 

引导挂钩要求足够早注册的插件(内部和setuptools插件),可以使用的钩子

 

def pytest_load_initial_conftests(early_config, parser, args):
    """
    在命令行选项解析之前实现初始conftest文件的加载. (即:当在命令行通过 pytest 执行命令时,会先执行该钩子函数)

    :param early_config:pytest 配置对象。
    :param args:命令行上传递的参数。
    :param parser:命令行添加的选项。
    :return: 注意:该钩子函数只有定义在插件中才会调用,在 conftest 定义则不会调用
    """
import time


def pytest_terminal_summary(terminalreporter, exitstatus, config):
    """
    获取测试执行结果参数, 收集测试结果

    :param terminalreporter:内部使用的终端测试报告对象
    :param exitstatus:返回给操作系统的返回码
    :param config:pytest 的 config 对象
    :return:
    """
    total_case = terminalreporter._numcollected
    passed_case = len(terminalreporter.stats.get('passed', []))
    failed_case = len(terminalreporter.stats.get('failed', []))
    error_case = len(terminalreporter.stats.get('error', []))
    skipped_case = len(terminalreporter.stats.get('skipped', []))
    deselected_case = len(terminalreporter.stats.get('deselected', []))
    real_case = total_case - deselected_case - skipped_case
    session_starttime = terminalreporter._sessionstarttime
    total_time = time.time() - session_starttime

    print("用例总数:", total_case)
    print('无效用例条数:', deselected_case)
    print('跳过用例条数:', skipped_case)
    print("有效用例条数", real_case)
    print('通过用例条数:', passed_case)
    print('失败用例条数:', failed_case)
    print('报错用例条数:', error_case)
    print('本次执行用例成功率:', round(passed_case / real_case * 100, 2), "%")
    print('本次测试总用时:', round(total_time, 2), 'seconds')
pytest_terminal_summary 示例

 

 

def pytest_cmdline_preparse(config, args):
    """(不建议使用)在选项解析之前修改命令行参数。"""

 

 

def pytest_cmdline_parse(pluginmanager, args):
    """
    返回一个初始化的配置对象,解析指定的args。(即:用来初始化配置对象,解析指定的参数)

    :param pluginmanager: 插件管理器
    :param args: 命令行上传递的参数。
    :return: Tips:该钩子函数只有定义在插件中才会调用,在 conftest 定义则不会调用
    """

 

 

def pytest_cmdline_main(config):
    """
    要求执行主命令行动作。默认实现将调用configure hooks和runtest_mainloop。 (即:调用命令解析钩子 pytest_cmdline_parse 和执行 runtest_mainloop)

    :param config: pytest 配置对象
    :return:
    """

 

 

 

第二部分: 初始化挂钩

初始化钩子需要插件和conftest.py文件
pytest_addoption(parser): 
    """注册argparse样式的选项和ini样式的配置值,这些值在测试运行开始时被调用一次。"""
    
pytest_addhooks(pluginmanager): 
    """在插件注册时调用,以允许通过调用来添加新的挂钩"""
    
pytest_configure(config): 
    """许插件和conftest文件执行初始配置"""
    
pytest_unconfigure(config): 
    """在退出测试过程之前调用。"""
    
pytest_sessionstart(session): 
    """在Session创建对象之后,执行收集并进入运行测试循环之前调用。"""
    
pytest_sessionfinish(session,exitstatus): 
    """在整个测试运行完成后调用,就在将退出状态返回系统之前。"""
    
pytest_plugin_registered(plugin,manager):
    """一个新的pytest插件已注册。"""

 


第三部分: collection 收集钩子

pytest_collection(session): 
    """执行给定会话的收集协议"""
    
pytest_collect_directory(path, parent): 
    """在遍历目录以获取集合文件之前调用"""
    
pytest_collect_file(path, parent):
    """为给定的路径创建一个收集器,如果不相关,则创建“无”。"""
    
pytest_pycollect_makemodule(path: py._path.local.LocalPath, parent):
    """返回给定路径的模块收集器或无。"""
    
pytest_pycollect_makeitem(collector: PyCollector, name: str, obj: object):
    """ 返回模块中Python对象的自定义项目/收集器,或者返回None。在第一个非无结果处停止"""
    
pytest_generate_tests(metafunc: Metafunc):
    """生成(多个)对测试函数的参数化调用。"""
    
pytest_make_parametrize_id(config: Config, val: object, argname: str):
    """返回val 将由@pytest.mark.parametrize调用使用的给定用户友好的字符串表示形式,如果挂钩不知道,则返回None val。"""
    
pytest_collection_modifyitems(session: Session, config: Config, items: List[Item]):
    """在执行收集后调用。可能会就地过滤或重新排序项目。"""
    
pytest_collection_finish(session: Session):
    """在执行并修改收集后调用。"""

 

第四部分:测试运行(runtest)钩子

pytest_runtestloop(session: Session):
    """执行主运行测试循环(收集完成后)。"""
    
pytest_runtest_protocol(item: Item, nextitem: Optional[Item]):
    """ 对单个测试项目执行运行测试协议。"""
    
pytest_runtest_logstart(nodeid: str, location: Tuple[str, Optional[int], str]):
    """在运行单个项目的运行测试协议开始时调用。"""
    
pytest_runtest_logfinish(nodeid: str, location: Tuple[str, Optional[int], str]):
    """在为单个项目运行测试协议结束时调用。"""
    
pytest_runtest_setup(item: Item):
    """ 调用以执行测试项目的设置阶段。"""
    
pytest_runtest_call(item: Item):
    """ 调用以运行测试项目的测试(调用阶段)。"""
    
pytest_runtest_teardown(item: Item, nextitem: Optional[Item]):
    """ 调用以执行测试项目的拆卸阶段。"""
    
pytest_runtest_makereport(item: Item, call: CallInfo[None]):
    """ 被称为为_pytest.reports.TestReport测试项目的每个设置,调用和拆卸运行测试阶段创建一个。"""

pytest_pyfunc_call(pyfuncitem: Function):
    """ 调用基础测试功能。"""


第五部分:Reporting 报告钩子

pytest_collectstart(collector: Collector): 
  """  收集器开始收集  """
pytest_make_collect_report(collector: Collector): 
  """  执行collector.collect()并返回一个CollectReport。  """
  
pytest_itemcollected(item: Item): 
  """  我们刚刚收集了一个测试项目。  """ 
  
pytest_collectreport(report: CollectReport) : 
  """  收集器完成收集。  """
  
pytest_deselected(items: Sequence[Item]): 
  """  要求取消选择的测试项目,例如按关键字。  """ 
  
pytest_report_header(config: Config, startdir: py._path.local.LocalPath) : 
  """  返回要显示为标题信息的字符串或字符串列表,以进行终端报告。  """
  
pytest_report_collectionfinish(config: Config, startdir: py._path.local.LocalPath, items: Sequence[Item]): 
  """  返回成功完成收集后将显示的字符串或字符串列表。  """
    
pytest_report_teststatus(report: Union[CollectReport, TestReport], config: Config): 
  """  返回结果类别,简写形式和详细词以进行状态报告。  """
  
pytest_terminal_summary(terminalreporter: TerminalReporter, exitstatus: ExitCode, config: Config): 
  """  在终端摘要报告中添加一个部分。  """
  
pytest_fixture_setup(fixturedef: FixtureDef[Any], request: SubRequest): 
  """  执行夹具设置执行。  """
     
pytest_fixture_post_finalizer(fixturedef: FixtureDef[Any], request: SubRequest): 
  """  在夹具拆除之后但在清除缓存之前调用,因此夹具结果fixturedef.cached_result仍然可用(不是 None)  """
     
pytest_warning_captured(warning_message: warnings.WarningMessage, when: Literal[‘config’, ‘collect’, ‘runtest’], item: Optional[Item], location: Optional[Tuple[str, int, str]]): 
  """  (已弃用)处理内部pytest警告插件捕获的警告。  """
     
pytest_warning_recorded(warning_message: warnings.WarningMessage, when: Literal[‘config’, ‘collect’, ‘runtest’], nodeid: str, location: Optional[Tuple[str, int, str]]): 
  """  处理内部pytest警告插件捕获的警告。  """
     
pytest_runtest_logreport(report: TestReport): 
  """  处理项目的_pytest.reports.TestReport每个设置,调用和拆卸运行测试阶段产生的结果。  """
     
pytest_assertrepr_compare(config: Config, op: str, left: object, right: object): 
  """返回失败断言表达式中的比较的说明。"""
     
pytest_assertion_pass(item: Item, lineno: int, orig: str, expl: str): 
  """(实验性的)在断言通过时调用。"""
     


第六部分:调试/相互作用钩

很少有可以用于特殊报告或与异常交互的挂钩

pytest_internalerror(excrepr: ExceptionRepr, excinfo: ExceptionInfo[BaseException]):
    """  要求内部错误。返回True以禁止对将INTERNALERROR消息直接打印到sys.stderr的回退处理。 """
    
pytest_keyboard_interrupt(excinfo: ExceptionInfo[Union[KeyboardInterrupt, Exit]]):
    """ 要求键盘中断。 """ 
    
pytest_exception_interact(node: Union[Item, Collector], call: CallInfo[Any], report: Union[CollectReport, TestReport]):
    """ 在引发可能可以交互处理的异常时调用。 """ 
    
pytest_enter_pdb(config: Config, pdb: pdb.Pdb):
    """  调用了pdb.set_trace()。 """

 

posted @ 2023-04-03 15:04  Phoenixy  阅读(1780)  评论(0)    收藏  举报