-
Notifications
You must be signed in to change notification settings - Fork 37
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
the tb.patch will only allow strings to be returned from mocks #72
Comments
Hey! First of all, thank you for using testbook! :) Here are a few comments about the code snippet provided:
Here is the suggested code snippet:from testbook import testbook
from collections import namedtuple
@testbook('./doc/examples/notebooks/tools/01-CreateFromObjectNo.ipynb')
def test_get_object(tb):
mock_object = namedtuple("dataobject", ["name", "description"])
with tb.patch(
"library.dataobjects.DataObject.retrieve",
return_value=mock_object("Some Name", "Some Description"),
):
tb.execute_cell([2, 4])
# obtain reference to the mocked `retrieve` function
retrieve = tb.ref("DataObject.retrieve")
dataobject = retrieve("DO-320")
assert dataobject.name == "Some Name"
assert dataobject.description == "Some Description" |
Also, please note that In case you do want to execute cells from 2 to 4 (inclusive), you would need to pass in a slice object as follows: tb.execute_cell(slice(2, 5)) # end is exclusive, hence it will execute until 5 - 1 = 4 |
Thanks for providing testbook, which looks extremely useful for my case of testing notebooks. The And yes, the intention was to only execute cell 2 and 4, since cell 3 contains markdown. Cell 2 contains the import statements and cell 4 the logic that retrieves the object and queries its attributes. The point is not to test the return values of |
After trying to experiment with the source code I have the understanding now that we cannot carry arbitrary custom defined types through the Instead I utilized the possibility of injecting code into the notebook to ensure arbitrary types could be mocked via the from testbook import testbook
from collections import namedtuple
@testbook('./doc/examples/notebooks/tools/01-CreateFromObjectNo.ipynb')
def test_get_object(tb):
tb.execute_cell(2) # execute cell with import statements
tb.inject(
"""
from unittest.mock import patch
from collections import namedtuple
mock_object = namedtuple("dataobject", ["name", "description"])
p1 = patch(
"library.dataobjects.DataObject.retrieve",
return_value=mock_object("Some Name", "Some Description")
)
p1.start()
"""
)
tb.execute_cell(4) # execute cell with business logic
tb.inject(
"""
p1.stop()
"""
)
assert tb.cell_output_text(4) == "Some Name\n\nSome Description" |
That's right, which is why we have built the
No worries, I hope this approach solved the problem. You can take a look at the reference for inject for more flexibility while injecting. Few arguments you can do pass to the
Note that Coming back to the original issue of :
I am not too sure how that could have happened, but please let me know if this issue still persisted after the changes I had recommended. If not, may I go ahead and close this issue? PS: Please drop an email to mail@rohitsanjay.com so that I may invite you to nteract's Slack workspace. |
Thanks for the reply, the As to the TLDR; great tool, documentation could use a brush-up, and using the |
I have a notebook that calls some library function and gets a complex object returned. Since the object ultimately comes from a database call, I want to mock this such that the notebook gets a synthetic object instead. I have tried with the following pattern:
The notebook cell 2 handles imports and the notebook cell 4 looks something like:
The test the fails with:
Am I missing something in the documentation or can we only return strings with the wrapped
patch
?The text was updated successfully, but these errors were encountered: