eliot
eliot copied to clipboard
There are syntax errors and typos in the "Unit Testing Your Logging" documentation
Here are some examples.
diff --git a/docs/source/types-testing.rst b/docs/source/types-testing.rst
index 517c85d..9c68add 100644
--- a/docs/source/types-testing.rst
+++ b/docs/source/types-testing.rst
@@ -15,27 +15,24 @@ Let's unit test some code that relies on the ``LOG_USER_REGISTRATION`` object we
from myapp.logtypes import LOG_USER_REGISTRATION
- class UserRegistration(object):
+ class UserRegistration(object):
def __init__(self):
self.db = {}
def register(self, username, password, age):
self.db[username] = (password, age)
LOG_USER_REGISTRATION(
- username=username, password=password, age=age).write()
-
+ username=username, age=age).write()
Here's how we'd test it:
.. code-block:: python
from unittest import TestCase
- from eliot import MemoryLogger
from eliot.testing import assertContainsFields, capture_logging
from myapp.registration import UserRegistration
- from myapp.logtypes import LOG_USER_REGISTRATION
class LoggingTests(TestCase):
@@ -47,8 +44,7 @@ Here's how we'd test it:
msg = logger.messages[0]
assertContainsFields(self, msg,
{u"username": u"john",
- u"password": u"password",
- u"age": 12}))
+ u"age": 12})
@capture_logging(assertRegistrationLogging)
def test_registration(self, logger):
@@ -57,7 +53,7 @@ Here's how we'd test it:
"""
registry = UserRegistration()
registry.register(u"john", u"password", 12)
- self.assertEqual(registry.db[u"john"], (u"passsword", 12))
+ self.assertEqual(registry.db[u"john"], (u"password", 12))
Besides calling the given validation function the ``@capture_logging`` decorator will also validate the logged messages after the test is done.
@@ -77,18 +73,28 @@ The result will be a list of traceback message dictionaries for the particular e
.. code-block:: python
from unittest import TestCase
+ from eliot import write_traceback
from eliot.testing import capture_logging
+
+ class MyThing(object):
+ def load(self, path):
+ try:
+ return open(path)
+ except:
+ write_traceback(logger=None)
+
+
class MyTests(TestCase):
def assertMythingBadPathLogging(self, logger):
- messages = logger.flush_tracebacks(OSError)
+ messages = logger.flush_tracebacks(IOError)
self.assertEqual(len(messages), 1)
@capture_logging(assertMythingBadPathLogging)
def test_mythingBadPath(self, logger):
- mything = MyThing()
- # Trigger an error that will cause a OSError traceback to be logged:
- self.assertFalse(mything.load("/nonexistent/path"))
+ mything = MyThing()
+ # Trigger an error that will cause a IOError traceback to be logged:
+ self.assertFalse(mything.load("/nonexistent/path"))
@@ -100,12 +106,16 @@ The simplest method is using the ``assertHasMessage`` utility function which ass
.. code-block:: python
+ from unittest import TestCase
from eliot.testing import assertHasMessage, capture_logging
+ from myapp.logtypes import LOG_USER_REGISTRATION
+ from myapp.registration import UserRegistration
+
+
class LoggingTests(TestCase):
@capture_logging(assertHasMessage, LOG_USER_REGISTRATION,
{u"username": u"john",
- u"password": u"password",
u"age": 12})
def test_registration(self, logger):
"""
@@ -113,7 +123,7 @@ The simplest method is using the ``assertHasMessage`` utility function which ass
"""
registry = UserRegistration()
registry.register(u"john", u"password", 12)
- self.assertEqual(registry.db[u"john"], (u"passsword", 12))
+ self.assertEqual(registry.db[u"john"], (u"password", 12))
``assertHasMessage`` returns the found message and can therefore be used within more complex assertions. ``assertHasAction`` provides similar functionality for actions (see example below).
@@ -125,18 +135,25 @@ For example, we could rewrite the registration logging test above like so:
.. code-block:: python
- from eliot.testing import LoggedMessage, capture_logging
+ from unittest import TestCase
+ from eliot.testing import assertContainsFields, capture_logging, LoggedMessage
+
+ from myapp.logtypes import LOG_USER_REGISTRATION
+ from myapp.registration import UserRegistration
+
class LoggingTests(TestCase):
def assertRegistrationLogging(self, logger):
"""
Logging assertions for test_registration.
"""
- logged = LoggedMessage.of_type(logger.messages, LOG_USER_REGISTRATION)[0]
+ logged = LoggedMessage.of_type(
+ logger.messages,
+ LOG_USER_REGISTRATION
+ )[0]
assertContainsFields(self, logged.message,
{u"username": u"john",
- u"password": u"password",
- u"age": 12}))
+ u"age": 12})
@capture_logging(assertRegistrationLogging)
def test_registration(self, logger):
@@ -145,7 +162,7 @@ For example, we could rewrite the registration logging test above like so:
"""
registry = UserRegistration()
registry.register(u"john", u"password", 12)
- self.assertEqual(registry.db[u"john"], (u"passsword", 12))
+ self.assertEqual(registry.db[u"john"], (u"password", 12))
Similarly, ``LoggedAction.of_type`` finds all logged actions of a specific ``ActionType``.