Created
March 31, 2017 16:37
-
-
Save WaylonWalker/e203b5a0043fdf3d7e2115d273ae9615 to your computer and use it in GitHub Desktop.
Unittesting_Jupyter_Notebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def plus_one(x): | |
""" | |
return x + 1 for numeric inputs | |
:numeric param x: input number to add one | |
:numeric return: x + 1 | |
""" | |
return x + 1 | |
def minus_one(x): | |
""" | |
return x - 1 for numeric inputs | |
:numeric param x: input number to subtract one | |
:numeric return: x - 1 | |
""" | |
return x-1 | |
class TestNotebook(unittest.TestCase): | |
""" | |
Test suite for this example | |
""" | |
def test_plus_one(self): | |
""" | |
test plus one function based on known solutions | |
""" | |
validation = [(1, 2), (2, 3), (100, 101), | |
(-1, 0), (-52, -51), (455, 456,)] | |
for pair in validation: | |
self.assertEqual( | |
plus_one(pair[0]), pair[1], | |
'\nThis is an example error message: \ | |
\n\t solution for minus_one(%s) is not valid, \ | |
valid solution is %s' | |
% (minus_one(pair[0]), pair[1])) | |
def test_minus_one(self): | |
""" | |
test minus_one function based on known solutions | |
""" | |
validation = [(2, 1), (3, 2), (101, 100), | |
(0, -1), (-51, -52), (456, 455)] | |
for pair in validation: | |
self.assertEqual( | |
minus_one(pair[0]), pair[1], | |
'\n\t solution for plus_one(%s) is not valid, \ | |
valid solution is %s' | |
% (minus_one(pair[0]), pair[1])) | |
def test_fail(self): | |
""" | |
create a failure using minus_one for example | |
""" | |
validation = [(2, 1), (3, 2), (101, 100), | |
(0, -1), (-51, -52), (456, 454)] | |
for pair in validation: | |
self.assertEqual( | |
minus_one(pair[0]), pair[1], | |
'\n\t solution for minus_one(%s) is not valid, \ | |
valid solution is %s' | |
% (minus_one(pair[0]), pair[1])) | |
suite = unittest.TestLoader().loadTestsFromModule(TestNotebook()) | |
results = unittest.TextTestRunner().run(suite) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an example of how I unit test jupyter notebooks. I have used this as a template several times, and have had great success with it.