From 9cc17abfeac94a88bcd8bf611aa91468721e6e9d Mon Sep 17 00:00:00 2001 From: Ryan Babbush Date: Wed, 4 Oct 2017 17:01:42 -0700 Subject: [PATCH] added demo tests (#17) * added demo tests --- .travis.yml | 7 +-- examples/openfermionprojectq_demo.ipynb | 30 +++++++++--- openfermionprojectq/_demo_test.py | 62 +++++++++++++++++++++++++ requirements.txt | 6 +-- 4 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 openfermionprojectq/_demo_test.py diff --git a/.travis.yml b/.travis.yml index ea53310..2e89d0d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,13 +36,14 @@ before_install: install: - if [ "${PYTHON:0:1}" = "3" ]; then export PY=3; fi - - pip$PY install --only-binary=numpy,scipy numpy scipy - - pip$PY install pybind11 + - pip$PY install --only-binary=numpy numpy + - pip$PY install --only-binary=scipy scipy + - pip$PY install --only-binary=jupyter jupyter + - pip$PY install --only-binary=matplotlib matplotlib - pip$PY install -r requirements.txt - pip$PY install pytest-cov - pip$PY install coveralls -# command to run tests script: export OMP_NUM_THREADS=1 && pytest openfermionprojectq --cov openfermionprojectq after_success: diff --git a/examples/openfermionprojectq_demo.ipynb b/examples/openfermionprojectq_demo.ipynb index 8076e71..7b9de64 100644 --- a/examples/openfermionprojectq_demo.ipynb +++ b/examples/openfermionprojectq_demo.ipynb @@ -1,5 +1,21 @@ { "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# This code block is for automatic testing purposes, please ignore.\n", + "try:\n", + " import openfermionprojectq\n", + "except:\n", + " import os\n", + " os.chdir('..')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -10,8 +26,10 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, + "execution_count": 2, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "import os\n", @@ -38,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "collapsed": true }, @@ -69,7 +87,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": true }, @@ -114,7 +132,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -161,7 +179,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { diff --git a/openfermionprojectq/_demo_test.py b/openfermionprojectq/_demo_test.py new file mode 100644 index 0000000..41f4590 --- /dev/null +++ b/openfermionprojectq/_demo_test.py @@ -0,0 +1,62 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests the code in the examples directory of the git repo.""" +import nbformat +import os +import subprocess +import sys +import tempfile +import unittest + + +class ExampleTest(unittest.TestCase): + + def setUp(self): + this_directory = os.path.realpath(__file__) + string_length = len(this_directory) + directory = this_directory[:(string_length - 33)] + '/examples/' + demo_name = 'openfermionprojectq_demo.ipynb' + self.path = directory + demo_name + + def test_demo(self): + """Execute a notebook via nbconvert and collect output.""" + + # Determine if python 2 or 3 is being used. + major_version, minor_version = sys.version_info[:2] + if major_version == 2 or minor_version == 6: + version = str(major_version) + + # Run ipython notebook. + with tempfile.NamedTemporaryFile(suffix='.ipynb') as output_file: + args = ['jupyter', + 'nbconvert', + '--to', + 'notebook', + '--execute', + '--ExecutePreprocessor.timeout=60', + '--ExecutePreprocessor.kernel_name=python{}'.format( + version), + '--output', + output_file.name, + self.path] + subprocess.check_call(args) + output_file.seek(0) + nb = nbformat.read(output_file, nbformat.current_nbformat) + + # Parse output and make sure there are no errors. + errors = [output for cell in nb.cells if "outputs" in cell for + output in cell["outputs"] if + output.output_type == "error"] + else: + errors = [] + self.assertEqual(errors, []) diff --git a/requirements.txt b/requirements.txt index 03e7100..2fc8da7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,2 @@ -scipy>=0.18.0 -numpy>=1.11.0 openfermion==0.1a0 -projectq>=0.3.1 -pytest>=3.0 -future +projectq==0.3.5