pytest_report_teststatus挂钩在会话范围的拆卸fixture之后执行



钩子函数pytest_report_teststatus的拆卸部分/触发器似乎是在任何具有yield语句的会话范围的fixture之后执行的。我该怎么改?例如,这里是我的钩子函数:

def pytest_report_teststatus(report,): 
"""
Using the pytest hook pytest_report_teststatus.  This will give us
the test result of each test function.
Note, this hook can get called up to 3 times for one test funtion.
Test Setup, Call, and Teardown.  Call is the actual test function.
This hook function will write out test results for each
test to the summary.log file.
"""
sum_log = dnet_generic.get_logger()
if report.when == 'setup' and report.outcome == 'failed':
sum_log.info("n    --------------------------------------")
sum_log.info("    ---- Test Setup Stage FAILED!")
sum_log.info(f'    ---- Test: {report.nodeid}' )
sum_log.info("    --------------------------------------n")
elif report.when == 'call':
now = datetime.datetime.now()
end_time = now.strftime('%a %b %d %H:%M:%S %Z %Y')
sum_log.info("n    --------------------------------------")
sum_log.info('    ---- Test Function completed at: %s' % (end_time) )
sum_log.info(f'    ---- Test: {report.nodeid}' )
sum_log.info(f'    ---- Result: {report.outcome.upper()}' )
sum_log.info("    --------------------------------------n")
elif report.when == 'teardown' and report.outcome == 'failed':
sum_log.info("n    --------------------------------------")
sum_log.info("    ---- Test Teardown Stage FAILED!")
sum_log.info(f'    ---- Test: {report.nodeid}' )
sum_log.info("    --------------------------------------n")

这是会话范围内的fixture,带有yield语句:

@pytest.fixture(scope="session")
def logger(testinfo):
sum_log = dnet_generic.get_logger(initialize=True)
############################################################
# Don't like to do this but I'm going to add a new key
# to testinfo which defines the summary.log logger
# Since dictionaries are mutable this should work fine....
############################################################
testinfo.update({"sum_log" : sum_log })
start_time = testinfo['start_time'].strftime('%a %b %d %H:%M:%S %Z %Y')
script_name = sys.argv[0]
script_args = sys.argv[1:] # all remaining arguments other than script name
script_basename = os.path.basename(script_name)
sum_log.info("nn----------------------------------------------------------")
sum_log.info("----------------------------------------------------------")
sum_log.info('-----Pytest Session Started' )
sum_log.info('-----Start Time:: %s' % (start_time) )
sum_log.info('-----Results for script_name %s' % (script_basename))
sum_log.info('-----Script Arguments %s' % ' '.join(map(str,script_args)))
sum_log.info("----------------------------------------------------------")
sum_log.info("----------------------------------------------------------")
yield sum_log
now = datetime.datetime.now()
end_time = now.strftime('%a %b %d %H:%M:%S %Z %Y')
script_basename = os.path.basename(script_name)
sum_log.info("+++++++++++++++++++++++++++++++++++++++++++++")
sum_log.info("+++++++++++++++++++++++++++++++++++++++++++++")
sum_log.info('-----Pytest Session Ended' )
sum_log.info('-----End Time: %s' % (end_time) )
sum_log.info("+++++++++++++++++++++++++++++++++++++++++++++")
sum_log.info("+++++++++++++++++++++++++++++++++++++++++++++")

我只是有拆卸的问题。如果我在一个模块中有5个单独的测试函数,那么这个钩子会被调用5次,并将适当的消息写入日志文件,然后会话范围的fixture最后写入日志文件。

当另一个"函数范围"的fixture在yield语句之后失败时,日志文件的输出是什么样子的(我需要在"Pytest Session Ended"消息之前出现Teardown消息:

+++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++
-----Pytest Session Ended
-----End Time: Wed Apr 08 02:48:31  2020
+++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++
--------------------------------------
---- Test Teardown Stage FAILED!
---- Test: lag/test_preconfig_setup.py::test_lag_load_balance[au22-bundle-28130-4-80-128-True-60-lag_setup_test.yml]
--------------------------------------

谢谢辅助

从fixture传输会话日志"记录器";进入与测试会话相关的挂钩
示例-";"pytestrongessionstart";以及";"pytestrongessionfinish">
(https://docs.pytest.org/en/stable/_modules/_pytest/hookspec.html#pytestrongessionfinish)
我的简单检查-

@pytest.hookspec(firstresult=True)
def pytest_sessionstart(session):
print("n+++++++++++++++++++++++++++++++++++++++++++++")
print('n-----Pytest Session Started')
print("n+++++++++++++++++++++++++++++++++++++++++++++")

@pytest.hookspec(firstresult=True)
def pytest_sessionfinish(session, exitstatus):
print("n+++++++++++++++++++++++++++++++++++++++++++++")
print('n-----Pytest Session Ended')
print("n+++++++++++++++++++++++++++++++++++++++++++++")
  • 与您的"测试状态";,当夹具在屈服后失效时

最新更新