qff
qff

Reputation: 5952

Why is pytest hook defined in the same file not running?

In the py.test documentation (v3.0.3) it states that I can make a test hook like this:

import pytest


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    print "Running hook!"

    yield

    print "Doing interesting stuff inside hook!"


def test_call_fails():
    assert False

But running it (pytest -s mytest.py), I don't get any printed output.

I believe this is because the hook is never run -- why is this?

Upvotes: 4

Views: 1618

Answers (1)

qff
qff

Reputation: 5952

I figured out that I need to put the hook in a separate file conftest.py (in the same folder) in order for it to be run:

conftest.py

import pytest

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    print "Running hook!"

    yield

    print "Doing interesting stuff inside hook!"

mytest.py

def test_call_fails():
    assert False

And now it works!

this is also shown in the documentation with the comment # contents of conftest.py but this unfortunately wasn't clear to me at all in my numerous readthroughs :/

Upvotes: 4

Related Questions