It depends on what data you want to emit. If a simple completion check will suffice, write a custom pytest_runtest_logfinish
hook in the conftest.py
file as it directly provides lots of test info:
def pytest_runtest_logfinish(nodeid, location):
(filename, line, name) = location
print('finished', nodeid, 'in file', filename,
'on line', line, 'name', name)
Should you need to access the test result, a custom pytest_runtest_makereport
is a good option. You can get the same test info (and more) as above from the item
parameter:
import pytest
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
report = yield
result = report.get_result()
if result.when == 'teardown':
(filename, line, name) = item.location
print('finished', item.nodeid, 'with result', result.outcome,
'in file', filename, 'on line', line, 'name', name)
You can also go with the fixture teardown option, as suggested in the comments:
@pytest.fixture(autouse=True)
def myhook(request):
yield
item = request.node
(filename, line, name) = item.location
print('finished', item.nodeid, 'in file', filename,
'on line', line, 'name', name)
However, it depends on when you want your webhook to emit - the custom hookimpls above will run when the test finished and all fixtures have finalized, while with the fixture example, you can't guarantee that all fixtures have finalized yet as there's no explicit fixture ordering. Also, should you need the test result, you can't access it in a fixture.