Skip to content

Using pytest with aiologger crashes pytest stdout capturing. #9119

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

Closed
zefciu opened this issue Sep 23, 2021 · 2 comments
Closed

Using pytest with aiologger crashes pytest stdout capturing. #9119

zefciu opened this issue Sep 23, 2021 · 2 comments

Comments

@zefciu
Copy link

zefciu commented Sep 23, 2021

My output of pip freeze is:

aiologger==0.6.1
anyio==3.3.1
attrs==21.2.0
backcall==0.2.0
certifi==2021.5.30
charset-normalizer==2.0.6
decorator==5.1.0
fastapi==0.68.1
h11==0.12.0
httpcore==0.13.7
httpx==0.19.0
idna==3.2
iniconfig==1.1.1
ipdb==0.13.9
ipython==7.27.0
jedi==0.18.0
matplotlib-inline==0.1.3
packaging==21.0
parso==0.8.2
pexpect==4.8.0
pickleshare==0.7.5
pkg_resources==0.0.0
pluggy==1.0.0
prompt-toolkit==3.0.20
ptyprocess==0.7.0
py==1.10.0
pydantic==1.8.2
Pygments==2.10.0
pyparsing==2.4.7
pytest==6.2.5
pytest-asyncio==0.15.1
rfc3986==1.5.0
sniffio==1.2.0
starlette==0.14.2
toml==0.10.2
traitlets==5.1.0
typing-extensions==3.10.0.2
wcwidth==0.2.5

To reproduce this bug, you can use the following code:

import sys                                                                     
                                                                               
import pytest                                                                  
from fastapi import FastAPI, APIRouter                                         
from httpx import AsyncClient                                                  
from starlette.requests import Request                                         
from aiologger.logger import Logger                                            
                                                                               
                                                                               
logger = Logger.with_default_handlers(name='my-logger')                        
                                                                               
                                                                               
app = FastAPI()                                                                
router = APIRouter()                                                           
                                                                               
                                                                               
@router.get('/')                                                               
async def homepage(request: Request):                                          
    await logger.info('Rendering the homepage')                                
    return {"message": "Hello World"}                                          
                                                                               
                                                                               
app.include_router(router)                                                     
                                                                               
                                                                               
                                                                               
@pytest.mark.asyncio                                                           
async def test_homepage():                                                     
    async with AsyncClient(app=app, base_url='http://my-page/') as client:     
        response = await client.get('/')                                       
    assert response.json()['message'] == 'Hello World'

After running the test we get:

FAILED test.py::test_homepage - ValueError: Pipe transport is only for pipes, sockets and character devices

If using pytest with -s switch, this bug does not appear. Therefore it is an incompatibility between pytest stdout capturing and the way aiologger is sending logs to stdout. I’m not sure, which side should handle this problem, therefore I post the issue on both projects.

@RonnyPfannschmidt
Copy link
Member

aiologger unconditionally creates stream/default handlers and has no own pytest integration taking care of redirection, object replacement or closing

thus it ends up with the stdio redirection breaking the objects it fetched

this is the reason why the general logging suggestion for any lib/application is to set up logging handlers at a managed place in application start-up instead of randomly at a module global logger creation

i consider that a bug solely in aiologger, as far as i can tell its suggested standard usage is a anti-pattern

@RonnyPfannschmidt
Copy link
Member

#5743 is related in the sense that pytest could have better exceptions for the stream types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants