Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Commit

Permalink
added demo tests (#17)
Browse files Browse the repository at this point in the history
* added demo tests
  • Loading branch information
babbush authored and jarrodmcc committed Oct 5, 2017
1 parent d6c3b9f commit 9cc17ab
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 14 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 24 additions & 6 deletions examples/openfermionprojectq_demo.ipynb
Original file line number Diff line number Diff line change
@@ -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": {},
Expand All @@ -10,8 +26,10 @@
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import os\n",
Expand All @@ -38,7 +56,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {
"collapsed": true
},
Expand Down Expand Up @@ -69,7 +87,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {
"collapsed": true
},
Expand Down Expand Up @@ -114,7 +132,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -161,7 +179,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down
62 changes: 62 additions & 0 deletions openfermionprojectq/_demo_test.py
Original file line number Diff line number Diff line change
@@ -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, [])
6 changes: 1 addition & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9cc17ab

Please sign in to comment.