pytest. This approach allows you to build comprehensive yet flexible tasks for the user to complete. For example, you can test the contents of a given variable, the return value of a function, or even a class.
pytestPython libraries and unit tests.
testbookis used to execute the entire notebook and then import "references" to Python objects (variables, classes, and functions) that can be tested using a unit test.
pytestprovides a simple yet powerful test runner that conforms to typical Python testing approaches, for example, using
pytestpre-installed. If you need to use a different stack, that's okay! Just add the following as a startup script:
double_arraythat takes an array (
a) and returns another array with the values in
pytest. This will prevents having to re-execute the notebook for each test, which will slow down the test run considerably. Inside of that shared scope on line 8, it executes the notebook in its entirety. You can also specify which cells you want to execute using notebook cell tags (click View > Cell Toolbar > Tags in the notebook to edit cell tags) or a given range of cells. More details can be found here.
testbookto load a reference to the
double_arrayfunction from the notebook. This function is then called directly using an array of values. An assertion is made about the return result of that array.
test_double_array_inject) also loads a reference to the
double_arrayfunction. It then injects an array directly into the notebook using the
tb.injectfunction. This array is then also referenced and used when calling the function and making the assertion. While this second method does not offer any benefits over the first, it is a good example of how code can easily be injected into a notebook using