Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finalizer function does not get called #225

Closed
pytestbot opened this issue Nov 14, 2012 · 7 comments
Closed

finalizer function does not get called #225

pytestbot opened this issue Nov 14, 2012 · 7 comments
Labels
type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Thomas Waldmann (BitBucket: thomaswaldmann, GitHub: thomaswaldmann)


py.test 2.3.3 release from pypi

:::python

import pytest

@pytest.fixture(scope="function")
def fix(request):
    print "fix init"
    def fin():
        print "fix finalize"
    request.addfinalizer(fin)
    return 42

def test_xxx(fix):
    print fix
    assert False

Output:

-------- Captured stdout -------------------------
fix init
42
======== 1 failed in 0.03 seconds ================

@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


ups, shouldn't first resolve then explain :)

If you run with "py.test -s" then you'll see the finalizer print. Setup/Call/Teardown phases of a test are stdout-captured separately.

@pytestbot
Copy link
Contributor Author

Original comment by Thomas Waldmann (BitBucket: thomaswaldmann, GitHub: thomaswaldmann):


ah, ok. is there a special reason why setup is usually shown, but teardown not?

@pytestbot
Copy link
Contributor Author

Original comment by BitBucket: ansel1, GitHub: ansel1:


I'm hitting this too. I have some log files I want to cat out to pytest's log in my finalizer if the test fails. Really struggling to figure out how to do this. Not intuitive to me that all logging in my finalizers is swallowed in all cases.

@pytestbot
Copy link
Contributor Author

Original comment by BitBucket: ansel1, GitHub: ansel1:


Here's the solution I came up with, based on some of the examples in the docs:

in conftest:

#!python

# This is needed to print out the client log file if the test fails.
# I want to print these logs in a fixture finalizer, but finalizers:
#   a. don't know if the test passed
#   b. can't print (all stdout is swallowed by pytest)
# So I need to make the pytest "report" available to the finalizer.  The finalizer
# can use the report to figure out if the test failed, and can append the
# contents of the log to the report.
@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call, __multicall__):
    # execute all other hooks to obtain the report object
    rep = __multicall__.execute()

    # set an report attribute for each phase of a call, which can
    # be "setup", "call", "teardown"

    setattr(item, "rep_" + rep.when, rep)
    return rep

@pytest.fixture
def loglog(request):
    cli = TestClientCli()
    def add_log_to_report():
        for rep_type in ['setup', 'call']:
            attr = 'rep_{}'.format(rep_type)
            rep = getattr(request.node, attr) if hasattr(request.node, attr) else None
            if rep and rep.failed:
                log = cli.read_client_log()
                rep.sections.append(("Client log file", log if log else "Log empty or missing"))
    request.addfinalizer(add_log_to_report)
    return cli

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jun 15, 2015
@moorecm
Copy link

moorecm commented Oct 7, 2015

Isn't this still an issue? Printing something from setup works but doesn't work in finalizers, that is. I'm seeing the finalizers run, they just can't print stuff for some reason.

@nicoddemus
Copy link
Member

Perhaps we should open a new issue specifically about prints not being shown for teardown?

@moorecm
Copy link

moorecm commented Oct 8, 2015

Well, I simplified my problem to open a new issue and wouldn't you know it worked fine. I must have something wrong on my side. I tested it using pytest 2.8.0.

fkohlgrueber pushed a commit to fkohlgrueber/pytest that referenced this issue Oct 27, 2018
There is no need to keep the pickled grammar files in git. PR pytest-dev#203 will
move them into a user-specific cache directory any way.

See: psf/black#192
Signed-off-by: Christian Heimes <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

3 participants