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 xdoctest work inside a notebook? #80
Comments
Hmm, I don't think there is a supported API for notebooks at this time. The reason is that the functions are not defined in a file or in a proper module (a notebook is just a big global namespace), and the current API assumes that you are testing something in one of these. Essentially, there is no registry (that I know of) that lists all of the functions in your notebook, or provides the source code to them. However, I don't see any reason why we can't add support for that (I'm not a big user of notebooks myself, so I never considered it as a usecase). Here is a small proof of concept I wrote that allows you to execute all the doctests in a function: def doctest_callable(func):
"""
Example:
>>> def inception():
>>> '''
>>> Example:
>>> >>> print("I heard you liked doctests")
>>> '''
>>> func = inception
>>> doctest_callable(func)
"""
from xdoctest.core import parse_docstr_examples
doctests = list(parse_docstr_examples(
func.__doc__, callname=func.__name__))
# TODO: can this be hooked up into runner to get nice summaries?
for doctest in doctests:
doctest.run(verbose=3) Now if you have a notebook that defines a function with a doctest: def myfunc():
"""
Example:
>>> print("TEST ME")
""" You should be able to simply call doctest_callable(myfunc) I like the idea of a future version of |
Note the latest version of xdoctest 0.14.0 now has a basic version of |
It seems that this method does not work for the test case I had previously proposed. Is there a mistake in my example?
|
Thanks for the report, I guess I should have tried this in a notebook itself (rather than just an IPython terminal). There seems to be an issue obtaining the global variables from a notebook. There is likely a way to detect and handle this case. I'll re-open the issue until it is fixed. Do you know of any way to add Jupyter notebooks to an automated pytest suite? (I'm sure I can google this, and I will if I need to, but any recommendations would be nice). Once I have a fix, I'll want to make sure its tested in the unit tests. |
Sorry, I'm actually relatively inexperienced with using notebooks. Notebooks just happen to be a good medium for teaching aspiring data scientists. I'd like to teach them how to document and test their code the way software engineers would, which is why I'm interested in this use case. |
This is a good use case. I would love it if more data scientists actually tested their code. I'll definitely support this. I'm working on fixing the bug with I've looked into the structure of Jupyter notebooks, and they seem like they are just running everything in a dynamic Do you have a timeline on this? Is this for classes in the fall? Or is this more for consulting-style teaching? |
We plan to begin teaching the class to another group of students in mid September. We would be teaching the lesson on documentation and testing in early November. If it's not ready in time, it won't be the end of the world. |
I'll give it a 95% probability that I'll have it done by then, and a 90% chance I'll do it this weekend. |
I've got an initial version of this working in #84, there is still some code cleanup that I'd like to do, and I'd also like to have the ability to run xdoctest on a jupyter notebook from the command line. Tests are currently failing mainly because this feature isn't implemented yet. To support this feature I've had to enable allowing live-modules to be passed to the "parse_doctestable" and related functions, which somewhat breaks the documentation (currently I'm passing the module via the When you are teaching this class, I'd appreciate if you could encourage the students to contribute any issues / bugfixes / documentation improvements to xdoctest. |
I have this mostly finished, there is a small bug left to fix on windows. |
@Sitwon I believe I have a working version of this in #85. Tomorrow I will merge that into master and then into the release branch, which will push xdoctest If you are able to, could you test out the branch works for your use case?
|
@Erotemic It's working for my tests now. Thank you! |
@Sitwon I'm glad its working for you! The latest version has now been pushed to pypi. Again, when you are teaching this class, I'd appreciate if you could encourage the students to contribute any issues / bugfixes / documentation improvements to xdoctest. Also, I'd appreciate if you would mention while using doctests in Jupyter notebooks may be supported, they are much more powerful when they are used as part of a proper python module (see caveats section bellow). Here is a draft of some new docs that I will likely add to the readthedocs page. Running Doctests in Jupyter NotebooksYou can run doctests within a Jupyter notebook in two ways: Method 1 - Inside the notebookEither insert this cell into your notebook: if __name__ == '__main__':
import xdoctest
xdoctest.doctest_module() This will execute any doctests for callables that are in the top-level namespace of the notebook. While you don't have to include the Method 2 - Outside the notebookAn alternative way to run would be using the xdoctest command line tool and pointing to the notebook file. xdoctest path/to/notebook.ipynb This will execute every cell in the notebook and then execute the doctest of any defined callable with a doctest. CaveatsWARNING: in both of the above methods, when you execute doctests it will include any function / class that was defined in the notebook, but also any external library callable with a doctest that you import directly! Therefore it is best to (1) never use Lastly, it is important to note that Jupyter notebooks are great for prototyping and exploration, but in practice storing algorithm and utilities in Jupyter notebooks is not sustainable (for some of these reasons). Reusable code should eventually be refactored into a proper pip-installable Python package where the top level directory contains a |
Is there a straight-forward way to run xdoctest inside of a notebook?
I am testing with Google Colaboratory. I tried a few different way of invoking
xdoctest.doctest_module()
but they all threw errors.The behavior I was expecting would be similar to the built-in doctest (first code block in the example.)
https://colab.research.google.com/drive/1oOQWUDdFxHKWNiUUjMrJXwPcMaumLy0O?usp=sharing
The text was updated successfully, but these errors were encountered: