doctest

Is it possible to test a function that uses get_type_hints with a doctest?

时光毁灭记忆、已成空白 提交于 2019-12-08 12:37:22
问题 I have a function that uses typing.get_type_hints. I want to add a documentation test to it. However, it looks like get_type_hints fails to resolve types that are defined in a doctest. Here is a simplified example: import typing def f(clazz): """ >>> class MyClass: ... my_field: 'MyClass' >>> f(MyClass) """ typing.get_type_hints(clazz) When running it with python3 -m doctest test.py it throws NameError: name 'MyClass' is not defined . 回答1: from __future__ import annotations import typing def

Test exception chaining and traceback output with doctest

拟墨画扇 提交于 2019-12-08 06:17:27
问题 How to test a 'multi traceback' using doctest? It seems that using several ELLIPSIS and <BLANKLINE> won't do the trick: def myfunc(): """ >>> myfunc() Traceback (most recent call last): ... ValueError: this is <BLANKLINE> The above exception was the direct cause of the following exception: <BLANKLINE> Traceback (most recent call last): ... TypeError: it """ try: raise ValueError('this is') except ValueError as err: raise TypeError('it') from err import doctest doctest.testmod(optionflags

Doctest returning failure, yet “Expected” and “Got” match perfectly

我只是一个虾纸丫 提交于 2019-12-07 09:23:09
问题 I'm trying to do the second exercise in the Lists section of the book "How to Think Like a Computer Scientist". I basically have to match the given "doctest" with a program of my own that returns no error. I tried several ways, but despite the fact that the "Got" matches the "Expected" perfectly, it keeps giving me 1 failure. I already saw one question here that asked "How can python 2 doctest fail and yet have no difference in values in the failure message?" I tried a few of the solutions

Python: How can I define a class in a doctest?

笑着哭i 提交于 2019-12-06 19:56:38
问题 I would like to use a doctest comment block to demonstrate the usage of a particular base class, but either this cannot be done with doctest or I am doing something wrong. Here is my simple demo code. class MyClass(object): ''' >>> m = MyClass() >>> print m.x 1 >>> class A(MyClass): >>> def __init__(self): >>> super(A,self).__init__() >>> >>> a = A() >>> print a.x 1 ''' def __init__(self): self.x = 1 if __name__ == "__main__": import doctest doctest.testmod() The code doesn't run. Here's the

Python doctests: test for None

故事扮演 提交于 2019-12-06 18:18:28
问题 Using Python 2.7 I'm trying to test that the result of a particular function call is None I would expect these tests to pass (excuse the rather silly example) def six_or_none(val): """ >>> six_or_none(6) 6 >>> six_or_none(4) None """ if val == 6: return 6 return None However they yield the following result Failed example: six_or_none(4) Expected: None Got nothing What's the correct way to test for None in doctests? 回答1: The Python interpreter ignores None return values, so doctests do the

Why could doctests raise a NameError when run with Sphinx's `make doctest`?

北城余情 提交于 2019-12-06 07:47:38
问题 I have a simple function with a doctest, which, when run with Sphinx's make doctest , gives me the following error: File "scheemey.rst", line ?, in default Failed example: verify_balanced('asdf (foo [bar] [[baz], {}, ()]') Exception raised: Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1315, in __run compileflags, 1) in test.globs File "<doctest default[0]>", line 1, in <module> verify_balanced(

use doctest and logging in python program

我只是一个虾纸丫 提交于 2019-12-06 05:38:41
问题 #!/usr/bin/python2.4 import logging import sys import doctest def foo(x): """ >>> foo (0) 0 """ print ("%d" %(x)) _logger.debug("%d" %(x)) def _test(): doctest.testmod() _logger = logging.getLogger() _logger.setLevel(logging.DEBUG) _formatter = logging.Formatter('%(message)s') _handler = logging.StreamHandler(sys.stdout) _handler.setFormatter(_formatter) _logger.addHandler(_handler) _test() I would like to use logger module for all of my print statements. I have looked at the first 50 top

Doctest not recognizing __future__.division

假装没事ソ 提交于 2019-12-05 20:58:11
问题 I have the following doctest written x.doctest : This is something: >>> x = 3 + 4 foo bar something else: >>> from __future__ import division >>> y = 15 >>> z = int('24') >>> m = z / y >>> print (m) 1.6 But when I ran python -m doctest x.doctest on python 2.7.11, the doctest didn't recognize from __future__ import division : ********************************************************************** File "x.doctest", line 11, in x.doctest Failed example: print (m) Expected: 1.6 Got: 1 ************

django doctests not being run

流过昼夜 提交于 2019-12-05 12:44:25
I'm having a problem running django doctests with django-nose. Unit tests added to a /tests directory are running fine, but doctests are not. I am trying to run doctests on my "season" module: python manage.py test season and get this output: nosetests --verbosity 1 season --with-doctest Creating test database for alias 'default'... ---------------------------------------------------------------------- Ran 0 tests in 0.001s OK Destroying test database for alias 'default'... I'm just trying a basic doctest to try to get this to work, e.g.: """ >>> 1+1 == 2 True """ This is in my models.py. I've

How come there's no C# equivalent of python's doctest feature?

喜欢而已 提交于 2019-12-05 11:05:45
Seems like it would be a good way to introduce some people to unit testing. Well for one thing, the documentation for doctest talks about "interactive Python sessions". There's no equivalent of that in C#... so how would the output be represented? How would you perform all the necessary setup? I dare say such a thing would be possible, but personally I think that at least for C#, it's clearer to have unit tests as unit tests , where you have all the benefits of the fact that you're writing code rather than comments. The code can be checked for syntactic correctness at compile-time, you have