0

Consider the following doc containing doctests.

def f(x):
   """
   Increments by one.
   >>> f(2)
   3
   """
   return x + 1

PyCharm's test runner (as seen here) picks the doctest up.

But the following is not seen by PyCharm as a doctest (though it is seen by pytest --doctest-modules -v for example).

f = lambda x: x + 1
f.__doc__ = """
Increments by one.
>>> f(2)
3
"""

thorwhalen
  • 1,920
  • 14
  • 26
  • 1
    I sense an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Julia Mar 04 '21 at 15:33
  • 1
    Silly thing to try: Try indenting the contents of the manually assigned docstring by one level (and maybe the close triple-quote line as well). I don't know what PyCharm does normally to find tests, but outside of module level docstrings, the contents can be assumed indented, and it's possible the functionality it's using to find/parse docstrings for tests has a problem with the tests aren't indented at all. Heck, in [the answer to the question you linked](https://stackoverflow.com/a/33980789/364696) the example has even the module-level doc string indented, which makes me suspicious. – ShadowRanger Mar 04 '21 at 18:28
  • Yes, @ShadowRanger; that seems to have worked. Why don't you make it an answer so I can assign it as the answer? – thorwhalen Mar 08 '21 at 19:28

1 Answers1

0

Per comments, it looks like PyCharm expects all the tests to be indented at least one level (even in cases where it's the module level docstring, which need not be indented at all, it seems to rely on that level of indentation). This is not a requirement of regular doctests (python3 -mdoctest -v modulename.py works just fine), but PyCharm's gotta be difficult.

Your manually assigned docstring has no indentation, and PyCharm appears to ignore it as a result. Changing it to:

f = lambda x: x + 1
f.__doc__ = """
    Increments by one.
    >>> f(2)
    3
    """

to make the string include one level of indentation seems to be enough to get PyCharm on board.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271