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

Aiologger incompatible with pytest #8

Open
zefciu opened this issue Sep 23, 2021 · 3 comments
Open

Aiologger incompatible with pytest #8

zefciu opened this issue Sep 23, 2021 · 3 comments

Comments

@zefciu
Copy link

zefciu commented Sep 23, 2021

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.

@RonnyPfannschmidt
Copy link

from the pytest-dev/pytest#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

@19as
Copy link

19as commented Aug 10, 2022

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

@agronick
Copy link

Is there any solution for this?

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

4 participants