Code Monkey home page Code Monkey logo

Comments (7)

MSeal avatar MSeal commented on June 19, 2024 1

I like the pattern. It's flexible and clean to read. We'll want to document it well when we get to that step of the project so it's clear how to execute in either mode with isolated or shared kernels.

from testbook.

rohitsanj avatar rohitsanj commented on June 19, 2024 1

The following pattern seems promising and is fairly flexible 🎉

import pytest
from testbook import testbook


@pytest.fixture(scope='module')
def notebook():
    # prerun cells here that need to be run just once across all tests in the scope
    with testbook('/path/to/notebook.ipynb', prerun=['some', 'cells']) as tb:
        yield tb

def test_1(notebook):
    assert notebook.value("bar") == 'hello world'
    notebook.inject("bar = 'something else'", run=True)


def test_2(notebook):
    assert notebook.value("bar") == 'something else'

We will want to document this well, perhaps provide tutorials/examples as to how it can be used.

from testbook.

rohitsanj avatar rohitsanj commented on June 19, 2024

#19 brings this functionality in place as well.
We can let the user create a global instance of notebook_loader, which can then be used across multiple tests - hence sharing the execution context.

Take a look at the following code snippet:

from testbook import notebook_loader

foo = notebook_loader('/path/to/notebook.ipynb', prerun=['tag1', tag2', tag3'])

@foo
def test_notebook_1(notebook):
    assert notebook.cell_output_text('execute_foo') == 'foo\n'


@foo
def test_notebook_1(notebook):
    assert notebook.cell_output_text('some_other_tag') == 'some other output\n'

or..

def test_notebook():
    with foo():
        assert notebook.cell_output_text('execute_foo') == 'foo\n'

def test_notebook_1():
    with foo():
        assert notebook.cell_output_text('some_other_tag') == 'some other output\n'

Of course, it can be named something much clearer than "foo"

UPDATE: The above implementation does not work, it does not preserve the kernel context across multiple tests. More info at #28

from testbook.

rohitsanj avatar rohitsanj commented on June 19, 2024

Is it possible to replicate the kernel context across multiple tests? For example, let us say that there is a specific set of cells which need to be executed prior to all the tests in a module. Is it possible to always start with the same fresh initial context in every test?

I would imagine that it would involve storing the kernel context somehow and "injecting" it into future kernels? Not sure.

cc @MSeal

from testbook.

MSeal avatar MSeal commented on June 19, 2024

Is it possible to always start with the same fresh initial context in every test?

Aren't we making that an option to the testbook decorator via the prerun argument? It does require you decorate each test, so another option would be to follow the top answer from https://stackoverflow.com/questions/22627659/run-code-before-and-after-each-test-in-py-test and document (or provide?) a function for wrapping all tests in a scope with the notebook prep, maybe by generating a fixture within that scope, or more likely, by passing the tb object to each test?

Related: in ipython there's also %reset -f which isn't a perfect solution but it would save a lot of time on kernel reboot times as an optimization for tests against that kernel.

from testbook.

MSeal avatar MSeal commented on June 19, 2024

I would imagine that it would involve storing the kernel context somehow and "injecting" it into future kernels? Not sure.

In process state transfers... not a reliable pattern. In some controlled contexts it's technically possible but I would not pursue that path for implementations.

from testbook.

rohitsanj avatar rohitsanj commented on June 19, 2024

Related: in ipython there's also %reset -f which isn't a perfect solution but it would save a lot of time on kernel reboot times as an optimization for tests against that kernel.

This is actually perfect for speeding up the tests that run on the same notebook. 💯

from testbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.