In most cases you are testing your subscriber and publisher functions, but sometimes you need to trigger some lifespan hooks in your tests too.
For this reason, FastStream has a special TestApp patcher working as a regular async context manager.
1 2 3 4 5 6 7 8 910111213141516171819202122
importpytestfromfaststreamimportFastStream,TestAppfromfaststream.kafkaimportKafkaBroker,TestKafkaBrokerbroker=KafkaBroker()app=FastStream(broker)@app.after_startupasyncdefhandle():print("Calls in tests too!")@pytest.mark.asyncioasyncdeftest_lifespan():asyncwith(TestKafkaBroker(broker,connect_only=True),TestApp(app),):# test somethingpass
1 2 3 4 5 6 7 8 910111213141516171819202122
importpytestfromfaststreamimportFastStream,TestAppfromfaststream.confluentimportKafkaBroker,TestKafkaBrokerbroker=KafkaBroker()app=FastStream(broker)@app.after_startupasyncdefhandle():print("Calls in tests too!")@pytest.mark.asyncioasyncdeftest_lifespan():asyncwith(TestKafkaBroker(broker,connect_only=True),TestApp(app),):# test somethingpass
1 2 3 4 5 6 7 8 910111213141516171819202122
importpytestfromfaststreamimportFastStream,TestAppfromfaststream.rabbitimportRabbitBroker,TestRabbitBrokerbroker=RabbitBroker()app=FastStream(broker)@app.after_startupasyncdefhandle():print("Calls in tests too!")@pytest.mark.asyncioasyncdeftest_lifespan():asyncwith(TestRabbitBroker(broker,connect_only=True),TestApp(app),):# test somethingpass
1 2 3 4 5 6 7 8 910111213141516171819202122
importpytestfromfaststreamimportFastStream,TestAppfromfaststream.natsimportNatsBroker,TestNatsBrokerbroker=NatsBroker()app=FastStream(broker)@app.after_startupasyncdefhandle():print("Calls in tests too!")@pytest.mark.asyncioasyncdeftest_lifespan():asyncwith(TestNatsBroker(broker,connect_only=True),TestApp(app),):# test somethingpass
1 2 3 4 5 6 7 8 910111213141516171819202122
importpytestfromfaststreamimportFastStream,TestAppfromfaststream.redisimportRedisBroker,TestRedisBrokerbroker=RedisBroker()app=FastStream(broker)@app.after_startupasyncdefhandle():print("Calls in tests too!")@pytest.mark.asyncioasyncdeftest_lifespan():asyncwith(TestRedisBroker(broker,connect_only=True),TestApp(app),):# test somethingpass
1 2 3 4 5 6 7 8 910111213141516171819202122
importpytestfromfaststreamimportFastStream,TestAppfromfaststream.mqttimportMQTTBroker,TestMQTTBrokerbroker=MQTTBroker()app=FastStream(broker)@app.after_startupasyncdefhandle():print("Calls in tests too!")@pytest.mark.asyncioasyncdeftest_lifespan():asyncwith(TestMQTTBroker(broker,connect_only=True),TestApp(app),):# test somethingpass
If you want to use an In-Memory patched broker in your tests, it's advised to patch the broker first (before applying the application patch).
Also, TestApp and TestBroker are both calling broker.start(). According to the original logic, the broker should be started in the FastStream application, but if TestBroker is applied first – it breaks this behavior. For this reason, TestApp prevents TestBrokerbroker.start() call if it is placed inside the TestBroker context.
This behavior is controlled by the connect_only argument to TestBroker. While None is the default value, TestApp can set it to True or False during the code execution. If the connect_only argument is provided manually, it would not be changed.
Warning
With connect_only=False, all FastStream hooks will be called after the broker start, which can break some @app.on_startup logic.