File: testing.rst

package info (click to toggle)
python-pykka 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 520 kB
  • sloc: python: 2,817; makefile: 113
file content (38 lines) | stat: -rw-r--r-- 1,244 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
=======
Testing
=======

Pykka actors can be tested using the regular Python
testing tools like `pytest <https://docs.pytest.org/>`_,
:mod:`unittest`, and :mod:`unittest.mock`.

To test actors in a setting as close to production as possible,
a typical pattern is the following:

1. 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.
2. In the test,
   :meth:`~pykka.ActorRef.ask` or :meth:`~pykka.ActorRef.tell`
   the actor something.
3. In the test,
   assert on the actor's state
   or the return value from the :meth:`~pykka.ActorRef.ask`.
4. In the test teardown,
   stop the actor to properly clean up before the next test.

An example
==========

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 <https://docs.pytest.org/en/latest/fixture.html>`_ feature.