doctest

Configure Django to find all doctests in all modules?

纵然是瞬间 提交于 2019-12-03 12:12:05
If I run the following command: >python manage.py test Django looks at tests.py in my application, and runs any doctests or unit tests in that file. It also looks at the __ test __ dictionary for extra tests to run. So I can link doctests from other modules like so: #tests.py from myapp.module1 import _function1, _function2 __test__ = { "_function1": _function1, "_function2": _function2 } If I want to include more doctests, is there an easier way than enumerating them all in this dictionary? Ideally, I just want to have Django find all doctests in all modules in the myapp application. Is there

Is it possible to only test specific functions with doctest in a module?

ε祈祈猫儿з 提交于 2019-12-03 10:35:39
I am trying to get into testing in Python using the doctest module. At the moment I do Write the tests for the functions. implement the functions code. If Tests pass, write more tests and more code. When the function is done move on to the next function to implement. So after 3 or 4 (independent) functions in the same module with many tests I get a huge output by doctest. And it is a little annoysing. Is there a way to tell doctest "don't test functions a() , b() and c() ", so that it runs only the unmarked functions? I only found the doctest.SKIP flag, which is not sufficient for my needs. I

How do I run doctests with PyCharm?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 04:42:58
问题 In the PyCharm IDE, if I right-click on a function/method with a doctest, sometimes the right-click menu will give me the option: " Run 'Doctest my_function_name' " and sometimes the right-click menu, instead, only gives the option to run the whole file (NOT as a doctest). What determines when it will give the "run doctest" option and when it will not? Is there a way to force it one way or the other? 回答1: Running a module (or the tests in it) in PyCharm is done via a Run Configuration. When

How do I run doctests with PyCharm?

人走茶凉 提交于 2019-12-02 17:12:23
In the PyCharm IDE, if I right-click on a function/method with a doctest, sometimes the right-click menu will give me the option: " Run 'Doctest my_function_name' " and sometimes the right-click menu, instead, only gives the option to run the whole file (NOT as a doctest). What determines when it will give the "run doctest" option and when it will not? Is there a way to force it one way or the other? asherbar Running a module (or the tests in it) in PyCharm is done via a Run Configuration . When you right click a module, PyCharm searches for an existing Run Configuration for that module. If a

unicode_literals and doctest in Python 2.7 AND Python 3.5

本小妞迷上赌 提交于 2019-12-01 18:51:42
Consider the following demo script: # -*- coding: utf-8 -*- from __future__ import division from __future__ import unicode_literals def myDivi(): """ This is a small demo that just returns the output of a divison. >>> myDivi() 0.5 """ return 1/2 def myUnic(): """ This is a small demo that just returns a string. >>> myUnic() 'abc' """ return 'abc' if __name__ == "__main__": import doctest extraglobs = {} doctest.testmod(extraglobs=extraglobs) The doctest passes on Python 3.5, but fails on Python 2.7.9. The strange thing is, the divison test works, but the unicode test fails. I have seen various

Can I have an ellipsis at the beginning of the line in a Python doctest?

删除回忆录丶 提交于 2019-12-01 15:04:59
Python doctests are cool. Let me start with a simple example: def foo(): """ >>> foo() hello world """ print "hello world" Now let's assume some part is somewhat varying, e.g., because it is a time value or a random number. In general, doctests allow me to specify a wildcard saying using the +ELLIPSIS option. This works fine when for instance "world" is a varying string: def foo(): """ >>> foo() # doctest: +ELLIPSIS hello ... """ print "hello world" In my case however, the variable string is at the beginning of the line: def foo(): """ >>> foo() # doctest: +ELLIPSIS ... world """ print "hello

Python doctests and unicode

雨燕双飞 提交于 2019-12-01 06:25:42
I have a code base that runs unchanged in Python 2.7 and 3.2+. But the doctests in the documentation rst files are giving me a headache. When I run them in Python2, I get UnicodeEncodeError: 'ascii' codec can't encode character u'\xb2' in position 16: ordinal not in range(128) . If I add .. testsetup:: * from __future__ import unicode_literals then I get a lot of errors like Expected: 'something' Got: u'something' Is there a way to have doctest containing unicode characters in the rst files that work unchanged in Python 2.7 and 3.2+? Make sure you are using Python 3.3. It added the explicit u

Python doctests and unicode

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 04:12:50
问题 I have a code base that runs unchanged in Python 2.7 and 3.2+. But the doctests in the documentation rst files are giving me a headache. When I run them in Python2, I get UnicodeEncodeError: 'ascii' codec can't encode character u'\xb2' in position 16: ordinal not in range(128) . If I add .. testsetup:: * from __future__ import unicode_literals then I get a lot of errors like Expected: 'something' Got: u'something' Is there a way to have doctest containing unicode characters in the rst files

Python: using doctests for classes

筅森魡賤 提交于 2019-11-30 10:44:10
问题 Is it possible to use Python's doctest concept for classes, not just functions? If so, where shall I put the doctests - at the class' docstring, or at the constructor's docstring? To clarify, I'm looking for something like: class Test: """ >>> a=Test(5) >>> a.multiply_by_2() 10 """ def __init__(self, number): self._number=number def multiply_by_2(self): return self._number*2 Thanks in advance, Adam 回答1: You're missing the code to actually run the doctests at the bottom of the file: class Test

Wrapping python doctest results that are longer than 80 characters

自作多情 提交于 2019-11-30 05:39:35
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 using # doctest: +NORMALIZE_WHITESPACE and trying to simply wrap the line with a newline. Just figured out: