Coder Social home page Coder Social logo

Comments (3)

euri10 avatar euri10 commented on July 25, 2024 1

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.

florimondmanca avatar florimondmanca commented on July 25, 2024

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 and client fixtures into a single client 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.

euri10 avatar euri10 commented on July 25, 2024

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 and client fixtures into a single client 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.