Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does pytest-doctestplus support testcode and testoutput directives? #106

Open
artsiomkaltovich opened this issue Apr 23, 2020 · 10 comments
Open

Comments

@artsiomkaltovich
Copy link

Hello.

I've just install pytest-doctestplus, so sorry, for probably stupid question.

I am written the following doc:

>>> 43 + 1
44

.. testcode::
    43 + 1

The result is:

.. testoutput::
    44

But when I run pytest only first test is executed.

While is is also correct way for defining tests: https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html

Regards.

@bsipocz
Copy link
Member

bsipocz commented Apr 23, 2020

Please note that sphinx doctest extension is not the same as the pytest-doctestplus plugin. In your exampe, you need to add >>> to the testcode as well to be picked up by pytest.

@artsiomkaltovich
Copy link
Author

artsiomkaltovich commented Apr 24, 2020

Hello @bsipocz

But do you have plans to support it?

Maybe you know if it is difficult to add? :) I can try if it doesn't take much time.

I think code examples without >>> are more readable, and more easy to copy-paste, so I would like to use them.

@TiemenSch
Copy link

What would need to happen for this extension to pickup the .. testcode:: and .. testoutput:: directives?

As they are pretty much introduced in the same place in Sphinx's docs (https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html), I think it would be rather intuitive to include them with this extension for pytest, too.

Copying code snippets from .. testcode:: blocks is way easier for users reading documentation than the .. doctest:: ones with the >>> littered everywhere IMHO.

@pllim
Copy link
Contributor

pllim commented Mar 3, 2021

I don't think we have plans to add this support, as we don't use it ourselves in the Astropy project. You can copy the >>> directly into IPython session and the session is smart enough to ignore the >>>, so we have gotten no such complains from users (yet).

@TiemenSch
Copy link

TiemenSch commented Mar 3, 2021

Ah, that makes sense. I understood this was separated from the rest of the codebase to be a bit more standalone applicable for "any" pytest/Python user.

For non-IPython console users (e.g. flat Python files/packages or when working in JupyterLab/notebooks), the >>> represent a bit of a hassle.

@pllim
Copy link
Contributor

pllim commented Mar 3, 2021

I see a similar discussion upstream at readthedocs/sphinx_rtd_theme#167 . I did remember a long time ago I came across a doc that lets you toggle the >>> on/off but now I can't find it anymore.

@saimn
Copy link
Contributor

saimn commented Mar 3, 2021

Probably https://github.com/astropy/astropy-sphinx-theme/blob/master/astropy_sphinx_theme/bootstrap-astropy/static/copybutton.js ;-)

@pllim
Copy link
Contributor

pllim commented Mar 3, 2021

Ah, yes. It is still there. I was blind. 😆

Screenshot 2021-03-03 100708

@TiemenSch
Copy link

TiemenSch commented Mar 3, 2021

That https://github.com/jurasofish/sphinx-toggleprompt extension is a nice workaround to be able to get the >>> out of the way quickly :)

I think there is quite some overlap with the logic that's already implemented over at https://github.com/thisch/pytest-sphinx
That seems to be a self-made plugin to support more or less the same as this extension does, except for the other directives. A little less mature and not fully supporting the directive options, though.

Thanks for the quick responses btw!

EDIT:
As another workaround you can also stick with only sphinx-copybutton button using regex prompt identifiers.

Still, 'natively' picking up testcode would be awesome.

@lpsinger
Copy link
Contributor

lpsinger commented Mar 6, 2022

Here's a little Sphinx extension that monkeypatches pytest-doctestplus and sphinx.ext.doctest to at least get FLOAT_CMP working:

def setup(*args, **kwargs):
    import doctest
    import pytest_doctestplus.output_checker
    import sphinx.ext.doctest

    doctest.OutputChecker = pytest_doctestplus.output_checker.OutputChecker
    return sphinx.ext.doctest.setup(*args, **kwargs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants