Skip to content

Commit bff46af

Browse files
authored
Merge pull request #82 from takluyver/i81
Fail output comparison on unexpected output
2 parents f18a696 + efd3bc1 commit bff46af

File tree

3 files changed

+100
-16
lines changed

3 files changed

+100
-16
lines changed

nbval/plugin.py

+24-13
Original file line numberDiff line numberDiff line change
@@ -404,17 +404,29 @@ def compare_outputs(self, test, ref, skip_compare=None):
404404
# The traceback from the comparison will be stored here.
405405
self.comparison_traceback = []
406406

407-
for key in reference_outs.keys():
407+
ref_keys = set(reference_outs.keys())
408+
test_keys = set(testing_outs.keys())
408409

409-
# Check if they have the same keys
410-
if key not in testing_outs.keys():
411-
self.comparison_traceback.append(
412-
bcolors.FAIL
413-
+ "missing key: TESTING %s != REFERENCE %s"
414-
% (testing_outs.keys(), reference_outs.keys())
415-
+ bcolors.ENDC)
416-
return False
410+
if ref_keys - test_keys:
411+
self.comparison_traceback.append(
412+
bcolors.FAIL
413+
+ "Missing output fields from running code: %s"
414+
% (ref_keys - test_keys)
415+
+ bcolors.ENDC
416+
)
417+
return False
418+
elif test_keys - ref_keys:
419+
self.comparison_traceback.append(
420+
bcolors.FAIL
421+
+ "Unexpected output fields from running code: %s"
422+
% (test_keys - ref_keys)
423+
+ bcolors.ENDC
424+
)
425+
return False
426+
427+
# If we've got to here, the two dicts must have the same set of keys
417428

429+
for key in reference_outs.keys():
418430
# Get output values for dictionary entries.
419431
# We use str() to be sure that the unicode key strings from the
420432
# reference are also read from the testing dictionary:
@@ -453,9 +465,9 @@ def compare_outputs(self, test, ref, skip_compare=None):
453465
def format_output_compare(self, key, left, right):
454466
"""Format an output for printing"""
455467
if isinstance(left, six.string_types):
456-
left = _trim_base64(output)
468+
left = _trim_base64(left)
457469
if isinstance(right, six.string_types):
458-
right = _trim_base64(output)
470+
right = _trim_base64(right)
459471

460472
self.comparison_traceback.append(
461473
bcolors.OKBLUE
@@ -474,7 +486,7 @@ def format_output_compare(self, key, left, right):
474486
else:
475487
# Fallback repr:
476488
self.comparison_traceback.append(
477-
+ " <<<<<<<<<<<< Reference output from ipynb file:"
489+
" <<<<<<<<<<<< Reference output from ipynb file:"
478490
+ bcolors.ENDC)
479491
self.comparison_traceback.append(_indent(left))
480492
self.comparison_traceback.append(
@@ -786,7 +798,6 @@ def coalesce_streams(outputs):
786798

787799
# process \r and \b characters
788800
for output in streams.values():
789-
backspace_pat
790801
old = output.text
791802
while len(output.text) < len(old):
792803
old = output.text
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"test\n"
13+
]
14+
}
15+
],
16+
"source": [
17+
"print('test')"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 1,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"print('test')"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 3,
32+
"metadata": {},
33+
"outputs": [
34+
{
35+
"name": "stdout",
36+
"output_type": "stream",
37+
"text": [
38+
"test\n"
39+
]
40+
}
41+
],
42+
"source": [
43+
"print('test')"
44+
]
45+
}
46+
],
47+
"metadata": {
48+
"kernelspec": {
49+
"display_name": "Python 3",
50+
"language": "python",
51+
"name": "python3"
52+
},
53+
"language_info": {
54+
"codemirror_mode": {
55+
"name": "ipython",
56+
"version": 3
57+
},
58+
"file_extension": ".py",
59+
"mimetype": "text/x-python",
60+
"name": "python",
61+
"nbconvert_exporter": "python",
62+
"pygments_lexer": "ipython3",
63+
"version": "3.6.3"
64+
}
65+
},
66+
"nbformat": 4,
67+
"nbformat_minor": 2
68+
}

tests/test_unit_tests_in_notebooks.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,15 @@ def test_print(filename, correctoutcome):
6363
else:
6464
exitcode = subprocess.call(command)
6565

66-
if correctoutcome is 'pass':
66+
if correctoutcome == 'pass':
67+
if exitcode != 0:
68+
raise AssertionError("Tests failed on ipynb (expected pass)")
6769
assert exitcode is 0
6870
print("The call of py.test has not reported errors - this is good.")
69-
elif correctoutcome is 'fail':
70-
assert exitcode is not 0
71+
elif correctoutcome == 'fail':
72+
if exitcode == 0:
73+
raise AssertionError("Tests passed on ipynb (expected fail)")
7174
print("The call of py.test has reported errors - " +
7275
"this is good for this test.")
76+
else:
77+
raise AssertionError("correctoutcome=%r (not 'pass' or 'fail')" % correctoutcome)

0 commit comments

Comments
 (0)