3. Testing and Debugging Jupyter Notebooks¶
3.1. Running Example¶
[ ]:
def add(a, b):
"""Return the sum of a and b."""
sum = a
return sum
3.2. Doctest¶
[3]:
def add(a, b):
"""Return the sum of a and b.
>>> add(2, 2)
4
"""
sum = a
return sum
import doctest
doctest.testmod(verbose=True)
Trying:
add(2, 2)
Expecting:
4
**********************************************************************
File "__main__", line 4, in __main__.add
Failed example:
add(2, 2)
Expected:
4
Got:
2
1 items had no tests:
__main__
**********************************************************************
1 items had failures:
1 of 1 in __main__.add
1 tests in 2 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.
[3]:
TestResults(failed=1, attempted=1)
3.3. Unittest¶
[ ]:
import unittest
class TestNotebook(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 2), 5)
unittest.main(argv=[''], verbosity=2, exit=False)
test_add (__main__.TestNotebook) ... FAIL
======================================================================
FAIL: test_add (__main__.TestNotebook)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<ipython-input-4-4ac71e015a29>", line 7, in test_add
self.assertEqual(add(2, 2), 5)
AssertionError: 2 != 5
----------------------------------------------------------------------
Ran 1 test in 0.003s
FAILED (failures=1)
<unittest.main.TestProgram at 0x7f9fdcd4d4a8>
3.4. Debugging a Failed Test¶
[4]:
def add(a, b):
"""Return the sum of a and b.
>>> add(2, 2)
4
"""
sum = a
import pdb; pdb.set_trace()
return sum
import doctest
doctest.testmod(verbose=True)
Trying:
add(2, 2)
Expecting:
4
> <ipython-input-4-c9a7cd1fc273>(9)add()
-> return sum
(Pdb) sum
2
(Pdb) q
**********************************************************************
File "__main__", line 4, in __main__.add
Failed example:
add(2, 2)
Exception raised:
Traceback (most recent call last):
File "/usr/lib/python3.6/doctest.py", line 1330, in __run
compileflags, 1), test.globs)
File "<doctest __main__.add[0]>", line 1, in <module>
add(2, 2)
File "<ipython-input-4-c9a7cd1fc273>", line 9, in add
return sum
File "<ipython-input-4-c9a7cd1fc273>", line 9, in add
return sum
File "/usr/lib/python3.6/doctest.py", line 379, in trace_dispatch
return pdb.Pdb.trace_dispatch(self, *args)
File "/usr/lib/python3.6/bdb.py", line 51, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib/python3.6/bdb.py", line 70, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
1 items had no tests:
__main__
**********************************************************************
1 items had failures:
1 of 1 in __main__.add
1 tests in 2 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.
[4]:
TestResults(failed=1, attempted=1)