conftest.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # (c) Nelen & Schuurmans
  2. import asyncio
  3. import multiprocessing
  4. import os
  5. import pytest
  6. import uvicorn
  7. def pytest_sessionstart(session):
  8. """
  9. Called after the Session object has been created and
  10. before performing collection and entering the run test loop.
  11. """
  12. if os.environ.get("DEBUG") or os.environ.get("DEBUG_WAIT_FOR_CLIENT"):
  13. from clean_python.testing.debugger import setup_debugger
  14. setup_debugger()
  15. @pytest.fixture(scope="session")
  16. def event_loop(request):
  17. """Create an instance of the default event loop per test session.
  18. Async fixtures need the event loop, and so must have the same or narrower scope than
  19. the event_loop fixture. Since we have async session-scoped fixtures, the default
  20. event_loop fixture, which has function scope, cannot be used. See:
  21. https://github.com/pytest-dev/pytest-asyncio#async-fixtures
  22. """
  23. loop = asyncio.get_event_loop_policy().new_event_loop()
  24. yield loop
  25. loop.close()
  26. @pytest.fixture(scope="session")
  27. async def postgres_url():
  28. return os.environ.get("POSTGRES_URL", "postgres:postgres@localhost:5432")
  29. @pytest.fixture(scope="session")
  30. async def s3_url():
  31. return os.environ.get("S3_URL", "http://localhost:9000")
  32. @pytest.fixture(scope="session")
  33. async def fastapi_example_app():
  34. port = int(os.environ.get("API_PORT", "8005"))
  35. config = uvicorn.Config("fastapi_example:app", host="0.0.0.0", port=port)
  36. p = multiprocessing.Process(target=uvicorn.Server(config).run)
  37. p.start()
  38. yield f"http://localhost:{port}"
  39. p.terminate()