You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a suite of example notebooks that I wish to use as tests. Many use a python package to connect to a server, but when testing the syntax for making that connection is different. For example, in general the notebooks invoke Client() and because they're in the prod environment the connection requires no additional information, but the tests are run from a different runner which needs something like Client(additional_credentials).
Client is from an external library I cannot change and does not provide a good way to override the defaults from environment variables, etc. My plan was to mock the Client() during testing by using a session-scoped, autouse=True fixture in a conftest.py to inject the extra information into Client() calls. This works for regular pytests (.py files), but the notebooks evaluated by nbval do not have their Client() mocked over.
Schematic example:
client.py
class Client():
def __init__(self, creds):
self.x = creds
I don't think there's any great way to do this, because the notebook code is running in a separate process - nbval launches a Jupyter kernel, and sends the cells to that. It might be possible to hack something with .pth files or a sitecustomize.py (see the site module docs) to make code run when the kernel is starting up. But that definitely wouldn't be elegant, and I don't know quite how to ensure it's cleaned up at the right time.
I have a suite of example notebooks that I wish to use as tests. Many use a python package to connect to a server, but when testing the syntax for making that connection is different. For example, in general the notebooks invoke
Client()
and because they're in the prod environment the connection requires no additional information, but the tests are run from a different runner which needs something likeClient(additional_credentials)
.Client
is from an external library I cannot change and does not provide a good way to override the defaults from environment variables, etc. My plan was to mock theClient()
during testing by using a session-scoped,autouse=True
fixture in aconftest.py
to inject the extra information intoClient()
calls. This works for regular pytests (.py
files), but the notebooks evaluated by nbval do not have theirClient()
mocked over.Schematic example:
client.py
conftest.py
calling_notebook.ipynb
Is there a way to make this work or a better pattern achieve the same result?
The text was updated successfully, but these errors were encountered: