Skip to content

Commit

Permalink
Use pytest-notebook again to avoid Permission denied error on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
birgits committed Jan 12, 2024
1 parent edc6862 commit 5f418bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 39 deletions.
51 changes: 14 additions & 37 deletions example/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
"""

import os
import subprocess
import tempfile
import nbformat
from example import modelchain_example as mc_e
from example import turbine_cluster_modelchain_example as tc_mc_e
from numpy.testing import assert_allclose
import pytest_notebook


class TestExamples:
Expand Down Expand Up @@ -55,48 +53,27 @@ def test_turbine_cluster_modelchain_example_flh(self):

def _notebook_run(self, path):
"""
Execute a notebook via nbconvert and collect output.
Returns (parsed nb object, execution errors)
Execute a notebook and collect output.
Returns execution errors.
"""
dirname, __ = os.path.split(path)
os.chdir(dirname)
with tempfile.NamedTemporaryFile(suffix=".ipynb", dir=dirname) as fout:
args = [
"jupyter",
"nbconvert",
"--to",
"notebook",
"--execute",
"--ExecutePreprocessor.timeout=600",
"--output",
fout.name,
path,
]
subprocess.check_call(args)

fout.seek(0)
nb = nbformat.read(fout, 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
notebook = pytest_notebook.notebook.load_notebook(path=path)
result = pytest_notebook.execution.execute_notebook(
notebook,
with_coverage=False,
timeout=600,
)
return result.exec_error

def test_modelchain_example_ipynb(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
nb, errors = self._notebook_run(
errors = self._notebook_run(
os.path.join(dir_path, "modelchain_example.ipynb")
)
assert errors == []
assert errors is None

def test_turbine_cluster_modelchain_example_ipynb(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
nb, errors = self._notebook_run(
errors = self._notebook_run(
os.path.join(dir_path, "turbine_cluster_modelchain_example.ipynb")
)
assert errors == []
assert errors is None
1 change: 1 addition & 0 deletions example/turbine_cluster_modelchain_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"source": [
"import os\n",
"import pandas as pd\n",
"import requests\n",
"\n",
"from windpowerlib import create_power_curve, TurbineClusterModelChain, WindFarm, WindTurbine, WindTurbineCluster\n",
"\n",
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def read(fname):
long_description=read("README.rst"),
long_description_content_type="text/x-rst",
zip_safe=False,
install_requires=["pandas >= 0.20.0", "requests"],
install_requires=["pandas >= 0.20.0", "requests"], # check
extras_require={
"dev": [
"pytest",
"jupyter",
"sphinx_rtd_theme",
"nbformat",
"numpy",
"matplotlib",
"pytest-notebook",
]
},
)

0 comments on commit 5f418bc

Please sign in to comment.