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

Add pytest functionality #2

Open
asteppke opened this issue Apr 5, 2019 · 5 comments
Open

Add pytest functionality #2

asteppke opened this issue Apr 5, 2019 · 5 comments
Labels
enhancement New feature or request

Comments

@asteppke
Copy link

asteppke commented Apr 5, 2019

Combining Jupyter notebooks with test driven development feels great, treon is really helpful for CI pipelines. Is it possible besides doctest and unittest to also include the pytest framework?

It seems that pytest does not have a drop-in function such as the unittest.main() which executes the current module but requires a filename. Still this would be a nice addition and removes a lot of boilerplate one needs for the unittest framework.

@amit1rrr
Copy link
Member

amit1rrr commented Apr 6, 2019

I debated for a while between supporting unittest vs pytest at the beginning & mainly chose unittest because,

  • It's easier for non-developers to get started with (a lot of Jupyter users are non developers)
  • Slightly easier to implement since it can be triggered as one of the code cell itself

Anyhow, thinking more about it, there's no reason why treon shouldn't support pytest as well. So I'm going to work on it sometime in the next few weeks (unless someone else picks it up before that).

Here's the rough implementation outline,

  1. Convert all the notebooks under test into .py temporary files
  2. Run py.test filename for each of those
  3. Capture+Modify the stdout output to reflect correct notebook name instead of .py name
  4. Include the result (success/failure) in overall result summary

Let me know if there's a better way.

@asteppke
Copy link
Author

asteppke commented Apr 8, 2019

I agree that unittest should definitely be supported for the reasons you gave and also because it is part of the standard library.

Pytest seems to develop into somewhat of a successor to unittest, nose and other python test libraries. Additionally the syntax is easier to learn, e.g.

def test_example():
    assert [1, 2, 3] == [1, 2, 3]

is just much simpler than what unittest offers. To implement it I think think your route sounds good. There are also the ipytest and nbval projects which slightly overlap in functionality. I don't know if including either of these would make the integration of pytest into treon easier though.

@amit1rrr
Copy link
Member

amit1rrr commented Apr 8, 2019

There are also the ipytest and nbval projects which slightly overlap in functionality. I don't know if including either of these would make the integration of pytest into treon easier though.

I had looked at ipytest earlier and it's mainly geared for running pytest as a cell magic from the Jupyter environment itself. Nothing wrong with it just wouldn't integrate well with treon's command line approach. Probably best to directly use nbconvert (for ipynb to py) and py.test so we can access the test result (success/failure) and integrate it into treon result summary etc.

Aside:
nbval is pretty nice actually but it doesn't run pytests, instead it's a pytest plugin that validates the notebook (i.e. see if cell output after execution matches the cell output already present in the file and such). Very handy. I have been thinking of something along the same lines, parameterised tests with papermill. Anyway, that's worthy of a separate issue.

@amit1rrr amit1rrr added the enhancement New feature or request label Apr 8, 2019
@stas00
Copy link

stas00 commented May 3, 2019

I debated for a while between supporting unittest vs pytest at the beginning & mainly chose unittest because,
[...]
Let me know if there's a better way.

There is. You parse the cells - and then run them.

There is an old way that uses runipy - you can see here: https://github.com/stas00/pytest-ipynb (it's a fork of no longer maintained pytest module, that I fixed up just enough to satisfy my needs). You can see it in action by running make test in https://github.com/stas00/ipyexperiments after you installed my fork of pytest-ipynb.

And there is the modern approach, https://www.blog.pythonlibrary.org/2018/10/16/testing-jupyter-notebooks/

May be you could take over pytest-ipynb and integrate treon into it (or the other way around), I am not attached.

I've been trying to summarize the existing approaches here: https://github.com/stas00/pytest-ipynb/blob/master/ipynb-testing-ways.md - please send me a PM to add treon.

p.s. Kudos on all the great work with reviewnb and now treon, Amit!

@amit1rrr amit1rrr mentioned this issue May 13, 2019
@westurner
Copy link

I've been trying to summarize the existing approaches here: https://github.com/stas00/pytest-ipynb/blob/master/ipynb-testing-ways.md - please send me a PM to add treon.

Just learned of treon from https://github.com/markusschanta/awesome-jupyter/blob/master/README.md#testing ; which currently lists:

Testing

  • ipytest - Test runner for running unit tests from within a notebook.
  • nbval - Py.test plugin for validating Jupyter notebooks.
  • sphinxcontrib-jupyter - Sphinx Extension for Generating Jupyter Notebooks.
  • nosebook - Nose plugin for finding and running IPython notebooks as nose tests.
  • treebeard - GitHub Action for testing/scheduling Jupyter notebooks.
  • treon - Easy-to-use test framework for Jupyter Notebooks

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

No branches or pull requests

4 participants