doctest

Wrapping python doctest results that are longer than 80 characters

不问归期 提交于 2019-11-29 06:00:34
问题 I'm trying to keep my source code under the 80 character guideline width that PEP8 recommends, but can't figure out how to wrap my doctest which has results longer than 80 characters. A noddy example: def long_string(): """ Returns a string which is wider than the recommended PEP8 linewidth >>> print long_string() 0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 """ return '0123456789' * 10 I've tried a couple of combinations, including

Django doctests in views.py

梦想与她 提交于 2019-11-29 05:29:43
The Django 1.4 documentation on tests states: For a given Django application, the test runner looks for doctests in two places: The models.py file. You can define module-level doctests and/or a doctest for individual models. It's common practice to put application-level doctests in the module docstring and model-level doctests in the model docstrings. A file called tests.py in the application directory -- i.e., the directory that holds models.py. This file is a hook for any and all doctests you want to write that aren't necessarily related to models. Out of curiosity I'd like to know why

Python - doctest vs. unittest [closed]

倾然丶 夕夏残阳落幕 提交于 2019-11-28 15:07:52
I'm trying to get started with unit testing in Python and I was wondering if someone could explain the advantages and disadvantages of doctest and unittest. What conditions would you use each for? Brian Both are valuable. I use both doctest and nose taking the place of unittest. I use doctest for cases where the test is giving an example of usage that is actually useful as documentation. Generally I don't make these tests comprehensive, aiming solely for informative. I'm effectively using doctest in reverse: not to test my code is correct based on my doctest, but to check that my documentation

Can python doctest ignore some output lines?

穿精又带淫゛_ 提交于 2019-11-28 08:57:09
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! 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 for just one test case: one example in the online docs is: >>> print range(20) # doctest:+ELLIPSIS [0, 1, ..

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

邮差的信 提交于 2019-11-28 08:02:21
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? Claudiu Doctest doesn't check __repr__ equality, per se, it just checks that the output is exactly the same. You

Auto generate doctest output with Sphinx extension

半世苍凉 提交于 2019-11-28 01:05:35
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 . Instead, I would like sphinx to insert the real output alone in my docstring or in my .rst file. For

Django doctests in views.py

南楼画角 提交于 2019-11-27 23:21:41
问题 The Django 1.4 documentation on tests states: For a given Django application, the test runner looks for doctests in two places: The models.py file. You can define module-level doctests and/or a doctest for individual models. It's common practice to put application-level doctests in the module docstring and model-level doctests in the model docstrings. A file called tests.py in the application directory -- i.e., the directory that holds models.py. This file is a hook for any and all doctests

Mocking ImportError in Python

若如初见. 提交于 2019-11-27 23:12:49
I'm trying this for almost two hours now, without any luck. I have a module that looks like this: try: from zope.component import queryUtility # and things like this except ImportError: # do some fallback operations <-- how to test this? Later in the code: try: queryUtility(foo) except NameError: # do some fallback actions <-- this one is easy with mocking # zope.component.queryUtility to raise a NameError Any ideas? EDIT: Alex's suggestion doesn't seem to work: >>> import __builtin__ >>> realimport = __builtin__.__import__ >>> def fakeimport(name, *args, **kw): ... if name == 'zope.component'

Python - doctest vs. unittest [closed]

心已入冬 提交于 2019-11-27 09:01:06
问题 I'm trying to get started with unit testing in Python and I was wondering if someone could explain the advantages and disadvantages of doctest and unittest. What conditions would you use each for? 回答1: Both are valuable. I use both doctest and nose taking the place of unittest. I use doctest for cases where the test is giving an example of usage that is actually useful as documentation. Generally I don't make these tests comprehensive, aiming solely for informative. I'm effectively using

Multi version support for Python doctests

梦想与她 提交于 2019-11-27 02:48:05
问题 I am writing my doctests like this: >>> some_function(a=1, b=2) {u'id': u'123', u'name': u'abc'} This works fine for Python version 2.5, 2.6 & 2.7 but fails for Python 3 with following error: Expected: {u'id': u'123', u'name': u'abc'} Got: {'id': '123', 'name': 'abc'} Problem is that if I write my doctests like this: >>> some_function(a=1, b=2) {'id': '123', 'name': 'abc'} They will work only for Python3 and fail on Python2 version. My question is how do I make it cross version compatible?