Skip to content

Commit

Permalink
Create test_notebooks.py
Browse files Browse the repository at this point in the history
  • Loading branch information
furniturewalatkNIH authored Nov 19, 2024
1 parent 023cd4c commit 8f96153
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions testing/test_notebooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import json
import pytest
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor

# Read configuration from a JSON file (injected via workflow)
def load_configuration(config_file="env.json"):
if os.path.exists(config_file):
with open(config_file) as f:
return json.load(f)
return {}

# Parameterize the tests with notebook paths
@pytest.mark.parametrize("notebook_path", [
"notebooks/GenAI/Gemini_Intro.ipynb",
])
def test_notebook_execution(notebook_path):
config = load_configuration()
parameters = {
"API_KEY": config.get("NOTEBOOK_PROJECT_ID", ""),
"DB_URL": config.get("NOTEBOOK_GCP_LOCATION", ""),
}

with open(notebook_path) as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')

# Inject parameters into the notebook via metadata
nb.metadata["parameters"] = parameters
ep.preprocess(nb, {'metadata': {'path': './'}})

0 comments on commit 8f96153

Please sign in to comment.