Comments (3)
I switched all my tests cases to httpx Asynclient and use the asgi-lifespan, it's great.
I dont know how others are operating but most of the time I'm using relative urls in my tests, so in the example above would be something like:
15c15
< async with httpx.AsyncClient(app=app) as client:
---
> async with httpx.AsyncClient(app=app, base_url"http://testserver") as client:
20c20,21
< r = await client.get("http://testserver")
---
> url = url_path_for("home")
> r = await client.get(url)
➜ ~
from asgi-lifespan.
Glad to know you’ve been using this library with HTTPX! I’ve also been using the base_url
pattern, very handy. (Though I’m not sure it’s particularly related to asgi-lifespan
.)
The example pytest setup in the issue description is a bit outdated, now...
- Drop the decorators on the example app, as encouraged with Starlette 0.13+.
- Register lifespan event handles on the app, and include the pytest output to show when handlers are running (ie when preparing the
app
fixture). - We could actually merge the
app
andclient
fixtures into a singleclient
fixture, as in general we don’t need to tap into the app itself when testing it as an “HTTP black box”.
from asgi-lifespan.
everything below is absolutely correct, yet ymmv @florimondmanca : some examples beow
- Drop the decorators on the example app, as encouraged with Starlette 0.13+.
in my case I'm using FastAPi which still has the decorator syntax
- We could actually merge the
app
andclient
fixtures into a singleclient
fixture, as in general we don’t need to tap into the app itself when testing it as an “HTTP black box”.
I need 2 different fixtures as the app in my case is built in a get_app
function passing some app_settings parameters, and I want to be able to test different settings. Maybe there's a better layout !
@pytest.fixture(scope="session")
async def app_test(monkeypatch_session: MonkeyPatch) -> AsyncIterator[FastAPI]:
logger.debug("app_test")
get_app_settings.cache_clear()
monkeypatch_session.setenv(
"APP_DEBUG", "True",
)
app_settings = get_app_settings()
app = get_app(app_settings)
logger.debug("yield app_test")
async with LifespanManager(app):
yield app
logger.debug("end yield app_test")
logger.debug("end app_test")
@pytest.fixture(scope="session")
async def client(app_test: FastAPI) -> AsyncIterator[AsyncClient]:
async with httpx.AsyncClient(app=app_test, base_url="http://testserver") as client:
yield client
from asgi-lifespan.
Related Issues (16)
- Usage examples should be tested
- Can't use LifespanManager with pytest-asyncio HOT 2
- Remove dependency on anyio?
- Switch to register-on-init style for Lifespan event handlers
- Enforcing top-level imports
- Should we get rid of `Lifespan` and `LifespanMiddleware`? HOT 8
- Implement lifespan manager
- Rationale for strong dependency on starlette 0.13? HOT 4
- Implement lifespan middleware
- Handling `lifespan.shutdown.failed` ASGI messages HOT 1
- [feature request] support ContextVars HOT 9
- Test fail on FreeBSD HOT 1
- Error reproducing an example from the REAME.md HOT 3
- Support for lifespan state HOT 10
- Implement lifespan app
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asgi-lifespan.