testing.rst 2.7 KB

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