Doctests that contain string literals

后端 未结 1 731
走了就别回头了
走了就别回头了 2021-02-10 23:10

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

相关标签:
1条回答
  • 2021-02-11 00:00

    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')
    
    0 讨论(0)
提交回复
热议问题