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

Bug: FastStream global context set is not access in pytest #1403

Closed
kusime opened this issue Apr 28, 2024 · 1 comment
Closed

Bug: FastStream global context set is not access in pytest #1403

kusime opened this issue Apr 28, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@kusime
Copy link

kusime commented Apr 28, 2024

Describe the bug

only run with faststream run main:app can avoid this bug , i think this cause by the pytest is not access the app . so there is no Context is injected. is there any way in pytest to access the Global Context ?

How to reproduce
Include source code:

app = FastStream(broker)

@app.on_startup
async def set_global(context: ContextRepo):
    context.set_global("secret_str", "my-perfect-secret")

@broker.subscriber("test")
async def vaea(
    msg: dict,
    secret_str: Annotated[str, Context("secret_str")],
):
    assert secret_str == "my-perfect-secret"
@pytest.mark.asyncio
async def test_dependency():
    async with TestRabbitBroker(broker, with_real=True) as br:
        # message can not be None
        await br.publish({}, queue="test")
        await vaea.wait_call(timeout=3)
        vaea.mock.assert_called_once_with({})

And/Or steps to reproduce the behavior:

  1. ...

Expected behavior
In pytest , the global context should accessable

Observed behavior
can not access the global context variable , and cause missing field error

Screenshots
image

Environment
Running FastStream 0.5.3 with CPython 3.11.0 on Windows

Additional context

@kusime kusime added the bug Something isn't working label Apr 28, 2024
@Lancetnik
Copy link
Collaborator

Please, take a look at this documentation section

It talks about how you can run and test your application lifespan hooks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants