Repository navigation
Description
I am suggesting adding support for another directive, something like .. doctest-cd:: ../tests/example/ which would triggering the insert of a snippet like import os; os.chdir(new_path) into the docstring where the new path is absolute and pre-computed by interpreting the requested path relative to the RST filename and location.
Brief comparison to Biopython's home-grown mechanism for extracting doctests from RST files
For background, I see pytest-doctestplus dates back about 13 years to the needs of the astropy project. Going back slightly earlier to 14 years ago, for Biopython I started something conceptually similar for running doctests extracted from our documentation (first commit). We currently use a unittest based framework, with the a single script handling this.
Up until Biopython 1.83 this was based on LaTeX https://github.com/biopython/biopython/blob/biopython-183/Tests/test_Tutorial.py with special comment lines preceding code blocks to be tested as doctests.
As of Biopython 1.84 this is based on RST (which is rendered with Sphinx), again done special comments (so that Sphinx ignores our directives) interpreted as marking .. code:: pycon blocks to be tested: https://github.com/biopython/biopython/blob/biopython-184/Tests/test_Tutorial.py
This is very specific to our needs, so switching to a more widely used and maintained project like pytest-doctestplus is potentially attractive.
Our current solution enabled the following:
- pycon blocks are either ignored and not tested (default), become a new self-contained doctest, or are appended to the current doctest (allowing for text part way through an example)
- My initial impression from example https://github.com/scientific-python/pytest-doctestplus/blob/v1.2.1/tests/docs/skip_some.rst and the README is
pytest-doctestplustakes a different approach, with everything in a single RST file becoming a single large doctest unless.. doctest-skip::is used to skip a block.
- My initial impression from example https://github.com/scientific-python/pytest-doctestplus/blob/v1.2.1/tests/docs/skip_some.rst and the README is
- declare python dependencies (and the test is skipped if not met)
- This is handled here by
.. doctest-requires:: asdf(and is more flexible)
- This is handled here by
- declare internet access is required (and the test is skipped otherwise)
- This is handled here by
.. doctest-remote-data::
- This is handled here by
- declare a relative working directory (to allow shorter paths in the examples, for example using a test sub-directory specific to a topic)
- This does not appear to be possible in
pytest-doctestplusyet.
- This does not appear to be possible in
The scoping choice looks difficult to change here, but the final point about specifying a working directory for the tests looks like it could be added.
Activity
pllim commentedon Sep 19, 2024
@peterjc , thank you for your interest!
I really appreciate your detailed explanation above but I am not sure if I completely understand the need for specifying path. The way doctestplus works (if I remember correctly), any rendered API or user doc with
>>>prefix would be considered for doctest. It does not matter where that file is as long as Sphinx is collecting it. So there is no need to change directory. Can you please point me to one specific doctest requiring this? Is it the commented code at the beginning oftest_Tutorial.py?peterjc commentedon Sep 19, 2024
For the how: My implementation inserts the path change into the beginning of the docstring here (bad past-Peter using single letter variable names):
https://github.com/biopython/biopython/blob/biopython-184/Tests/test_Tutorial.py#L235
For the why: Most of our sample data files files are under
{repo}/Tests/in assorted subfolders, some are under the{repo}/Docs/examples/, but rarely do we want to expose the potentially long path to the user in an example snippet of code.Here we specifically run a doctest under
{repo}/Docs/examples/allowing the example to parse a file with a local filename"ls_orchid.fasta"(no path component needed):https://github.com/biopython/biopython/blob/biopython-184/Doc/Tutorial/chapter_seqio.rst?plain=1#L395
https://github.com/biopython/biopython/blob/biopython-184/Doc/examples/ls_orchid.fasta
While here run another doctest under
{repo}/Tests/Quality/instead, again so that the example can just use a local filename, in this case"example.fastq":https://github.com/biopython/biopython/blob/biopython-184/Doc/Tutorial/chapter_seqio.rst?plain=1#L1664
https://github.com/biopython/biopython/blob/biopython-184/Tests/Quality/example.fastq
pllim commentedon Sep 19, 2024
I have a counter proposal, I think what you need is one of the following possible alternatives:
testsetupand then have a dummy code with pretty filename with+skipthat gets rendered. The subsequent code use the file object that user thinks is from pretty name but actually was created fromtestsetup.Would any of these alternatives work for you?
peterjc commentedon Sep 19, 2024
get_pkg_data_filenamein Biopython, and adding something like that seems quite an ask.testsetupdirective, or perhaps more cleanly mocking this with a symlink (and removing it in the tear-down).So I think we can justify turning this suggestion down. But thank you!
bsipocz commentedon Sep 19, 2024
I would second Pey Lian's suggestions for workaround solutions. The testsetup directive is rather convenient.
It's rather unlikely that any of the current maintainers will implement this feature request, but I would like to be explicit on the fact that if you're willing to implement and PR a new directive, it would be more than welcome.
pllim commentedon Sep 19, 2024
I also haven't thought about any potential security implication of a feature that would allow changing into arbitrary directories.
pllim commentedon Sep 19, 2024
Granted the code in astropy is a bit too much to stare at, but if you don't need remote data server support or caching, I think it is not that many lines of code. The logic for path is mainly in https://github.com/astropy/astropy/blob/d09ba92c28a0e22c27e7dfcdd2ed45d3bb7fa80d/astropy/utils/data.py#L989 and you can totally ignore
if package == "astropy.utils.iers"block there.Anyway, if you are interested to pursue this, I will be happy to consult. Good luck!
peterjc commentedon Sep 19, 2024
I felt the security issues were moot (given the risks in running the example code), as that can do this anyway:
bsipocz commentedon Sep 19, 2024
I don't see any added security issues either, the directive would be a shortcut to hide existing, albeit ugly directory changes. (Although I wonder if the correct version of the library code would be picked up locally for this case, e.g. when one runs pytest in the source directory without installing it.)