I just started dealing with pytest hooks and I immediately encountered strange behavior of pytest_runtest_protocol(item, nextitem)
hook in case of calling pytest from python code. I tried to reproduce code from https://stackoverflow.com/a/29140467/3124227, but in pytest_runtest_protocol
myPlugin
object is passed as first argument and not testitem (see screenshot).
After that line I get exception because MyPlugin
object has no ihook
property.
This problem doesn't appear in case of athoner ways of calling pytest (with hook code in conftest.py
).
Project has only two files.
test_main.py
def test_first():
assert 2 + 2 == 4
def test_second():
assert 2 + 2 == 4
hooks.py
import pytest
from _pytest.runner import runtestprotocol
class MyPlugin(object):
def pytest_runtest_protocol(item, nextitem):
reports = runtestprotocol(item, nextitem=nextitem)
for report in reports:
if report.when == 'call':
print '\n%s --- %s' % (item.name, report.outcome)
return True
def main():
pytest.main(plugins=[MyPlugin()])
if __name__ == '__main__':
main()
Is it possible to solve this problem?
Version of pytest: 3.2.1