Skip to content

Commit

Permalink
Merge pull request #286 from fusion-energy/concise_tests
Browse files Browse the repository at this point in the history
shorter tests with less repetition
  • Loading branch information
shimwell authored Jun 6, 2024
2 parents b79dc97 + 36e8b69 commit 0929e88
Show file tree
Hide file tree
Showing 18 changed files with 157 additions and 641 deletions.
47 changes: 7 additions & 40 deletions tests/test_task_1.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@

"""
tests the create_isotope_plot from plotting_utils in the same way the examples
use the function.
"""

import os
import sys
from pathlib import Path

import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert.preprocessors.execute import CellExecutionError


def _notebook_run(path):
"""
Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
kernel_name = 'python%d' % sys.version_info[0]
this_file_directory = os.path.dirname(__file__)
errors = []

with open(path) as f:
nb = nbformat.read(f, as_version=4)
nb.metadata.get('kernelspec', {})['name'] = kernel_name
ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=300) #, allow_errors=True

try:
ep.preprocess(nb, {'metadata': {'path': this_file_directory}})
import pytest

except CellExecutionError as e:
if "SKIP" in e.traceback:
print(str(e.traceback).split("\n")[-2])
else:
raise e
from .utils import _notebook_run

return nb, errors

@pytest.mark.parametrize("filename", Path().rglob("tasks/task_01_*/*.ipynb"))
def test_task(filename):

def test_task_1():
for notebook in Path().rglob("tasks/tasks/task_01_*/*.ipynb"):
print(notebook)
nb, errors = _notebook_run(notebook)
assert errors == []
print(f"Attempting to run {filename}")
_, errors = _notebook_run(filename)
assert errors == []
47 changes: 7 additions & 40 deletions tests/test_task_10.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@

"""
tests the create_isotope_plot from plotting_utils in the same way the examples
use the function.
"""

import os
import sys
from pathlib import Path

import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert.preprocessors.execute import CellExecutionError


def _notebook_run(path):
"""
Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
kernel_name = 'python%d' % sys.version_info[0]
this_file_directory = os.path.dirname(__file__)
errors = []

with open(path) as f:
nb = nbformat.read(f, as_version=4)
nb.metadata.get('kernelspec', {})['name'] = kernel_name
ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=300) #, allow_errors=True

try:
ep.preprocess(nb, {'metadata': {'path': this_file_directory}})
import pytest

except CellExecutionError as e:
if "SKIP" in e.traceback:
print(str(e.traceback).split("\n")[-2])
else:
raise e
from .utils import _notebook_run

return nb, errors

@pytest.mark.parametrize("filename", Path().rglob("tasks/task_10_*/*.ipynb"))
def test_task(filename):

def test_task_10():
for notebook in Path().rglob("tasks/task_10_*/*.ipynb"):
print(notebook)
nb, errors = _notebook_run(notebook)
assert errors == []
print(f"Attempting to run {filename}")
_, errors = _notebook_run(filename)
assert errors == []
46 changes: 7 additions & 39 deletions tests/test_task_11.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,13 @@

"""
tests the create_isotope_plot from plotting_utils in the same way the examples
use the function.
"""

import os
import sys
from pathlib import Path

import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert.preprocessors.execute import CellExecutionError


def _notebook_run(path):
"""
Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
kernel_name = 'python%d' % sys.version_info[0]
this_file_directory = os.path.dirname(__file__)
errors = []

with open(path) as f:
nb = nbformat.read(f, as_version=4)
nb.metadata.get('kernelspec', {})['name'] = kernel_name
ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=900) #, allow_errors=True
import pytest

try:
ep.preprocess(nb, {'metadata': {'path': this_file_directory}})
from .utils import _notebook_run

except CellExecutionError as e:
if "SKIP" in e.traceback:
print(str(e.traceback).split("\n")[-2])
else:
raise e

return nb, errors
@pytest.mark.parametrize("filename", Path().rglob("tasks/task_11_*/*.ipynb"))
def test_task(filename):

def test_task_11():
for notebook in Path().rglob("tasks/task_11_*/*.ipynb"):
print(notebook)
nb, errors = _notebook_run(notebook)
assert errors == []
print(f"Attempting to run {filename}")
_, errors = _notebook_run(filename)
assert errors == []
53 changes: 7 additions & 46 deletions tests/test_task_12.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,13 @@

"""
tests the create_isotope_plot from plotting_utils in the same way the examples
use the function.
"""

import os
import sys
from pathlib import Path

import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert.preprocessors.execute import CellExecutionError


def _notebook_run(path):
"""
Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
kernel_name = 'python%d' % sys.version_info[0]
this_file_directory = os.path.dirname(__file__)
errors = []

with open(path) as f:
nb = nbformat.read(f, as_version=4)
nb.metadata.get('kernelspec', {})['name'] = kernel_name
ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=1400) #, allow_errors=True

try:
ep.preprocess(nb, {'metadata': {'path': this_file_directory}})

except CellExecutionError as e:
if "SKIP" in e.traceback:
print(str(e.traceback).split("\n")[-2])
else:
raise e
import pytest

return nb, errors
from .utils import _notebook_run


def test_task_12():
for notebook in Path().rglob("tasks/task_12_*/1*.ipynb"):
print(notebook)
nb, errors = _notebook_run(notebook)
assert errors == []
@pytest.mark.parametrize("filename", Path().rglob("tasks/task_12_*/*.ipynb"))
def test_task(filename):

for notebook in Path().rglob("tasks/task_12_*/2_.ipynb"):
nb, errors = _notebook_run(notebook)
assert errors == []

# task 3 is skipped as it causes errors, issue raised
print(f"Attempting to run {filename}")
_, errors = _notebook_run(filename)
assert errors == []
43 changes: 7 additions & 36 deletions tests/test_task_13.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@

"""
tests the create_isotope_plot from plotting_utils in the same way the examples
use the function.
"""

import os
import sys
from pathlib import Path

import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert.preprocessors.execute import CellExecutionError
import pytest

from .utils import _notebook_run

def _notebook_run(path):
"""
Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
kernel_name = 'python%d' % sys.version_info[0]
this_file_directory = os.path.dirname(__file__)
errors = []
with open(path) as f:
nb = nbformat.read(f, as_version=4)
nb.metadata.get('kernelspec', {})['name'] = kernel_name
ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=300) #, allow_errors=True
try:
ep.preprocess(nb, {'metadata': {'path': this_file_directory}})
except CellExecutionError as e:
if "SKIP" in e.traceback:
print(str(e.traceback).split("\n")[-2])
else:
raise e
return nb, errors

@pytest.mark.parametrize("filename", Path().rglob("tasks/task_13_*/*.ipynb"))
def test_task(filename):

def test_task_13():
for notebook in Path().rglob("tasks/task_13_*/*.ipynb"):
print(f'tests {notebook}')
nb, errors = _notebook_run(notebook)
assert errors == []
print(f"Attempting to run {filename}")
_, errors = _notebook_run(filename)
assert errors == []
47 changes: 7 additions & 40 deletions tests/test_task_14.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@

"""
tests the create_isotope_plot from plotting_utils in the same way the examples
use the function.
"""

import os
import sys
from pathlib import Path

import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert.preprocessors.execute import CellExecutionError


def _notebook_run(path):
"""
Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
kernel_name = 'python%d' % sys.version_info[0]
this_file_directory = os.path.dirname(__file__)
errors = []

with open(path) as f:
nb = nbformat.read(f, as_version=4)
nb.metadata.get('kernelspec', {})['name'] = kernel_name
ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=4000) #, allow_errors=True

try:
ep.preprocess(nb, {'metadata': {'path': this_file_directory}})
import pytest

except CellExecutionError as e:
if "SKIP" in e.traceback:
print(str(e.traceback).split("\n")[-2])
else:
raise e
from .utils import _notebook_run

return nb, errors

@pytest.mark.parametrize("filename", Path().rglob("tasks/task_14_*/*.ipynb"))
def test_task(filename):

def test_task_14():
for notebook in Path().rglob("tasks/task_14_*/*.ipynb"):
print(notebook)
nb, errors = _notebook_run(notebook)
assert errors == []
print(f"Attempting to run {filename}")
_, errors = _notebook_run(filename)
assert errors == []
47 changes: 7 additions & 40 deletions tests/test_task_15.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@

"""
tests the create_isotope_plot from plotting_utils in the same way the examples
use the function.
"""

import os
import sys
from pathlib import Path

import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbconvert.preprocessors.execute import CellExecutionError


def _notebook_run(path):
"""
Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
kernel_name = 'python%d' % sys.version_info[0]
this_file_directory = os.path.dirname(__file__)
errors = []

with open(path) as f:
nb = nbformat.read(f, as_version=4)
nb.metadata.get('kernelspec', {})['name'] = kernel_name
ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=1900) #, allow_errors=True

try:
ep.preprocess(nb, {'metadata': {'path': this_file_directory}})
import pytest

except CellExecutionError as e:
if "SKIP" in e.traceback:
print(str(e.traceback).split("\n")[-2])
else:
raise e
from .utils import _notebook_run

return nb, errors

@pytest.mark.parametrize("filename", Path().rglob("tasks/task_15_*/*.ipynb"))
def test_task(filename):

def test_task_14():
for notebook in Path().rglob("tasks/task_15_*/*.ipynb"):
print(notebook)
nb, errors = _notebook_run(notebook)
assert errors == []
print(f"Attempting to run {filename}")
_, errors = _notebook_run(filename)
assert errors == []
Loading

0 comments on commit 0929e88

Please sign in to comment.