doctest

Can python doctest ignore some output lines?

余生颓废 提交于 2019-11-27 02:37:43
问题 I'd like to write a doctest like this: """ >>> print a.string() foo : a bar : b date : <I don't care about the date output> baz : c """ Is there any way to do this? I think it would make more sense to switch to unittest, but I'm curious whether it's possible to specify a range of output that shouldn't be matched for the test in doctest. Thanks! 回答1: With doctest.ELLIPSIS , you can use ... to mean "match any string here". You can set doctest options with a doctest directive, to make it active

How do I test dictionary-equality with Python's doctest-package?

南笙酒味 提交于 2019-11-27 02:02:45
问题 I'm writing a doctest for a function that outputs a dictionary. The doctest looks like >>> my_function() {'this': 'is', 'a': 'dictionary'} When I run it, it fails with Expected: {'this': 'is', 'a': 'dictionary'} Got: {'a': 'dictionary', 'this': 'is'} My best guess as to the cause of this failure is that doctest isn't checking dictionary equality, but __repr__ equality. This post indicates that there's some way to trick doctest into checking dictionary equality. How can I do this? 回答1: Doctest

Auto generate doctest output with Sphinx extension

青春壹個敷衍的年華 提交于 2019-11-26 21:49:32
问题 I think I am missing something about the sphinx extension for doctest. The typical example in the documentation is: .. doctest:: >>> print 1 1 Isn't there a way to let sphinx generate the output (here: 1 ) automatically? As far as I understood, it is possible to run: $ make doctest which has the effect to test the code snippets, and compare the real output with the expected output. For example, if you have .. doctest:: >>> print 1 3 doctest will warn you that it got 1 while it was expecting 3