Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctest not running tests

Tags:

python

doctest

import doctest

def create_grid(size):
   grid = []
   for i in range(size):
       row = ['0']*size
       grid.append(row)
   """
   >>> create_grid(4)
   [['0', '0', '0', '0'], ['0', '0', '0', '0'],
    ['0', '0', '0', '0'], ['0', '0', '0', '0']]
   """
   return grid

if __name__ == '__main__':
    doctest.testmod()

Running the above with python Test_av_doctest.py -v gives the following message:

2 items had no tests:
    __main__
    __main__.create_grid
0 tests in 2 items.
0 passed and 0 failed.
Test passed.

Any idea why this error occurs?

like image 833
hamp Avatar asked Sep 16 '25 17:09

hamp


1 Answers

The issue is that your doctest-formatted string isn't a docstring.

Which docstrings are examined?

The module docstring, and all function, class and method docstrings are searched.

If you move the testing string below the function definition, it will become a function docstring, and thus will be targeted by doctest:

def create_grid(size):
   """
   >>> create_grid(4)
   [['0', '0', '0', '0'], ['0', '0', '0', '0'],
    ['0', '0', '0', '0'], ['0', '0', '0', '0']]
   """
   grid = []
   for i in range(size):
       row = ['0']*size
       grid.append(row)

   return grid

if __name__ == '__main__':
    doctest.testmod()

$ python Test_av_doctest.py -v
...
1 passed and 0 failed.
Test passed.
like image 70
David Cain Avatar answered Sep 18 '25 06:09

David Cain