Skip to content Skip to sidebar Skip to footer

Doctests That Contain String Literals

I have a unit test that I'd like to write for a function that takes XML as a string. It's a doctest and I'd like the XML in-line with the tests. Since the XML is multi-line, I tr

Solution 1:

This code works, e.g. with Python 2.7.12 and 3.5.2:

def test():
  """
  >>> config = '''<?xml version="1.0"?>
  ... <test>
  ...   <data>d1</data>
  ...   <data>d2</data>
  ... </test>'''
  >>> print(config)
  <?xml version="1.0"?>
  <test>
    <data>d1</data>
    <data>d2</data>
  </test>

  """

if __name__ == "__main__":
  import doctest
doctest.testmod(name='test')

Post a Comment for "Doctests That Contain String Literals"