Pytest允许您通过实现pytest_runtest_teardown插件中调用的函数来挂钩每个测试的拆解阶段:
def pytest_runtest_teardown(item, nextitem):
pass
Run Code Online (Sandbox Code Playgroud)
是否有一个属性或方法item,我可以用来确定刚刚完成运行的测试是通过还是失败?我找不到任何文档pytest.Item和搜索源代码并在游戏中ipdb没有透露任何明显的东西.
您还可以考虑在pytest_runtest_makereport中使用call.excinfo:
def pytest_runtest_makereport(item, call):
if call.when == 'setup':
print('Called after setup for test case is executed.')
if call.when == 'call':
print('Called after test case is executed.')
print('-->{}<--'.format(call.excinfo))
if call.when == 'teardown':
print('Called after teardown for test case is executed.')
Run Code Online (Sandbox Code Playgroud)
调用对象包含大量附加信息(测试开始时间,停止时间等)。
请参阅:http : //doc.pytest.org/en/latest/_modules/_pytest/runner.html
def pytest_runtest_makereport(item, call):
when = call.when
duration = call.stop-call.start
keywords = dict([(x,1) for x in item.keywords])
excinfo = call.excinfo
sections = []
if not call.excinfo:
outcome = "passed"
longrepr = None
else:
if not isinstance(excinfo, ExceptionInfo):
outcome = "failed"
longrepr = excinfo
elif excinfo.errisinstance(pytest.skip.Exception):
outcome = "skipped"
r = excinfo._getreprcrash()
longrepr = (str(r.path), r.lineno, r.message)
else:
outcome = "failed"
if call.when == "call":
longrepr = item.repr_failure(excinfo)
else: # exception in setup or teardown
longrepr = item._repr_failure_py(excinfo,
style=item.config.option.tbstyle)
for rwhen, key, content in item._report_sections:
sections.append(("Captured %s %s" %(key, rwhen), content))
return TestReport(item.nodeid, item.location,
keywords, outcome, longrepr, when,
sections, duration)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1429 次 |
| 最近记录: |