test_execute.py
86 lines
| 2.9 KiB
| text/x-python
|
PythonLexer
Julia Evans
|
r17074 | """ | ||
Julia Evans
|
r17075 | Module with tests for the execute preprocessor. | ||
Julia Evans
|
r17074 | """ | ||
# Copyright (c) IPython Development Team. | ||||
# Distributed under the terms of the Modified BSD License. | ||||
import copy | ||||
Julia Evans
|
r17082 | import os | ||
import re | ||||
Julia Evans
|
r17074 | |||
from IPython.nbformat import current as nbformat | ||||
from .base import PreprocessorTestsBase | ||||
from ..execute import ExecutePreprocessor | ||||
MinRK
|
r17091 | from IPython.nbconvert.filters import strip_ansi | ||
addr_pat = re.compile(r'0x[0-9a-f]{7,9}') | ||||
Julia Evans
|
r17074 | |||
class TestExecute(PreprocessorTestsBase): | ||||
"""Contains test functions for execute.py""" | ||||
Julia Evans
|
r17084 | @staticmethod | ||
MinRK
|
r17092 | def normalize_output(output): | ||
Julia Evans
|
r17084 | """ | ||
MinRK
|
r17092 | Normalizes outputs for comparison. | ||
Julia Evans
|
r17084 | """ | ||
MinRK
|
r17092 | output = dict(output) | ||
if 'metadata' in output: | ||||
del output['metadata'] | ||||
if 'text' in output: | ||||
output['text'] = re.sub(addr_pat, '<HEXADDR>', output['text']) | ||||
if 'svg' in output: | ||||
del output['text'] | ||||
if 'traceback' in output: | ||||
MinRK
|
r17091 | tb = [] | ||
MinRK
|
r17092 | for line in output['traceback']: | ||
MinRK
|
r17091 | tb.append(strip_ansi(line)) | ||
MinRK
|
r17092 | output['traceback'] = tb | ||
MinRK
|
r17091 | |||
MinRK
|
r17092 | return output | ||
Julia Evans
|
r17082 | |||
def assert_notebooks_equal(self, expected, actual): | ||||
expected_cells = expected['worksheets'][0]['cells'] | ||||
actual_cells = actual['worksheets'][0]['cells'] | ||||
assert len(expected_cells) == len(actual_cells) | ||||
Julia Evans
|
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
|
r17092 | normalized_expected_outputs = list(map(self.normalize_output, expected_outputs)) | ||
normalized_actual_outputs = list(map(self.normalize_output, actual_outputs)) | ||||
Julia Evans
|
r17084 | assert normalized_expected_outputs == normalized_actual_outputs | ||
Julia Evans
|
r17082 | |||
Julia Evans
|
r17074 | |||
def build_preprocessor(self): | ||||
"""Make an instance of a preprocessor""" | ||||
preprocessor = ExecutePreprocessor() | ||||
preprocessor.enabled = True | ||||
return preprocessor | ||||
Julia Evans
|
r17082 | |||
Julia Evans
|
r17074 | def test_constructor(self): | ||
"""Can a ExecutePreprocessor be constructed?""" | ||||
self.build_preprocessor() | ||||
Julia Evans
|
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__) | ||||
input_files = os.listdir(os.path.join(current_dir, 'input')) | ||||
for filename in input_files: | ||||
if not filename.endswith(".ipynb"): | ||||
continue | ||||
with open(os.path.join(current_dir, 'input', filename)) as f: | ||||
input_nb = nbformat.read(f, 'ipynb') | ||||
with open(os.path.join(current_dir, 'expected', filename)) as f: | ||||
expected_nb = nbformat.read(f, 'ipynb') | ||||
res = self.build_resources() | ||||
preprocessor = self.build_preprocessor() | ||||
output_nb, _ = preprocessor(input_nb, res) | ||||
self.assert_notebooks_equal(output_nb, expected_nb) | ||||