Issue #52 resolved

doctest.testfile() confuses source detection

Julian O
created an issue

The doctest.testmod() method searches the current file for comments of a specific format, and runs them as a test. When I use that, coverage.py does an excellent job.

The doctest.testfile() method searches another file for the comments, and runs them as a test. When I use that, coverage.py complains that it is unable to find the source code.

I did not expect the .doctest file to give me a percentage coverage, but I did hope it would be able to understand the format of the filename, and to handle it gracefully.

Note: This is an different issue to <<issue 19>>.

Here is an example:

  • File 1: factorial_testfile.py

{{{ def factorial(n): """ Compute the factorial of a natural number."""

if n == 0:
    return 1
else:
    return n * factorial(n-1)

if name == "main": import doctest doctest.testfile("factorial.doctest") }}}

  • File 2: factorial.doctest

{{{

from factorial_testfile import factorial factorial(0) 1 factorial(1) 1 factorial(3) 6 }}}

Here is what happens (under Windows, Python 2.6).

{{{ C:\temp\coveragetest>coverage run factorial_testfile.py

C:\temp\coveragetest>coverage report -m Name Stmts Exec Cover Missing


<doctest factorial NoSource: No source for code: 'c:\temp\coveragetest\<doctest factorial.doctest[0]>': [Errno 22] invalid mode ('rU') or filename: 'c:\temp\coveragetest\<doctest factorial.doctest[0]>' <doctest factorial NoSource: No source for code: 'c:\temp\coveragetest\<doctest factorial.doctest[3]>': [Errno 22] invalid mode ('rU') or filename: 'c:\temp\coveragetest\<doctest factorial.doctest[3]>' <doctest factorial NoSource: No source for code: 'c:\temp\coveragetest\<doctest factorial.doctest[1]>': [Errno 22] invalid mode ('rU') or filename: 'c:\temp\coveragetest\<doctest factorial.doctest[1]>' <doctest factorial NoSource: No source for code: 'c:\temp\coveragetest\<doctest factorial.doctest[2]>': [Errno 22] invalid mode ('rU') or filename: 'c:\temp\coveragetest\<doctest factorial.doctest[2]>' factorial_testfile 7 7 100% }}}

Comments (1)

  1. Log in to comment