nteract / testbook

๐Ÿงช ๐Ÿ“— Unit test your Jupyter Notebooks the right way
https://testbook.readthedocs.io
BSD 3-Clause "New" or "Revised" License
416 stars 37 forks source link

Decorated tests not being picked up by pytest #75

Closed bensenberner closed 3 years ago

bensenberner commented 3 years ago

With a test.py that looks like

from testbook import testbook

@testbook('my_notebook.ipynb')
def test_foo(tb):
    assert False, "foo"

def test_bar():
    assert False, "bar"

running pytest test.py yields

=================================== test session starts ===================================
platform linux -- Python 3.7.9, pytest-6.0.2, py-1.9.0
collected 1 item                                                                          

test.py F                        [100%]

======================================== FAILURES =========================================
________________________________________ test_bar _________________________________________

    def test_bar():
>       assert False, "bar"
E       AssertionError: bar
E       assert False

test.py:9: AssertionError
================================= short test summary info =================================
FAILED test.py::test_bar - AssertionE...
==================================== 1 failed in 0.17s ====================================

Only the non-testbook test is picked up, even though they both are prefixed with test.


On the other hand, when I use a context manager in test.py:

from testbook import testbook

def test_foo():
    with testbook('my_notebook.ipynb') as tb:
        assert False, "foo"

def test_bar():
    assert False, "bar"

then both the tests are picked up.

=================================== test session starts ===================================
platform linux -- Python 3.7.9, pytest-6.0.2, py-1.9.0
collected 2 items                                                                         

test.py FF                       [100%]

======================================== FAILURES =========================================
________________________________________ test_foo _________________________________________

    def test_foo():
        with testbook('my_notebook.ipynb') as tb:
>           assert False, "foo"
E           AssertionError: foo
E           assert False

test.py:7: AssertionError
________________________________________ test_bar _________________________________________

    def test_bar():
>       assert False, "bar"
E       AssertionError: bar
E       assert False

test.py:10: AssertionError
================================= short test summary info =================================
FAILED test.py::test_foo - AssertionE...
FAILED test.py::test_bar - AssertionE...
==================================== 2 failed in 1.20s ====================================
MSeal commented 3 years ago

Thanks for raising, it looks like the testbook decorator needs to preserve the function signature or something in pytest has changed recently. @rohitsanj think you'll have a little time to look into it?

rohitsanj commented 3 years ago

Thanks for raising this issue. This is certainly a high priority bug. I think this is happening due to #68, where pytest sees the test function as a fixture. Meanwhile, I could release a patch that fixes this issue by just removing the fixture call.

Hey @fcollonval, unfortunately wrapping decorator as a pytest fixture causes the test to not be picked up by pytest, which silently slipped by through our CI. Any ideas on how we can fix this? ๐Ÿ˜…

fcollonval commented 3 years ago

Hey sorry to miss that. I pushed a fix that works this time.