| 1 |
.. _testing: |
| 2 |
|
| 3 |
======= |
| 4 |
Testing |
| 5 |
======= |
| 6 |
|
| 7 |
Kazoo has several test harnesses used internally for its own tests that are |
| 8 |
exposed as public API's for use in your own tests for common Zookeeper cluster |
| 9 |
management and session testing. They can be mixed in with your own `unittest` |
| 10 |
or `pytest` tests along with a `mock` object that allows you to force specific |
| 11 |
`KazooClient` commands to fail in various ways. |
| 12 |
|
| 13 |
The test harness needs to be able to find the Zookeeper Java libraries. You |
| 14 |
need to specify an environment variable called `ZOOKEEPER_PATH` and point it |
| 15 |
to their location, for example `/usr/share/java`. The directory should contain |
| 16 |
a `zookeeper-*.jar` and a `lib` directory containing at least a `log4j-*.jar`. |
| 17 |
|
| 18 |
If your Java setup is complex, you may also override our classpath mechanism |
| 19 |
completely by specifying an environment variable called `ZOOKEEPER_CLASSPATH`. |
| 20 |
If provided, it will be used unmodified as the Java classpath for Zookeeper. |
| 21 |
|
| 22 |
You can specify an optional `ZOOKEEPER_PORT_OFFSET` environment variable to |
| 23 |
influence the ports the cluster is using. By default the offset is 20000 and |
| 24 |
a cluster with three members will use ports 20000, 20010 and 20020. |
| 25 |
|
| 26 |
|
| 27 |
Kazoo Test Harness |
| 28 |
================== |
| 29 |
|
| 30 |
The :class:`~kazoo.testing.harness.KazooTestHarness` can be used directly or |
| 31 |
mixed in with your test code. |
| 32 |
|
| 33 |
Example: |
| 34 |
|
| 35 |
.. code-block:: python |
| 36 |
|
| 37 |
from kazoo.testing import KazooTestHarness |
| 38 |
|
| 39 |
class MyTest(KazooTestHarness): |
| 40 |
def setUp(self): |
| 41 |
self.setup_zookeeper() |
| 42 |
|
| 43 |
def tearDown(self): |
| 44 |
self.teardown_zookeeper() |
| 45 |
|
| 46 |
def testmycode(self): |
| 47 |
self.client.ensure_path('/test/path') |
| 48 |
result = self.client.get('/test/path') |
| 49 |
... |
| 50 |
|
| 51 |
|
| 52 |
Kazoo Test Case |
| 53 |
=============== |
| 54 |
|
| 55 |
The :class:`~kazoo.testing.harness.KazooTestCase` is complete test case that |
| 56 |
is equivalent to the mixin setup of |
| 57 |
:class:`~kazoo.testing.harness.KazooTestHarness`. An equivalent test to the |
| 58 |
one above: |
| 59 |
|
| 60 |
.. code-block:: python |
| 61 |
|
| 62 |
from kazoo.testing import KazooTestCase |
| 63 |
|
| 64 |
class MyTest(KazooTestCase): |
| 65 |
def testmycode(self): |
| 66 |
self.client.ensure_path('/test/path') |
| 67 |
result = self.client.get('/test/path') |
| 68 |
... |
| 69 |
|
| 70 |
Zake |
| 71 |
==== |
| 72 |
|
| 73 |
For those that do not need (or desire) to setup a Zookeeper cluster to test |
| 74 |
integration with kazoo there is also a library called |
| 75 |
`zake <https://pypi.python.org/pypi/zake/>`_. Contributions to |
| 76 |
`Zake's github repository <https://github.com/yahoo/Zake>`_ are welcome. |
| 77 |
|
| 78 |
Zake can be used to provide a *mock client* to layers of your application that |
| 79 |
interact with kazoo (using the same client interface) during testing to allow |
| 80 |
for introspection of what was stored, which watchers are active (and more) |
| 81 |
after your test of your application code has finished. |