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

support testsetup #5

Open
jab opened this issue Oct 30, 2018 · 10 comments
Open

support testsetup #5

jab opened this issue Oct 30, 2018 · 10 comments

Comments

@jab
Copy link

jab commented Oct 30, 2018

I saw in the README that support for testsetup is planned but didn't see an issue to track it, so thought I'd create one and register interest to boot.

Is there a workaround you would recommend in the meantime?

Thanks for maintaining pytest-sphinx!

@twmr
Copy link
Owner

twmr commented Oct 31, 2018

No there is unfortunately no workaround. In the coming days I'll clean up the existing implementation first and then I'll implement support for testsetup. Since you are also interested in support for more flags, maybe you can try to contibute and add support for pyversion and skipif. That would be great.

@jab
Copy link
Author

jab commented Nov 3, 2018

(see pytest-dev/pytest#4274 (comment))

@nathanshammah
Copy link

I was wondering if there were any updates.

Maybe I need much less (I'm not so familiar with sphinx+doctest): My issue is that I have a function that is defined in the guide *.rst file, and I'd like to use it. Right now I am including a module with such function in the code base, including such module in the conf.py doctest_global_setup feature and then importing it in a testsetup block that I'd like to provide it for the subsequent blocks in the same .rst file. Is there some easy fix I am not looking? make doctest passes, but pytest --doctest-glob fail, and these are set in the CI on Github workflow (similar to Travis CI). Do you have any suggestion?

@twmr
Copy link
Owner

twmr commented Apr 20, 2020

Is there some easy fix I am not looking?

No currently not. I'll have a look at the testsetup code again, once I'm done with the support for ":skipif:" option.

@nathanshammah
Copy link

nathanshammah commented Apr 20, 2020

@Thisch thank you. Sorry, just to understand:
Now I am using testcode + testoutput and I get some errors with pytest --doctest-glob='*.rst' (with the pytest-sphinx plugin installed) but not with make doctest. The issue seems to be that I am defining some functions in a testcode block, and using them in a subsequent testcode block, in the same *.rst file. Is this an intended behavior, a bug, or I am not setting it up correctly?

@twmr
Copy link
Owner

twmr commented Apr 20, 2020

Your described behavior is definitely a bug. I don't yet have an idea how to implement/fix it, because it requires that we share data between pytest test-items.

@twmr
Copy link
Owner

twmr commented Apr 22, 2020

@nathanshammah Please take a look at #23. You can move the code block from conf.py to conftest.py and adapt it s.t. uses the doctest_namespace fixture. See https://docs.pytest.org/en/latest/doctest.html#doctest-namespace-fixture

Does this solve some of you issues?

@jab
Copy link
Author

jab commented Jan 4, 2023

The doctest_namespace workaround works in some cases, but not all. For example, if you try to reference an injected variable from a :skipif:, you get a NameError.

# conftest.py
@pytest.fixture(autouse=True)
def add_doctest_globals(doctest_namespace):
    doctest_namespace['foo'] = False
# test.rst

.. testcode::
    :skipif: not foo

    42
$ pytest --doctest-glob='*.rst'
...
ERROR test.rst - NameError: name 'foo' is not defined

@jab jab mentioned this issue Jan 4, 2023
4 tasks
@bveeramani
Copy link

Hey @Thisch! We're considering using pytest-sphinx for https://github.com/ray-project/ray. Do you know if support for testsetup is still planned?

@twmr
Copy link
Owner

twmr commented May 8, 2023

Hi! That's great news! Currently support for testsetup is still wanted, but it is not planned to be implemented anytime soon. I've just seen that https://github.com/astropy/pytest-doctestplus has "support" for testsetup, but it just works by accident by using the >>> in the testsetup block, s.t. the code is run by the doctest plugin that is part of pytest. (scientific-python/pytest-doctestplus#164)

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

4 participants