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

Can't get proper captured log call #3621

Closed
kiddten opened this issue Jun 26, 2018 · 5 comments
Closed

Can't get proper captured log call #3621

kiddten opened this issue Jun 26, 2018 · 5 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@kiddten
Copy link

kiddten commented Jun 26, 2018

Miniamal example to reproduce issue:

mycode.py

import asyncio

async def run():
    await asyncio.sleep(2)


def dumb(a, b):
    pass


def work():
    task = asyncio.ensure_future(run)
    task.add_done_callback(dumb)

test_code.py

from mycode import work
import pytest


@pytest.mark.asyncio
async def test_code():
    await work()
    # raise

pytest -s test_code.py
run it with commented raise and get successful result w/o any report

but there is an obvious issue with add_done_callback it will call that function with only one parameter

so if we uncomment raise statement we will get additional detail called Captured log call

__________________________________________________________________________________________________ test_code __________________________________________________________________________________________________

    @pytest.mark.asyncio
    async def test_code():
        await work()
>       raise
E       RuntimeError: No active exception to reraise

test_code.py:8: RuntimeError
---------------------------------------------------------------------------------------------- Captured log call ----------------------------------------------------------------------------------------------
base_events.py            1266 ERROR    Exception in callback dumb(<Task finishe...> result=None>) at /home/prj/mycode.py:8
handle: <Handle dumb(<Task finishe...> result=None>) at /home/prj/mycode.py:8>
Traceback (most recent call last):
  File "/usr/lib/python3.6/asyncio/events.py", line 145, in _run
    self._callback(*self._args)
TypeError: dumb() missing 1 required positional argument: 'b'

so is it possible to get that output with "successful" run?

@kiddten kiddten changed the title Can't get proper aptured log call Can't get proper captured log call Jun 26, 2018
@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #2647 (call to pytest_runtest_logreport is not proper ), #225 (finalizer function does not get called), #447 (Fixture params not accessible inside the fixture, not getting called multiple times.), #1210 (msg in pytest.exit(msg) doesn't get printed when called in pytest hook), and #2616 (The plugin hooks pytest_cmdline_parse and pytest_load_initial_conftests don't appear to get called).

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jun 26, 2018
@RonnyPfannschmidt RonnyPfannschmidt added type: question general question, might be closed after 2 weeks of inactivity and removed type: bug problem that needs to be addressed labels Jun 26, 2018
@nicoddemus
Copy link
Member

Hi @kiddick,

Currently the captured logs are only shown if a test fails.

I suppose you want your test to fail instead of just showing up an warning, so I think you can write a fixture which fails a test if it captures an specific logging message. Untested:

@pytest.fixture(autouse=True)
def error_on_async_callback(caplog):
    for r in caplog.records:
        if 'Exception in callback' in r.message:
            assert False, f'Error logged in callback: {r.message}'

@kiddten
Copy link
Author

kiddten commented Jun 28, 2018

HI @nicoddemus! Thank you for reply, I'll test this approach. May be it is reasonable to consider some cli option to enable log capturing even with successful run.

@nicoddemus
Copy link
Member

@kiddick I agree, currently we have -rP which prints captured output even for passing tests, I guess it makes sense to add -rW which does the same for captured warnings.

@nicoddemus
Copy link
Member

Created #3633 to follow up the feature request so I'm closing this one. Thanks again @kiddick!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

4 participants