doctest.testfile() confuses source detection

Julian O avatarJulian 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 #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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.