doctest

Module doctest does not run

Module doctest does not run Question: I am trying to use the doctest module to test code. I tried this example: import doctest def areaTriangulo(base, altura): return ‘El area del triangulo es: ‘+str((base*altura)/2) """ funcion que nos devuelve el area de un triangulo >>> areaTriangulo(4,5) ‘El area del triangulo es: 20.0’ """ doctest.testmod() The test …

Total answers: 1

How do I add bytearrays to doctests?

How do I add bytearrays to doctests? Question: This is my function signature, def fun12(bytes_var: bytearray, signed: bool) -> (int): When I do, print(fun12(b’x00x10′,False)) My code works just fine, but when I have something like the following in my doctest comments, >>> fun12(b’x00x10′,False) 16 I get an error, Failed example: fun12(b”,False) Exception raised: Traceback (most …

Total answers: 2

How to run the doctest for a single function in python3?

How to run the doctest for a single function in python3? Question: How do I run the doctest for only a single function in python using the command line? I can python3 -m doctest -v main.py but this will run all the doctests in main.py. How do I specify one function to call the doctest …

Total answers: 2

Use Sphinx doctest with :Example:

Use Sphinx doctest with :Example: Question: Consider the following function: # in mymodule.py: def myfunc(num,mystring): “”” replicates a string :param num: a positive integer :param mystring: a string :return: string concatenated with itself num times :Example: >>> num = 3 >>> mystring = “lol” >>> myfunc(num, mystring) “lollollol” “”” return num*mystring How can I make …

Total answers: 4

How to get django's unittest TestLoader to find and run my doctests?

How to get django's unittest TestLoader to find and run my doctests? Question: In Django, my tests are a set of test_foo.py files inside my_django_app/tests/, which each contain a TestCase subclass, and which django automatically finds and runs. I have a bunch of utility modules with simple doctests that I would like to be included …

Total answers: 3

How do I run doctests with PyCharm?

How do I run doctests with PyCharm? Question: 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 …

Total answers: 3

Python doctests: test for None

Python doctests: test for None Question: 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 …

Total answers: 3

Python doctest with newline characters: inconsistent leading whitespace error

Python doctest with newline characters: inconsistent leading whitespace error Question: When writing python doctests, how does one properly introduce newline characters within a string in the test? Here’s a simple example: def remove_newlines(text): “”” >>> remove_newlines(“line1 n” … “still line 1r” … “now line2 n” … “more line2n”) line1 still line1 now line2 more line2 …

Total answers: 3

Custom PyCharm docstring stubs (i.e. for google docstring or numpydoc formats)

Custom PyCharm docstring stubs (i.e. for google docstring or numpydoc formats) Question: Does PyCharm 2.7 (or will PyCharm 3) have support for custom docstring and doctest stubs? If so, how does one go about writing this specific type of custom extension? My current project has standardized on using the Google Python Style Guide (http://google-styleguide.googlecode.com/svn/trunk/pyguide.html). I …

Total answers: 3