0

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). enter image description here

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

Feedforward
  • 4,521
  • 4
  • 22
  • 34
  • You will get more and better answers if you create a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example. Especially make sure that the input and expected test data are complete (not pseudo-data), and can be easily cut and and paste into an editor to allow testing proposed solutions. – Stephen Rauch May 06 '18 at 21:09
  • Possible duplicate of [Static methods in Python?](https://stackoverflow.com/questions/735975/static-methods-in-python) – hoefling May 06 '18 at 21:49
  • Well, I don't see any relations with my question... – Feedforward May 06 '18 at 21:56
  • What arguments will `pytest_runtest_protocol` get when `MyPlugin().pytest_runtest_protocol(*args)` is called? What should you change in order for it to work? – hoefling May 06 '18 at 22:00
  • Okay, I did not realize that these methods should be static. Everything works fine with `@staticmethod` decorator. Thank you for help. – Feedforward May 06 '18 at 22:12

0 Answers0