forked from paulgb/runipy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
57 lines (44 loc) · 2.09 KB
/
test.py
File metadata and controls
57 lines (44 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import unittest
from glob import glob
from os import path, chdir
import re
from IPython.nbformat.current import read
from runipy.notebook_runner import NotebookRunner
class TestRunipy(unittest.TestCase):
maxDiff = 100000
def prepare_cell(self, cell):
cell = dict(cell)
if 'metadata' in cell:
del cell['metadata']
if 'text' in cell:
# don't match object's id; also happens to fix incompatible
# results between IPython2 and IPython3 (which prints "object" instead
# of "at [id]"
cell['text'] = re.sub('at 0x[0-9a-f]{7,9}', 'object', cell['text'])
if 'traceback' in cell:
cell['traceback'] = [re.sub('\x1b\\[[01];\\d\\dm', '', line) for line in cell['traceback']]
return cell
def assert_notebooks_equal(self, expected, actual):
self.assertEquals(len(expected['worksheets'][0]['cells']),
len(actual['worksheets'][0]['cells']))
for expected_out, actual_out in zip(expected['worksheets'][0]['cells'],
actual['worksheets'][0]['cells']):
for k in set(expected_out).union(actual_out):
if k == 'outputs':
self.assertEquals(len(expected_out[k]), len(actual_out[k]))
for e, a in zip(expected_out[k], actual_out[k]):
e = self.prepare_cell(e)
a = self.prepare_cell(a)
self.assertEquals(a, e)
def testRunNotebooks(self):
notebook_dir = path.join('tests', 'input')
for notebook_path in glob(path.join(notebook_dir, '*.ipynb')):
notebook_file = path.basename(notebook_path)
print notebook_file
expected_file = path.join('tests', 'expected', notebook_file)
runner = NotebookRunner(read(open(notebook_path), 'json'), working_dir=notebook_dir)
runner.run_notebook(True)
expected = read(open(expected_file), 'json')
self.assert_notebooks_equal(expected, runner.nb)
if __name__ == '__main__':
unittest.main()