Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to run python doctest on a jupyter cell function?

There seems to be a package to enable this functionality, but I have no luck with it in python 3.5.2, or 2.7.12:

from ipython_doctester import test

@test
def my_fun():
    '''
    >>> 2 + 3
    6
    '''
    pass

TypeError: data must be a dict, got: 'ipython_doctester'

Is it possible to run a doctest from a jupyter cell using this package or some other way?

I've looked at %doctest_mode also, and I see that it turns Doctest mode off and on, but have been unable to run an actual doctest from a cell.

like image 520
Rich L Avatar asked Oct 19 '16 17:10

Rich L


People also ask

Can you run a Python script in Jupyter Notebook?

Jupyter Notebook (formerly known as IPython Notebook) is an interactive way of running Python code in the terminal using the REPL model (Read-Eval-Print-Loop).


2 Answers

Try this on Jupyter notebook:

def my_fun():
    '''
    >>> 2 + 3
    6
    '''
    pass

import doctest
doctest.testmod()

The result should be:

**********************************************************************
File "__main__", line 3, in __main__.my_fun
Failed example:
    2 + 3
Expected:
    6
Got:
    5
**********************************************************************
1 items had failures:
   1 of   1 in __main__.my_fun
***Test Failed*** 1 failures.
TestResults(failed=1, attempted=3)

(I used python 2.7.12)

like image 157
swatchai Avatar answered Sep 25 '22 12:09

swatchai


I keep hitting this page, but wanted to run a test for a single function. In that instance, the answer at https://stackoverflow.com/a/10081450/741316 helps. Namely:

def my_fun():
    '''
    >>> 2 + 3
    6
    '''
    pass

import doctest
doctest.run_docstring_examples(my_fun, globals())
like image 41
pelson Avatar answered Sep 21 '22 12:09

pelson