from dicttoxml2 import dicttoxml payload: dict = {'mylist': ['foo', 'bar', 'baz'], 'mydict': {'foo': 'bar', 'baz': 1}, 'ok': True} expected: bytes = b'' \ b'' \ b'' \ b'' \ b'' assert dicttoxml(payload, cdata=True) == expected