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.testbook
and pytest
Python libraries and unit tests. testbook
is 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. pytest
provides a simple yet powerful test runner that conforms to typical Python testing approaches, for example, using assert
.testbook
and pytest
pre-installed. If you need to use a different stack, that's okay! Just add the following as a startup script:double_array
that takes an array (a
) and returns another array with the values in a
doubled.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.test_double_array
) uses testbook
to load a reference to the double_array
function 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_array
function. It then injects an array directly into the notebook using the tb.inject
function. 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 testbook
if necessary.testbook
on their documentation site. Take a look!