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

Tests fail if there's _ (underscore) defined in the notebook #82

Closed
sidujjain opened this issue Feb 8, 2021 · 6 comments · Fixed by #84
Closed

Tests fail if there's _ (underscore) defined in the notebook #82

sidujjain opened this issue Feb 8, 2021 · 6 comments · Fixed by #84

Comments

@sidujjain
Copy link

Is there a way to handle this?

Test:

from testbook import testbook

@testbook('nb.ipynb', execute=True)
def test_foo(tb):
    foo = tb.ref("foo")
    assert foo(2) == 3

Notebook:

_ = 20

def foo(x):
    return x + 1

Result:


    @testbook('nb.ipynb', execute=True)
    def test_foo(tb):
        foo = tb.ref("foo")
>       assert foo(2) == 3
E       AssertionError: assert 20 == 3
E        +  where 20 = <[TypeError('__repr__ returned non-string (type int)') raised in repr()] TestbookObjectReference object at 0x7ff04d735c90>(2)

tb.py:6: AssertionError
======================================================================== short test summary info ========================================================================
FAILED tb.py::test_foo - AssertionError: assert 20 == 3
@MSeal
Copy link
Member

MSeal commented Feb 8, 2021

So I have a fix for the type error occurring when reporting the value of foo(2).

However the root problem is that testbook is using the _ variable to fetch the execution value of the last cell. In ipython this has special meaning:

In [1]: 1 + 1
Out[1]: 2

In [2]: _
Out[2]: 2

However when you do an underscore assignment like _ = 20 you permanently assign that variable to 20 causing all 'last-value' checks to say they got a value of 20.

In [1]: _ = 20

In [2]: 1 + 1
Out[2]: 2

In [3]: _
Out[3]: 20

Generally I would avoid doing _ assignments within a notebook, though I'll submit the patch here in a second to make it so the assertion at least reports the error accurately as 20 == 3 assertion failure.

@rohitsanj
Copy link
Member

@sidujjain Thanks for reporting. Will look into this now.

@MSeal thanks for the change, I'll see if I can add a fix to the same PR.

@sidujjain
Copy link
Author

Thanks for the quick response, @MSeal ! Using _ is a common pattern in python for unpacking tuples or in loops.

I wonder if there is a way to capture the output in a different way or to reset _ before running the Testbook tests.

@MSeal
Copy link
Member

MSeal commented Feb 9, 2021

Yeah I know, though it's somewhat more of an ipython limitation: https://stackoverflow.com/questions/56734319/reset-underscore-variable-in-jupyter-notebook-powered-by-ipython-kernel. That link has some paths to fix the root issue. @rohitsanj we could see about forcing the result outcome each injection to see if we can auto-repair it?

@rohitsanj
Copy link
Member

@sidujjain Please upgrade to testbook 0.2.6 and the issue should be fixed!

pip install -U testbook

@sidujjain
Copy link
Author

Thanks for the quick fix @rohitsanj ! Works like a charm!

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

Successfully merging a pull request may close this issue.

3 participants