##// END OF EJS Templates
Don't use nbformat.current in core
Don't use nbformat.current in core

File last commit:

r18602:2377691a
r18604:d22b117f
Show More
test_execute.py
91 lines | 3.2 KiB | text/x-python | PythonLexer
Julia Evans
Add preprocessor to execute notebooks
r17074 """
Julia Evans
s/clearoutput/execute/
r17075 Module with tests for the execute preprocessor.
Julia Evans
Add preprocessor to execute notebooks
r17074 """
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import copy
MinRK
merge separate input/expected notebooks for execute preprocessor
r17101 import glob
Julia Evans
Start adding tests from runipy
r17082 import os
import re
Julia Evans
Add preprocessor to execute notebooks
r17074
from IPython.nbformat import current as nbformat
from .base import PreprocessorTestsBase
from ..execute import ExecutePreprocessor
MinRK
strip ANSI colors from tracebacks when normalizing outputs
r17091 from IPython.nbconvert.filters import strip_ansi
addr_pat = re.compile(r'0x[0-9a-f]{7,9}')
Julia Evans
Add preprocessor to execute notebooks
r17074
class TestExecute(PreprocessorTestsBase):
"""Contains test functions for execute.py"""
Julia Evans
Refactor assert_notebooks_equal
r17084 @staticmethod
MinRK
update execute preprocessor for msg spec 5
r17092 def normalize_output(output):
Julia Evans
Refactor assert_notebooks_equal
r17084 """
MinRK
update execute preprocessor for msg spec 5
r17092 Normalizes outputs for comparison.
Julia Evans
Refactor assert_notebooks_equal
r17084 """
MinRK
update execute preprocessor for msg spec 5
r17092 output = dict(output)
if 'metadata' in output:
del output['metadata']
MinRK
move mime-bundle data to rich output.data...
r18589 if 'text' in output:
output['text'] = re.sub(addr_pat, '<HEXADDR>', output['text'])
if 'text/plain' in output.get('data', {}):
output['data']['text/plain'] = \
re.sub(addr_pat, '<HEXADDR>', output['data']['text/plain'])
MinRK
update execute preprocessor for msg spec 5
r17092 if 'traceback' in output:
MinRK
strip ANSI colors from tracebacks when normalizing outputs
r17091 tb = []
MinRK
update execute preprocessor for msg spec 5
r17092 for line in output['traceback']:
MinRK
strip ANSI colors from tracebacks when normalizing outputs
r17091 tb.append(strip_ansi(line))
MinRK
update execute preprocessor for msg spec 5
r17092 output['traceback'] = tb
MinRK
strip ANSI colors from tracebacks when normalizing outputs
r17091
MinRK
update execute preprocessor for msg spec 5
r17092 return output
Julia Evans
Start adding tests from runipy
r17082
def assert_notebooks_equal(self, expected, actual):
MinRK
update nbconvert to nbformat 4
r18580 expected_cells = expected['cells']
actual_cells = actual['cells']
MinRK
address review from takluyver...
r18602 self.assertEqual(len(expected_cells), len(actual_cells))
Julia Evans
Start adding tests from runipy
r17082
Julia Evans
Refactor assert_notebooks_equal
r17084 for expected_cell, actual_cell in zip(expected_cells, actual_cells):
expected_outputs = expected_cell.get('outputs', [])
actual_outputs = actual_cell.get('outputs', [])
MinRK
update execute preprocessor for msg spec 5
r17092 normalized_expected_outputs = list(map(self.normalize_output, expected_outputs))
normalized_actual_outputs = list(map(self.normalize_output, actual_outputs))
MinRK
address review from takluyver...
r18602 self.assertEqual(normalized_expected_outputs, normalized_actual_outputs)
Julia Evans
Start adding tests from runipy
r17082
MinRK
s/prompt_number/execution_count in nbformat 4
r18587 expected_execution_count = expected_cell.get('execution_count', None)
actual_execution_count = actual_cell.get('execution_count', None)
MinRK
address review from takluyver...
r18602 self.assertEqual(expected_execution_count, actual_execution_count)
Jessica B. Hamrick
Add test for prompt numbers
r17823
Julia Evans
Add preprocessor to execute notebooks
r17074
def build_preprocessor(self):
"""Make an instance of a preprocessor"""
preprocessor = ExecutePreprocessor()
preprocessor.enabled = True
return preprocessor
Julia Evans
Start adding tests from runipy
r17082
Julia Evans
Add preprocessor to execute notebooks
r17074 def test_constructor(self):
"""Can a ExecutePreprocessor be constructed?"""
self.build_preprocessor()
Julia Evans
Start adding tests from runipy
r17082
def test_run_notebooks(self):
"""Runs a series of test notebooks and compares them to their actual output"""
current_dir = os.path.dirname(__file__)
MinRK
merge separate input/expected notebooks for execute preprocessor
r17101 input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb'))
Julia Evans
Start adding tests from runipy
r17082 for filename in input_files:
MinRK
merge separate input/expected notebooks for execute preprocessor
r17101 with open(os.path.join(current_dir, 'files', filename)) as f:
Julia Evans
Start adding tests from runipy
r17082 input_nb = nbformat.read(f, 'ipynb')
res = self.build_resources()
preprocessor = self.build_preprocessor()
Jessica B. Hamrick
Add test for prompt numbers
r17823 cleaned_input_nb = copy.deepcopy(input_nb)
MinRK
update nbconvert to nbformat 4
r18580 for cell in cleaned_input_nb.cells:
MinRK
s/prompt_number/execution_count in nbformat 4
r18587 if 'execution_count' in cell:
del cell['execution_count']
Jessica B. Hamrick
Add test for prompt numbers
r17823 cell['outputs'] = []
output_nb, _ = preprocessor(cleaned_input_nb, res)
MinRK
merge separate input/expected notebooks for execute preprocessor
r17101 self.assert_notebooks_equal(output_nb, input_nb)