To test actors in a setting as close to production as possible, a typical pattern is the following:
- In the test setup, start an actor together with any actors/collaborators it depends on. The dependencies will often be replaced by mocks to control their behavior.
- In the test, :meth:`~pykka.ActorRef.ask` or :meth:`~pykka.ActorRef.tell` the actor something.
- In the test, assert on the actor's state or the return value from the :meth:`~pykka.ActorRef.ask`.
- In the test teardown, stop the actor to properly clean up before the next test.
Let's look at an example actor that we want to test:
.. literalinclude:: ../examples/producer.py
We can test this actor with pytest by mocking the consumer and asserting that it receives a newly produced item:
.. literalinclude:: ../examples/producer_test.py
If this way of setting up and tearing down test resources is unfamiliar to you, it is strongly recommended to read up on pytest's great fixture feature.