Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make python's coverage library include doctests

From googling and experimentation, it seems python's coverage library doesn't include doctests in its calculations. Is there any way to make it do so?

I have searched the docs (https://coverage.readthedocs.io/en/coverage-4.4.1/) and found no mention of doctests, but it seems so odd that it wouldn't have some way of including them that I feel like I must be missing something.

If I'm right and coverage won't include them, how can I get a measurement of my test coverage without changing all my doctests into unit tests with unittest (which I don't want to do)?

like image 370
Katie Byers Avatar asked Oct 17 '25 03:10

Katie Byers


1 Answers

Two ways come to mind, either having the module import itself or loading the module from another module.

Have the module import itself

In file a.py:

def linear(a, b):
    ''' Solve ax + b = 0

        >>> linear(3, 5)
        -1.6666666666666667

    '''
    if a == 0 and b != 0:
        raise ValueError('No solutions')
    return -b  / a

if __name__ == '__main__':
    import doctest
    import a

    print(doctest.testmod(a))

At the command line:

$ coverage run a.py
$ coverage annotate
$ cat a.py,cover

This produces:

> def linear(a, b):
>     ''' Solve ax + b = 0

>         >>> linear(3, 5)
>         -1.6666666666666667

>     '''
>     if a == 0 and b != 0:
!         raise ValueError('No solutions')
>     return -b  / a

> if __name__ == '__main__':
>     import doctest
>     import a

>     print(doctest.testmod(a))

Run the tests from a separate module

Alternatively, you can move the imports and testmod() out of a.py and put them in a separate module.

In file b.py

import doctest
import a

print(doctest.testmod(a))

At the command line:

$ coverage run b.py
$ coverage annotate
$ cat a.py,cover

This produces:

> def linear(a, b):
>     ''' Solve ax + b = 0

>         >>> linear(3, 5)
>         -1.6666666666666667

>     '''
>     if a == 0 and b != 0:
!         raise ValueError('No solutions')
>     return -b  / a
like image 148
Raymond Hettinger Avatar answered Oct 19 '25 17:10

Raymond Hettinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!