-
-
Save AlfonsoRReyes/b9b1994611969b40a3367af740f4a41c to your computer and use it in GitHub Desktop.
Unit testing for jupyter notebooks
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
The MIT License (MIT) | |
Copyright (c) 2016 Lindsey Heagy | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
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
numpy | |
scipy | |
matplotlib | |
ipython | |
jupyter | |
ipywidgets |
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 | |
import sys | |
import os | |
import subprocess | |
# Testing for the notebooks - use nbconvert to execute all cells of the | |
# notebook | |
# For testing on TravisCI, be sure to include a requirements.txt that | |
# includes jupyter so that you run on the most up-to-date version. | |
TESTDIR = os.path.abspath(__file__) # absolutepath of this file | |
NBDIR = os.path.sep.join(TESTDIR.split(os.path.sep)[:-2] + ['notebooks/']) # where are the notebooks? | |
def setUp(): | |
nbpaths = [] # list of notebooks, with file paths | |
nbnames = [] # list of notebook names (for making the tests) | |
# walk the test directory and find all notebooks | |
for dirname, dirnames, filenames in os.walk(NBDIR): | |
for filename in filenames: | |
if filename.endswith('.ipynb') and not filename.endswith('-checkpoint.ipynb'): | |
nbpaths.append(os.path.abspath(dirname) + os.path.sep + filename) # get abspath of notebook | |
nbnames.append(''.join(filename[:-6])) # strip off the file extension | |
return nbpaths, nbnames | |
def get(nbname, nbpath): | |
# use nbconvert to execute the notebook | |
def test_func(self): | |
print '\n--------------- Testing {0} ---------------'.format(nbname) | |
print ' {0}'.format(nbpath) | |
# execute the notebook using nbconvert to generate html | |
nbexe = subprocess.Popen(['jupyter', 'nbconvert', '{0}'.format(nbpath), | |
'--execute', | |
'--ExecutePreprocessor.timeout=120'], | |
stdin=subprocess.PIPE, stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
output, err = nbexe.communicate() | |
check = nbexe.returncode | |
if check == 0: | |
print '\n ..... {0} Passed ..... \n'.format(nbname) | |
# if passed remove the generated html file | |
subprocess.call(['rm', '{0}.html'.format( | |
os.path.sep.join(os.getcwd().split(os.path.sep) | |
+ [nbpath.split(os.path.sep)[-1][:-6]] | |
))]) | |
else: | |
print '\n <<<<< {0} FAILED >>>>> \n'.format(nbname) | |
print 'Captured Output: \n {0}'.format(err) | |
self.assertTrue(check == 0) | |
return test_func | |
attrs = dict() | |
nbpaths, nbnames = setUp() | |
# build test for each notebook | |
for i, nb in enumerate(nbnames): | |
attrs['test_'+nb] = get(nb, nbpaths[i]) | |
# create class to unit test notebooks | |
TestNotebooks = type('TestNotebooks', (unittest.TestCase,), attrs) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment