首页 > 解决方案 > Pytest:将测试变量或测试文件 var 传递给 @pytest.hookimpl(tryfirst=True, hookwrapper=True)

问题描述

我正在为 pytest 开发几个自定义记录器,以改善测试结果在执行时的表示。目前,对于测试的每次迭代,我都能够捕获测试名称、状态和参数化的 blob。为了能够按需使用自定义记录器,我希望能够在每个测试文件、每个测试或从 cli 全局打开它。

目前,当我查看@pytest.hookimpl(tryfirst=True, hookwrapper=True)测试文件变量或测试变量中的数据时,似乎无法访问。

有没有办法将特定变量从测试文件、测试文件中的特定测试或全局从 cli 传递到@pytest.hookimpl(tryfirst=True, hookwrapper=True)例如:(log_suite or log_test or cli_log) and log_file

conftest.py

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    setattr(item, "rep_" + rep.when, rep) # for later use

    if rep.when == 'call':
        data = {'when': rep.when, 'outcome': rep.outcome, 'nodeid':rep.nodeid}

        # the reason loggin is here, as I would like to log every test permutation result as its happening.
        if (log_suite or log_test or cli_log) and log_file: # somehow to get those
            # log logic

测试文件.py

import pytest 

log_suite = True
log_file = '/tmp/pytest.log'

@pytest.mark.parametrize('t_params', {300_permutations})
def test_permutations(t_params):
    log_test = True

    # some test logic ...

标签: pythonpytest

解决方案


您可以通过 访问钩子中的模块级变量item.module,例如

def pytest_runtest_makereport(item, call):
    yield
    log_file = item.module.log_file
    log_suite = item.module.log_suite
    ...

等等。但是,您将无法从函数本地范围访问变量,那时它们已经消失了。您可以自己将它们分配给一个模块,例如pytest

def test_permutations(t_params):
    pytest.log_test = True

def pytest_runtest_makereport(item, call):
    yield
    log_test = pytest.log_test

或您当前的测试模块:

def test_permutations(t_params):
    globals()['log_test'] = True

def pytest_runtest_makereport(item, call):
    yield
    log_test = item.module.log_test

或使用全局定义的任何内容,例如缓存:

def test_permutations(t_params, request):
    log_test = True
    request.config.cache.set('log_test', log_test)

def pytest_runtest_makereport(item, call):
    yield
    log_test = item.session.config.cache.get('log_test', None)

顺便说一句,没有这样的钩子pytest_runtest_logger,请仔细检查该名称,否则测试将无法运行。这是所有可用钩子的参考


推荐阅读