Python单元测试框架——pytest(五)


什么是Hook函数

Pytest不仅自身功能强大,而且可以通过添加各种插件(plugin)的方式来扩展功能。为pytest开发插件,就需要用到Hook函数,又称之为钩子函数。

一个插件包含一个或多个钩子函数。pytest通过调用3种不同调用方式的插件,来实现配置、收集、运行和报告的各个方面:

  • 内置插件:从 pytest 的内部_pytest 目录加载。
  • 外部插件:通过setuptools入口点发现的模块
  • conftest.py插件:在测试目录中自动发现的模块

钩子函数在pytest称之为Hook函数,是pytest框架的开发者,为了让用户更好的去扩展开发预留的一些函数。而预留的这些函数,在整个测试执行的生命周期中特定的阶段会自动去调用执行。预留的函数是没有开发完成的,只定义了方法名的函数,其中的逻辑需要进行自行开发。pytest中的钩子函数按功能一共分为6类:引导钩子,初始化钩子、用例收集钩子、用例执行钩子、报告钩子、调试钩子,即实现了其中的某个预留函数即钩子函数,在测试执行过程中会自动调用执行。

钩子函数(Hook函数)官方介绍文档:https://docs.pytest.org/en/latest/reference/reference.html#hooks

1 引导钩子

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

  • pytest_load_initial_conftests(early_config,parser,args): 在命令行选项解析之前实现初始conftest文件的加载。
  • pytest_cmdline_preparse(config,args): (不建议使用)在选项解析之前修改命令行参数。
  • pytest_cmdline_parse(pluginmanager,args): 返回一个初始化的配置对象,解析指定的args。
  • pytest_cmdline_main(config): 要求执行主命令行动作。默认实现将调用configure hooks和runtest_mainloop。

2 初始化钩子

初始化钩子需要插件和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插件已注册。

3 用例收集钩子

  • 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) 在执行并修改收集后调用。

4 用例执行钩子

  • 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) 调用基础测试功能。

5 报告钩子

  • 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) (实验性的)在断言通过时调用。

6 调试钩子

  • 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()。

实际例子

用例执行失败截图添加到报告

在上面的【4 用例执行钩子】中找到pytest_runtest_makereport(item: Item, call: CallInfo[None]) 函数进行实现;

在项目根目录的conftest.py中增加如下代码:

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    # 获取钩子方法的调用结果
    out = yield
    # 从钩子方法的调用结果中获取测试报告
    report = out.get_result()
		
    if report.when == "call":
        # 当执行用例失败进行截图
        if report.failed:
            add_img_to_report(get_driver, "失败截图", need_sleep=False)

其中:

装饰器@pytest.hookimpl(hookwrapper=True, tryfirst=True)

  • hookimpl()是一个回调函数。被@pytest.hookimpl()装饰器修饰的钩子函数,可以获取到测试用例不同执行阶段的结果(setup, call , teardown)。这里修饰的是pytest_runtest_makereport(),即返回用例执行的result对象和report对象。
  • hookimpl函数传入hookwrapper=True时,需要这个插件实现一个yield,优先执行yield前的代码,然后去执行其他插件的代码,最后再执行yield后面的代码,同时通过yield可以获取其他插件执行的结果
  • hookimpl函数传入tryfirst=True时,这个hook函数优先执行

钩子函数pytest_runtest_makereport(item,call)

  • item是测试用例对象;
  • call是测试用例的步骤,具体如下:
    1. 先执行when="setup",返回setup用例前置操作的执行结果;
    2. 再执行when="call",返回call测试用例的执行结果;
    3. 最后执行when="teardown",返回teardown用例后置操作函数的执行结果;

执行上面的测试用例,查看报告

在pytest-html报告中添加备注

可以通过hook函数将测试用例函数的文档注释提取到报告中的一列展示出来。

安装的pytest-html报告,默认只有result、test、Duration和Links四列,这里增加一列备注信息:Description

在confteset.py 增加如下代码:

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)


def pytest_html_results_table_header(cells):
    cells.insert(1, html.th('Description'))


def pytest_html_results_table_row(report, cells):
    cells.insert(1, html.td(report.description))

这里的pytest_html_results_table_headerpytest_html_results_table_row都是pytest-html这款插件的hook函数

在用例函数中增加注释文档:

@pytest.mark.parametrize("cases", cases)
    def test_01_login(self, cases):
        """ 测试登录功能 """
        allure.dynamic.title(cases["测试用例ID"] + cases["接口名称"])
        allure.dynamic.story(cases["模块"])
        url = conf_y.get_host() + cases['请求URL']
        data = json.loads(cases["请求数据"])
        method = cases["请求方法"]
        allure.dynamic.description(
            "请求地址:" + url + "\n请求方法:" + method + "\n响应体提取器:" + str(cases["extract"]) + "\n预期结果断言表达式:" + cases["期望结果断言"])
        response = PLRequests.pl_request(method, url=url, data=data)
        pl_assert(cases["期望结果断言"], response.json())
@pytest.mark.parametrize("cases", cases)
    def test_order_flow(self, cases):
        """ 测试下单流程 """
        # 从excel读取到的每一行数据,先进行变量替换,再进行整理
        new_cases = data_cleaning(replace_case_with_re(cases))
        allure.dynamic.story(new_cases["模块"])
        allure.dynamic.title(new_cases["测试用例ID"] + "_" + new_cases["接口名称"])
        allure.dynamic.description(
            "请求地址:" + new_cases["url"] + "\n请求方法:" + cases["请求方法"] + "\n请求数据:" + cases["请求数据"] + "\n响应体提取器:" + str(
                cases["extract"]) + "\n预期结果断言表达式:" +
            cases["期望结果断言"])
        response = PLRequests.pl_request(method=new_cases["请求方法"], url=new_cases["url"], headers=new_cases['Headers'],
                                         params=new_cases['请求数据'], data=new_cases['请求数据'], json=new_cases["请求数据"])
        pl_assert(new_cases["期望结果断言"], response.json())

        # 在有extract列的请求的响应结果中提取数据,并设置为全局变量
        if new_cases["extract"]:
            extract_data_from_response(new_cases["extract"], response.json())
            pl_log.info("从接口请求中获取全局变量提取表达式为:{}".format(new_cases["extract"]))

执行用例


文章作者: 老百
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 老百 !
 上一篇
接口测试之基础入门 接口测试之基础入门
接口是指系统或组件之间的交互点,通过这些交互点可以实现数据的交互(交换、传递和控制管理)以及相互逻辑依赖关系。
2022-12-30
下一篇 
Python单元测试框架——pytest(四) Python单元测试框架——pytest(四)
Allure是一款轻量级的开源自动化测试报告生成框架。它支持绝大部分测试框架,比如TestNG、Junit 、pytest、unittest等。本文主要介绍pytest框架结合Allure生成美观的测试报告。
2022-12-18
  目录