Skip to content

Commit

Permalink
use testipynb for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lheagy committed Apr 5, 2018
1 parent 5edc7bf commit c8722d0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -10,7 +10,7 @@ env:

# Setup anaconda
before_install:
- if [ ${TRAVIS_PYTHON_VERSION:0:1} == "2" ]; then
- if [ ${TRAVIS_PYTHON_VERSION:0:1} == "2" ]; then
wget http://repo.continuum.io/miniconda/Miniconda-3.8.3-Linux-x86_64.sh -O miniconda.sh;
else
wget http://repo.continuum.io/miniconda/Miniconda3-3.8.3-Linux-x86_64.sh -O miniconda.sh;
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Expand Up @@ -7,3 +7,4 @@ git+git://github.com/simpeg/simpeg.git@dev
sphinx
sphinx_rtd_theme
nbsphinx
testipynb
3 changes: 2 additions & 1 deletion requirements.txt
Expand Up @@ -3,4 +3,5 @@ scipy
matplotlib
ipywidgets
jupyter
git+git://github.com/simpeg/simpeg.git@dev
discretize
SimPEG
73 changes: 11 additions & 62 deletions tests/test_notebooks.py
@@ -1,68 +1,17 @@
import unittest
import sys
import os
import subprocess

# Testing for the notebooks - use nbconvert to execute all cells of the
# notebook


TESTDIR = os.path.abspath(__file__)
NBDIR = os.path.sep.join(TESTDIR.split(os.path.sep)[:-2]) # 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)
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)
else:
print '\n <<<<< {0} FAILED >>>>> \n'.format(nbname)
print 'Captured Output: \n {0}'.format(err)

self.assertTrue(check == 0)

return test_func


def tearDown():
subprocess.call(['find', '{0}'.format(os.path.sep.join(TESTDIR.split(os.path.sep)[:-1])),
'-type', 'f', '-name', '*.html', '-delete'])


attrs = dict()
nbpaths, nbnames = setUp()
import testipynb
import unittest

# build test for each notebook
for i, nb in enumerate(nbnames):
attrs['test_'+nb] = get(nb, nbpaths[i])
NBDIR = os.path.sep.join(
os.path.abspath(__file__).split(os.path.sep)[:-2] + ['notebooks']
)
# IGNORE = ["TEM_VerticalConductor_1D_stiched_invrsion"]

# create class to unit test notebooks
TestNotebooks = type('TestNotebooks', (unittest.TestCase,), attrs)
class TestNotebooks(unittest.TestCase):

def test_notebooks(self):
Test = testipynb.TestNotebooks(directory=NBDIR, timeout=1800)
self.assertTrue(Test.run_tests())

if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
tearDown()

0 comments on commit c8722d0

Please sign in to comment.