You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/generating/testing.rst
+29Lines changed: 29 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -233,6 +233,35 @@ Or we can simplify further by using ``assertHasMessage`` and ``assertHasAction``
233
233
self.assertEqual(servers, [msg.message["server"] for msg in messages])
234
234
235
235
236
+
Custom JSON encoding
237
+
--------------------
238
+
239
+
Just like a ``FileDestination`` can have a custom JSON encoder, so can your tests, so you can validate your messages with that JSON encoder:
240
+
241
+
.. code-block:: python
242
+
243
+
from unittest import TestCase
244
+
from eliot.json import EliotJSONEncoder
245
+
from eliot.testing import capture_logging
246
+
247
+
classMyClass:
248
+
def__init__(self, x):
249
+
self.x = x
250
+
251
+
classMyEncoder(EliotJSONEncoder):
252
+
defdefault(self, obj):
253
+
ifisinstance(obj, MyClass):
254
+
return {"x": obj.x}
255
+
return EliotJSONEncoder.default(self, obj)
256
+
257
+
classLoggingTests(TestCase):
258
+
@capture_logging(None, encoder_=MyEncoder)
259
+
deftest_logging(self, logger):
260
+
# Logged messages will be validated using MyEncoder....
261
+
...
262
+
263
+
Notice that the hyphen after ``encoder_`` is deliberate: by default keyword arguments are passed to the assertion function (the first argument to ``@capture_logging``) so it's marked this way to indicate it's part of Eliot's API.
Copy file name to clipboardExpand all lines: docs/source/news.rst
+12Lines changed: 12 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,18 @@
1
1
What's New
2
2
==========
3
3
4
+
1.13.0
5
+
^^^^^^
6
+
7
+
Features:
8
+
9
+
* ``@capture_logging`` and ``MemoryLogger`` now support specifying a custom JSON encoder. By default they now use Eliot's encoder. This means tests can now match the encoding used by a ``FileDestination``.
0 commit comments