Aiologger incompatible with pytest
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.
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
Is there a workaround to make the tests work successfully without the -s flag?
Is there any solution for this?