doctest

Is there way to only perform the doctests, ignoring print function calls?

时光总嘲笑我的痴心妄想 提交于 2019-12-11 03:52:54
问题 Hypothetically speaking, my function returns a value and has lot of print statements (maybe 100 or more). Is there a way to run doctest such that all the other printing work can be ignored/skipped (I am familiar with the +SKIP directive, which is for skipping doctest examples), i.e. when I execute my function (or run my module as a script) with doctest s: python mymodule.py Or: python -m doctest mymodule.py I should get: nothing, in case of success; or error messages in case any test example

Doctests: How to suppress/ignore output?

旧巷老猫 提交于 2019-12-11 01:32:57
问题 The doctest of the following (nonsense) Python module fails: """ >>> L = [] >>> if True: ... append_to(L) # XXX >>> L [1] """ def append_to(L): L.append(1) class A(object): pass return A() import doctest; doctest.testmod() This is because the output after the line marked XXX is <__main__.A object at ...> (which is returned by append_to ). Of course, I could put this output directly after the line marked XXX but in my case this would distract the reader from what shall be actually tested,

Python doctest: Skip entire block?

僤鯓⒐⒋嵵緔 提交于 2019-12-10 12:45:45
问题 I've got a Python module with docstrings in class methods, and a real-world example in the module docstring. The distinction is that the method-docstrings have been carefully crafted to be utterly repeatable tests, while the real-world example is just a copy'n'paste of the history from a Linux shell - which happened to invoke the python interpreter. E.g. """ Real-world example: # python2.5 Python 2.5 (release25-maint, Jul 20 2008, 20:47:25) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]

How can a python 2 doctest fail and yet have no difference in the values in the failure message?

狂风中的少年 提交于 2019-12-10 12:31:15
问题 I'm using Python 2.7.9 in Windows. I have a UTF-8-encoded python script file with the following contents: # coding=utf-8 def test_func(): u""" >>> test_func() u'☃' """ return u'☃' I get a curious failure when I run the doctest: Failed example: test_func() Expected: u'\u2603' Got: u'\u2603' I see this same failure output whether I launch the doctests through the IDE I usually use (IDEA IntelliJ), or from the command line: > x:\my_virtualenv\Scripts\python.exe -m doctest -v hello.py I copied

Python: accept unicode strings as regular strings in doctests

依然范特西╮ 提交于 2019-12-10 04:34:02
问题 Writing doctests for a method that abbreviates a dictionary by searching for a passed key word in the keys of the original dictionary, and returning the new, abbreviated dictionary. My docstring looks as follows: def abbreviate_dict(key_word, original_dict): """ >>> orig_dict = {apple_stems: 2, apple_cores: 5, apple_seeds: 3} >>> abbreviate_dict('apple', orig_dict) {'cores': 5, 'seeds': 3, 'stems': 2} """ etc. return new_dict The function works, but when I run py.test's doctest, the function

Why does nose finds tests in files with only 644 permission?

女生的网名这么多〃 提交于 2019-12-10 03:51:04
问题 Today I ran a bunch of doctests using Python 2.6 on a Ubuntu 9.10 with nose : nosetests --with-doctest Ran 0 tests in 0.001s OK WTF? I had tests in that files, why didn't that work? I changed permission to 644: sudo chmod 644 * -R nosetests --with-doctest Ran 11 test in 0.004s FAILED (errors=1) Changing it back to 777: sudo chmod 777 * -R nosetests --with-doctest Ran 0 tests in 0.001s OK Why is that? Using 644, I can't even edit my files! 回答1: Try the --exe flag: $ nosetests --help ... --exe

Does Python doctest remove the need for unit-tests?

南笙酒味 提交于 2019-12-10 01:21:27
问题 A fellow developer on a project I am on believes that doctests are as good as unit-tests, and that if a piece of code is doctested, it does not need to be unit-tested. I do not believe this to be the case. Can anyone provide some solid, ideally cited, examples either for or against the argument that doctests replace the need for unit-tests? Thank you -Daniel EDIT: Can anyone provide a reference showing that doctesting should not replace unit-testing? 回答1: I (ab)used doctest in lieu of

How can I include special characters (tab, newline) in a python doctest result string?

点点圈 提交于 2019-12-09 14:14:28
问题 Given the following python script: # dedupe.py import re def dedupe_whitespace(s,spacechars='\t '): """Merge repeated whitespace characters. Example: >>> dedupe_whitespace(r"Green\t\tGround") # doctest: +REPORT_NDIFF 'Green\tGround' """ for w in spacechars: s = re.sub(r"("+w+"+)", w, s) return s The function works as intended within the python interpreter: $ python >>> import dedupe >>> dedupe.dedupe_whitespace('Purple\t\tHaze') 'Purple\tHaze' >>> print dedupe.dedupe_whitespace('Blue\t\tSky')

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

亡梦爱人 提交于 2019-12-09 09:02:09
问题 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

How to test exceptions with doctest in Python 2.x and 3.x?

一笑奈何 提交于 2019-12-08 15:00:34
问题 I defined an exception class SpamException in a module spam . Now I want to test a function spam_function , that raises this exception. So I wrote the following doctest. >>> spam_function() Traceback (most recent call last): .... SpamException The test succeeds on Python 2.x, but on Python 3.x the test fails. The following test works on Python 3.x. >>> spam_function() Traceback (most recent call last): .... spam.SpamException The notable difference here is the inclusion of the module name in