Comments (3)
To be more specific:
We should have a decorator pattern to add a testbook Client object to a test:
@testbook.notebook(path_to_notebook)
def test_notebooks_func(notebook):
notebook.execute_cell(1)
notebook.assert_cell_text_output(1, "Hello World")
notebook.execute_cell(2) # Should use the same kernel without shutting it down until the test completes
notebook.assert_cell_text_output(2, "Hello World 2")
and to support a fixture pattern something like this:
@pytest.fixture()
def notebook_tester():
def notebook_loader(nb_path):
nb = testbook.NotebookClient(nb_path)
with nb.setup_kernel(**kwargs):
yield nb
....
from testbook import notebook_tester
def test_notebooks_func(notebook_tester):
with notebook_tester(nb_path) as notebook:
notebook.execute_cell(1)
notebook.assert_cell_text_output(1, "Hello World")
notebook.execute_cell(2) # Should use the same kernel without shutting it down until the test completes
notebook.assert_cell_text_output(2, "Hello World 2")
from testbook.
Yeah... there is a few hacks to get around this. Here's some useful links on the topic:
https://stackoverflow.com/questions/52060950/how-to-use-pytest-capsys-on-tests-that-have-mocking-decorators/52065289#52065289
pytest-dev/pytest#2424
https://docs.python.org/3/library/inspect.html#types-and-members (for how to hack globals)
Here's a global hack pattern I just tried out quickly and it works for python 3 with pytest:
import functools
import inspect
def wrap_test(input):
parent_scope = inspect.stack()[1][0].f_globals # Hack alert, needed to setup pytest fixture variables in the same scope as the test :/
@pytest.fixture(scope='module')
def added(): # Can make the function name dynamic or whatever is needed
return input + '_added'
def wrapper(func):
@functools.wraps(func)
def inner_wrapper(*args, **kwargs):
if 'added' in parent_scope:
raise RuntimeError(
"Can't assign fixture {name} to test because the name is already in use".format(name='added'))
parent_scope['added'] = added
try:
return func(*args, **kwargs) # Can also inspect the function to get the name of the first argument if you want the name assignment to be positional instead of by name
finally:
# Cleanup definition if it's still present
if 'added' in parent_scope:
del parent_scope['added']
return inner_wrapper
return wrapper
then in a test file you can do:
@wrap_test('foo')
def test_something(added):
print(added) # 'foo_added'
from testbook.
@testbook.notebook(path_to_notebook)
def test_notebooks_func(notebook):
notebook.execute_cell(1)
notebook.assert_cell_text_output(1, "Hello World")
notebook.execute_cell(2) # Should use the same kernel without shutting it down until the test completes
notebook.assert_cell_text_output(2, "Hello World 2")
The above test will not work when we run it using pytest.
It will throw an error: fixture 'notebook' not found
How do we work around this?
If there is no way, seems like we will be restricted to implementing only the fixture pattern.
from testbook.
Related Issues (20)
- Getting an InterpreterNotFoundError on running the command tox -e py36 during setup HOT 1
- Document `nb` attribute and how it represents the nbformat object
- Entire notebook executed before the specific method HOT 3
- Testbook fails when using pytest test-classes HOT 2
- Support for objects as return_value when patching HOT 1
- Testbook giving timeout error because of nbclient HOT 1
- testbook loads wrong signature for function HOT 1
- Use pickle instead of JSON for serialization HOT 1
- support `async` function calls
- Asyncio errors and intermittent test failure
- Add support for SKIPPING cells' execution HOT 3
- Return value as None from testbook function HOT 4
- Mention in the contributing docs that an `ipykernel` with name `python3` must be present for tests to run locally
- Incompatibility between `execute=False` and `tb.patch()`
- testbook.ref cannot recognize complex variable
- `AttributeError: data` when repeatedly calling same function
- Because of project structure, relative paths in Notebook are no longer valid when testbook executes Notebook before tests HOT 1
- ModuleNotFoundError while using @testbook('....ipynb', execute=True) HOT 2
- Add type annotations
- Remove codecov dependency HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testbook.