aiologger icon indicating copy to clipboard operation
aiologger copied to clipboard

Aiologger incompatible with pytest

Open zefciu opened this issue 4 years ago • 3 comments

To reproduce this bug, 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 pytest we get:

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

Note that if running pytest with the -s switch, the test passes. The issue is therefore with the pytest stdout capturing being incompatible with aiologger’s stream handler.

Because I’m not 100% sure, which side should really handle the issue, I will post the same ticket on both pytest and aiologger page.

zefciu avatar Sep 23 '21 09:09 zefciu

from the https://github.com/pytest-dev/pytest/issues/9119 i take the stance that aiologgewr is reponsible and uses a logging setup antipattern (which is to set up handlers at module/library level instead of the application startup

RonnyPfannschmidt avatar Sep 25 '21 11:09 RonnyPfannschmidt

Is there a workaround to make the tests work successfully without the -s flag?

19as avatar Aug 10 '22 09:08 19as

Is there any solution for this?

agronick avatar Aug 29 '22 20:08 agronick