@@ -428,12 +428,22 @@ def compare_outputs(self, test, ref, skip_compare=None):
428
428
test_keys = set (testing_outs .keys ())
429
429
430
430
if ref_keys - test_keys :
431
- self .comparison_traceback .append (
432
- cc .FAIL
433
- + "Missing output fields from running code: %s"
434
- % (ref_keys - test_keys )
435
- + cc .ENDC
436
- )
431
+ if ref_keys == {'evalue' , 'ename' }:
432
+ self .comparison_traceback .append (
433
+ cc .FAIL
434
+ + "Expected error:\n %s: %r" % (
435
+ '\n ' .join (reference_outs ['ename' ]),
436
+ '\n ' .join (reference_outs ['evalue' ])
437
+ )
438
+ + cc .ENDC
439
+ )
440
+ else :
441
+ self .comparison_traceback .append (
442
+ cc .FAIL
443
+ + "Missing output fields from running code: %s"
444
+ % (ref_keys - test_keys )
445
+ + cc .ENDC
446
+ )
437
447
return False
438
448
elif test_keys - ref_keys :
439
449
self .comparison_traceback .append (
@@ -582,6 +592,13 @@ def runtest(self):
582
592
# TODO: Only store if comparing with nbdime, to save on memory usage
583
593
self .test_outputs = outs
584
594
595
+ # Cells where the reference is not run, will not check outputs:
596
+ unrun = self .cell .execution_count is None
597
+ if unrun and self .cell .outputs :
598
+ self .raise_cell_error ('Unrun reference cell has outputs' )
599
+
600
+ cell_has_error = False
601
+
585
602
# Now get the outputs from the iopub channel
586
603
while True :
587
604
# The iopub channel broadcasts a range of messages. We keep reading
@@ -692,6 +709,7 @@ def runtest(self):
692
709
# cell execution. Therefore raise a cell error and pass the
693
710
# traceback information.
694
711
elif msg_type == 'error' :
712
+ cell_has_error = True
695
713
# Store error in output first
696
714
out ['ename' ] = reply ['ename' ]
697
715
out ['evalue' ] = reply ['evalue' ]
@@ -700,9 +718,9 @@ def runtest(self):
700
718
if not self .options ['check_exception' ]:
701
719
# Ensure we flush iopub before raising error
702
720
try :
703
- self . parent . kernel .await_idle (msg_id , self .output_timeout )
721
+ kernel .await_idle (msg_id , self .output_timeout )
704
722
except Empty :
705
- self .stop ()
723
+ kernel .stop ()
706
724
raise RuntimeError ('Timed out waiting for idle kernel!' )
707
725
traceback = '\n ' + '\n ' .join (reply ['traceback' ])
708
726
if out ['ename' ] == 'KeyboardInterrupt' and self .parent .timed_out :
@@ -718,10 +736,11 @@ def runtest(self):
718
736
719
737
outs [:] = coalesce_streams (outs )
720
738
721
- # Cells where the reference is not run, will not check outputs:
722
- unrun = self .cell .execution_count is None
723
- if unrun and self .cell .outputs :
724
- self .raise_cell_error ('Unrun reference cell has outputs' )
739
+ if self .options ['check_exception' ] and unrun and not cell_has_error :
740
+ # If unrun, we cannot rely on output comparison for checking errors
741
+ self .raise_cell_error (
742
+ "Expected error" ,
743
+ "Expected cell to produce an error, but none was produced!" )
725
744
726
745
# Compare if the outputs have the same number of lines
727
746
# and throw an error if it fails
0 commit comments