hamp
hamp

Reputation: 131

Doctest not running tests

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?

Upvotes: 4

Views: 7560

Answers (1)

David Cain
David Cain

Reputation: 17333

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.

Upvotes: 5

Related Questions