Skip to content

Commit

Permalink
Add unit tests for tutorials (iiasa#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
gidden authored and danielhuppmann committed Jan 25, 2018
1 parent 01d61a1 commit 618f03e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2
jobs:
build:
working_directory: ~/ixmp_dev
working_directory: ~/ixmp
docker:
- image: docker:17.05.0-ce-git
steps:
Expand All @@ -12,11 +12,11 @@ jobs:
command: pwd && ls
- run:
name: Build App Docker Image
command: docker build -t message_ix/message_ix:latest .
command: docker build -t ixmp/ixmp:latest .
- run:
name: Python2 Tests
command: docker run message_ix/message_ix:latest python2 -m pytest /message_ix/tests
command: docker run ixmp/ixmp:latest python2 -m pytest /ixmp/tests
- run:
name: Python3 Tests
command: docker run message_ix/message_ix:latest python3 -m pytest /message_ix/tests
command: docker run ixmp/ixmp:latest python3 -m pytest /ixmp/tests

10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM gidden/messageix-base

COPY . /message_ix
COPY . /ixmp
WORKDIR /
ENV MESSAGE_IX_PATH /message_ix
ENV IXMP_R_PATH /message_ix/ixmp
RUN cd /message_ix && python2 setup.py install
RUN cd /message_ix && python3 setup.py install
ENV MESSAGE_IX_PATH /ixmp
ENV IXMP_R_PATH /ixmp/ixmp
RUN cd /ixmp && python2 setup.py install
RUN cd /ixmp && python3 setup.py install
69 changes: 69 additions & 0 deletions tests/test_tutorials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import io
import os
import subprocess
import sys
import tempfile
import nbformat
import pytest

from testing_utils import here

from test_r import r_installed

xport_path = os.path.join(here, '..', 'tutorial', 'transport')

# taken from the execellent example here:
# https://blog.thedataincubator.com/2016/06/testing-jupyter-notebooks/


def _notebook_run(path, kernel=None):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
major_version = sys.version_info[0]
kernel = kernel or 'python{}'.format(major_version)
dirname, __ = os.path.split(path)
os.chdir(dirname)
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = [
"jupyter", "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=60",
"--ExecutePreprocessor.kernel_name={}".format(kernel),
"--output", fout.name, path]
subprocess.check_call(args)

nb = nbformat.read(io.open(fout.name, encoding='utf-8'),
nbformat.current_nbformat)

errors = [
output for cell in nb.cells if "outputs" in cell
for output in cell["outputs"] if output.output_type == "error"
]

return nb, errors


def test_py_transport():
fname = os.path.join(xport_path, 'py_transport.ipynb')
nb, errors = _notebook_run(fname)
assert errors == []


def test_py_transport_scenario():
fname = os.path.join(xport_path, 'py_transport_scenario.ipynb')
nb, errors = _notebook_run(fname)
assert errors == []


@pytest.mark.skipif(not r_installed(), reason='requires R to be installed')
def test_R_transport():
fname = os.path.join(xport_path, 'R_transport.ipynb')
nb, errors = _notebook_run(fname, kernel='IR')
assert errors == []


@pytest.mark.skipif(not r_installed(), reason='requires R to be installed')
def test_R_transport_scenario():
fname = os.path.join(xport_path, 'R_transport_scenario.ipynb')
nb, errors = _notebook_run(fname, kernel='IR')
assert errors == []

0 comments on commit 618f03e

Please sign in to comment.