Skip to content
Advertisement

How to test a Faust agent that sends data to a sink?

I am trying to write unit tests using pytest for my Faust application. I have referred to the documentation here but it does not mention what to do when my Faust agent is sending data to a sink.

Without a sink, my tests are working fine but when I use a sink, I get this error:

JavaScript

I have tried various methods: such as patching out the decorator in my Faust app that sends the data to the sink, trying to test my function without the decorator (by trying to bypass it), patching out the sink parameter in my Faust app to have a None value (so it doesn’t send my data to the sink), etc. I have had no luck with any of these.

Here is my Faust agent:

JavaScript

Here is my current testing code:

JavaScript

I get the same error as mentioned above when I run this test. Is there any way to test my Faust app successfully?

Thank you!

Advertisement

Answer

Force pytest to use Faust’s asyncio event loop as the default global loop. Add the following fixture to your test code:

JavaScript

As described in the pytest documentation:

The event_loop fixture can be easily overridden in any of the standard pytest locations (e.g. directly in the test file, or in conftest.py) to use a non-default event loop. If the pytest.mark.asyncio marker is applied, a pytest hook will ensure the produced loop is set as the default global loop.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement