Skip to content

Add note to docs about pytest-cov and pytest doctest support #101

Closed

Description

@floer32

A coworker of mine was shocked that the built-in pytest doctest support module didn't integrate with pytest-cov. He wrote a shim to get things working. I was shocked too: I am pretty sure things already work, if you use the arguments in the right combination.

Seems like the gist is just to make sure you have "--doctest-modules" and "--cov" arguments, in your command (or config file).

like ...

py.test foo --cov=foo --doctest-modules

(EDIT -- my previous command example WORKED but it was misleading as to what was going on. command should now be more clear.*)

I think he was forgetting --cov argument and confusing himself. A hint in the docs might help folks avoid this blunder going forward.

So maybe it would be good to just include a usage example like the above, in some docs somewhere. When you look for "doctest" in the pytest-cov docs you don't find anything. Maybe this little FAQ or footnote should be stuffed somewhere, so that searching the pytest-cov docs for "doctest" brings up a hint.

Happy to make a PR... just unclear where to stick this.

Activity

  1. floer32 commented on Dec 16, 2015

    @floer32
    Author

    Could be related: #98

  2. ionelmc commented on Dec 16, 2015

    @ionelmc
    Member

    Hey. So having something like "pytest-cov will work with doctests if they are in docstrings and --cov (or coverage configuration) matches the containing module"?

  3. ionelmc commented on Dec 16, 2015

    @ionelmc
    Member

    I'm not sure how it even works (maybe pytest does some fancypants trickery to exec the code). We should have some tests, just in case.

  4. floer32 commented on Dec 16, 2015

    @floer32
    Author

    Yes, something quick like you mentioned ... maybe just a bit more detailed or search-able:

    pytest-cov will work with pytest's built-in doctest support. --cov=<module> (or coverage configuration) should "just work" when you add the --doctest-modules argument. (If it does not, please submit an issue on GitHub and we'll try to get to the bottom of it.) However, your mileage may vary if you are trying to use the --doctest-glob argument. Prefer using --doctest-modules if you can.

  5. floer32 commented on Dec 16, 2015

    @floer32
    Author

    As for fancypants trickery ...

    The doctest support comes from the official/out-of-the-box plugin, _pytest.doctest (docs). Source code is here. Assuming there's fine coverage of the doctest module, upstream ... I'm not sure we need to add unit tests before adding this little tip to the docs (but I'm all ears)

  6. floer32 commented on Dec 31, 2015

    @floer32
    Author

    @ionelmc given my last comment/two ... do you think we need to add unit tests before adding the tip to the documentation -- or can we just go ahead and add the tip?

  7. ionelmc commented on Dec 31, 2015

    @ionelmc
    Member

    Go ahead and change the docs. I'll take a look at the test part.

  8. ionelmc commented on Jan 6, 2016

    @ionelmc
    Member

    Oooops, looks like I was wrong. Sorry for making you do the PR. I did some local tests, docstrings are always going to appear as "covered". Doctests are in docstrings so they always get 100% coverage.

    Eg, try this and you'll see coverage is 100% which is wrong:

    diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py
    index 0e2f21d..0beb84f 100644
    --- a/tests/test_pytest_cov.py
    +++ b/tests/test_pytest_cov.py
    @@ -154,6 +154,35 @@ def test_central(testdir):
         assert result.ret == 0
    
    
    +def test_doctest(testdir):
    +    script = testdir.makepyfile('''
    +def foo():
    +    """
    +    Doctest::
    +
    +        >>> a = 1
    +        >>> a + 1
    +        2
    +        >>> if 0:
    +        ...     a + b
    +
    +
    +    """
    +''')
    +
    +    result = testdir.runpytest('-v',
    +                               '--cov=%s' % script.dirpath(),
    +                               '--cov-report=term-missing',
    +                               '--doctest-modules',
    +                               script)
    +
    +    result.stdout.fnmatch_lines([
    +        '*- coverage: platform *, python * -*',
    +        'test_central* 123 *',
    +    ])
    +    assert result.ret == 0
    +
    +
     def test_annotate(testdir):
         script = testdir.makepyfile(SCRIPT)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions