Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add notebook testing + fixes accordingly (#67)
* Testing: Change notebook testing to pytest-notebook (current tests were skipped). Add try-except ImportError for pyspark code. Bugfix in hist.py that broke the advanced tutorial. Enhancement: Add %%capture to pip install inside of notebooks. Enhancement: Make package install in notebooks work with paths with spaces. * Update requirements-test.txt * Update test_notebooks.py * Pickle doesn't work with tests (not really a popmon-specific feature anyway). Changed the notebook to fix the issue, left the code for reference. * Increase timeout
- Loading branch information
Showing
6 changed files
with
62 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
flake8>=3.7.8 | ||
pytest>=4.0.2 | ||
nbconvert>=5.3.1 | ||
pytest-notebook>=0.6.1 | ||
nbconvert~=5.6.1 | ||
jupyter_client>=5.2.3 | ||
ipykernel>=5.1.3 | ||
black>=19.10b0 | ||
isort>=5.0.7 | ||
isort>=5.0.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,30 @@ | ||
import os | ||
import unittest | ||
|
||
import nbformat | ||
import pytest | ||
from jupyter_client.kernelspec import KernelSpecManager, NoSuchKernel | ||
from nbconvert.preprocessors import ExecutePreprocessor | ||
from nbconvert.preprocessors.execute import CellExecutionError | ||
|
||
from popmon import resources | ||
|
||
kernel_name = "python3" | ||
|
||
# check if jupyter python3 kernel can be opened. if kernel not found, skip unit tests below. | ||
try: | ||
km = KernelSpecManager() | ||
km.get_kernel_spec(kernel_name) | ||
kernel_found = True | ||
except NoSuchKernel: | ||
kernel_found = False | ||
from pytest_notebook.nb_regression import NBRegressionFixture | ||
|
||
from popmon.resources import notebook | ||
|
||
class NotebookTest(unittest.TestCase): | ||
"""Unit test notebook""" | ||
|
||
def run_notebook(self, notebook): | ||
""" Test notebook """ | ||
@pytest.fixture(scope="module") | ||
def nb_tester(): | ||
"""Test notebooks using pytest-notebook""" | ||
nb_regression = NBRegressionFixture( | ||
diff_ignore=( | ||
"/metadata/language_info", | ||
"/cells/*/execution_count", | ||
"/cells/*/outputs/*", | ||
), | ||
exec_timeout=1800, | ||
) | ||
return nb_regression | ||
|
||
# load notebook | ||
with open(notebook) as f: | ||
nb = nbformat.read(f, as_version=4) | ||
|
||
# execute notebook | ||
ep = ExecutePreprocessor(timeout=600, kernel_name=kernel_name) | ||
try: | ||
ep.preprocess(nb, {}) | ||
status = True | ||
except CellExecutionError: | ||
# store if failed | ||
status = False | ||
executed_notebook = os.getcwd() + "/" + notebook.split("/")[-1] | ||
with open(executed_notebook, mode="wt") as f: | ||
nbformat.write(nb, f) | ||
def test_notebook_basic(nb_tester): | ||
nb_tester.check(notebook("popmon_tutorial_basic.ipynb")) | ||
|
||
# check status | ||
self.assertTrue(status, "Notebook execution failed (%s)" % notebook) | ||
|
||
def test_notebook_advanced(nb_tester): | ||
nb_tester.check(notebook("popmon_tutorial_advanced.ipynb")) | ||
|
||
@pytest.mark.filterwarnings("ignore:Session._key_changed is deprecated") | ||
@pytest.mark.skipif(not kernel_found, reason=f"{kernel_name} kernel not found.") | ||
class PipelineNotebookTest(NotebookTest): | ||
"""Unit test notebook""" | ||
|
||
def test_basic_tutorial(self): | ||
self.run_notebook(resources.notebook("popmon_tutorial_basic.ipynb")) | ||
def test_notebook_incremental_data(nb_tester): | ||
nb_tester.check(notebook("popmon_tutorial_incremental_data.ipynb")) |