Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/test-apps.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Test All Apps

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test-all:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Run tests script
run: ./scripts/test_apps.sh
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,4 @@ ml-models.ipr

.DS_Store
.databricks
.venv*
Empty file added dash-chatbot-app/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions dash-chatbot-app/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = .
24 changes: 24 additions & 0 deletions dash-chatbot-app/tests/test_model_serving_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from unittest.mock import patch
import sys
from model_serving_utils import query_endpoint

@patch("model_serving_utils.get_deploy_client")
def test_query_endpoint_chat_format(mock_get_client):
# Arrange
mock_client = mock_get_client.return_value
mock_client.predict.return_value = {
"choices": [{"message": {"role": "assistant", "content": "Hello there!"}}]
}

result = query_endpoint("dummy-endpoint", [{"role": "user", "content": "Hi"}], 400)
assert result == {"role": "assistant", "content": "Hello there!"}

@patch("model_serving_utils.get_deploy_client")
def test_query_endpoint_agent_format(mock_get_client):
mock_client = mock_get_client.return_value
mock_client.predict.return_value = {
"messages": [{"role": "tool", "content": "some tool call res"}, {"role": "assistant", "content": "Agent response."}]
}

result = query_endpoint("dummy-endpoint", [{"role": "user", "content": "Hi"}], 400)
assert result == {"role": "assistant", "content": "Agent response."}
2 changes: 2 additions & 0 deletions gradio-chatbot-app/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = .
24 changes: 24 additions & 0 deletions gradio-chatbot-app/tests/test_model_serving_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from unittest.mock import patch
import sys
from model_serving_utils import query_endpoint

@patch("model_serving_utils.get_deploy_client")
def test_query_endpoint_chat_format(mock_get_client):
# Arrange
mock_client = mock_get_client.return_value
mock_client.predict.return_value = {
"choices": [{"message": {"role": "assistant", "content": "Hello there!"}}]
}

result = query_endpoint("dummy-endpoint", [{"role": "user", "content": "Hi"}], 400)
assert result == {"role": "assistant", "content": "Hello there!"}

@patch("model_serving_utils.get_deploy_client")
def test_query_endpoint_agent_format(mock_get_client):
mock_client = mock_get_client.return_value
mock_client.predict.return_value = {
"messages": [{"role": "tool", "content": "some tool call res"}, {"role": "assistant", "content": "Agent response."}]
}

result = query_endpoint("dummy-endpoint", [{"role": "user", "content": "Hi"}], 400)
assert result == {"role": "assistant", "content": "Agent response."}
90 changes: 90 additions & 0 deletions scripts/test_apps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env bash
set -euo pipefail

GLOBAL_TEST_REQS="test-requirements.txt"

UNTESTED_APPS=(
dash-data-app-obo-user
dash-data-app
dash-hello-world-app
flask-hello-world-app
gradio-data-app-obo-user
gradio-data-app
gradio-hello-world-app
shiny-data-app-obo-user
shiny-data-app
shiny-hello-world-app
streamlit-data-app-obo-user
streamlit-data-app
streamlit-hello-world-app
shiny-chatbot-app
gradio-chatbot-app
dash-chatbot-app
)

is_untested() {
for app in "${UNTESTED_APPS[@]}"; do
[[ "$1" == "$app" ]] && return 0
done
return 1
}

echo "🔍 Checking for test coverage..."

missing_tests=()
for dir in */; do
base=$(basename "$dir")

if is_untested "$base"; then
continue
fi

if [[ -f "$dir/app.yaml" ]]; then
test_dir="${base}/tests"
if [[ ! -d "$test_dir" ]]; then
missing_tests+=("$base")
fi
fi
done

if (( ${#missing_tests[@]} > 0 )); then
echo "❌ The following apps have app.yaml but no tests:"
for app in "${missing_tests[@]}"; do
echo " - $app"
done
echo "Please add tests under tests/<app-name>/ or add to UNTESTED_APPS."
exit 1
fi

echo "✅ All testable apps have test coverage. Running tests..."

for dir in */; do
base=$(basename "$dir")

if is_untested "$base"; then
echo "🟡 Skipping $base (allowlisted as an untested app)"
continue
fi

if [[ ! -f "$dir/app.yaml" ]]; then
echo "⚠️ Skipping $base (no app.yaml)"
continue
fi

echo "🔧 Testing $base..."

VENV_DIR=".venv-${base}"
python3 -m venv "$VENV_DIR"
source "$VENV_DIR/bin/activate"

pip install -U pip
pip install -r "$GLOBAL_TEST_REQS"
pip install -r "$dir/requirements.txt"

test_dir="${base}/tests"
echo "🧪 Running tests in $test_dir from $dir"
pytest --cov="$dir" "$test_dir" --rootdir="$dir"

deactivate
rm -rf "$VENV_DIR"
done
2 changes: 2 additions & 0 deletions shiny-chatbot-app/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = .
24 changes: 24 additions & 0 deletions shiny-chatbot-app/tests/test_model_serving_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from unittest.mock import patch
import sys
from model_serving_utils import query_endpoint

@patch("model_serving_utils.get_deploy_client")
def test_query_endpoint_chat_format(mock_get_client):
# Arrange
mock_client = mock_get_client.return_value
mock_client.predict.return_value = {
"choices": [{"message": {"role": "assistant", "content": "Hello there!"}}]
}

result = query_endpoint("dummy-endpoint", [{"role": "user", "content": "Hi"}], 400)
assert result == {"role": "assistant", "content": "Hello there!"}

@patch("model_serving_utils.get_deploy_client")
def test_query_endpoint_agent_format(mock_get_client):
mock_client = mock_get_client.return_value
mock_client.predict.return_value = {
"messages": [{"role": "tool", "content": "some tool call res"}, {"role": "assistant", "content": "Agent response."}]
}

result = query_endpoint("dummy-endpoint", [{"role": "user", "content": "Hi"}], 400)
assert result == {"role": "assistant", "content": "Agent response."}
2 changes: 2 additions & 0 deletions streamlit-chatbot-app/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = .
18 changes: 18 additions & 0 deletions streamlit-chatbot-app/tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from unittest.mock import patch
import streamlit.testing.v1 as st_test
from pathlib import Path

@patch("model_serving_utils.query_endpoint")
def test_app_renders_and_responds(mock_query):
mock_query.return_value = {"role": "assistant", "content": "Mock response"}
os.environ["SERVING_ENDPOINT"] = "dummy-endpoint"
# Initial render
test_dir = Path(__file__).resolve().parent
app_path = test_dir.parent / "app.py"
app = st_test.AppTest.from_file(str(app_path))
app.run()
# Set chat input and rerender
app.chat_input[0].set_value("Hi there!")
app.run()
assert "Mock response" in set([md.value for md in app.markdown])
24 changes: 24 additions & 0 deletions streamlit-chatbot-app/tests/test_model_serving_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from unittest.mock import patch
import sys
from model_serving_utils import query_endpoint

@patch("model_serving_utils.get_deploy_client")
def test_query_endpoint_chat_format(mock_get_client):
# Arrange
mock_client = mock_get_client.return_value
mock_client.predict.return_value = {
"choices": [{"message": {"role": "assistant", "content": "Hello there!"}}]
}

result = query_endpoint("dummy-endpoint", [{"role": "user", "content": "Hi"}], 400)
assert result == {"role": "assistant", "content": "Hello there!"}

@patch("model_serving_utils.get_deploy_client")
def test_query_endpoint_agent_format(mock_get_client):
mock_client = mock_get_client.return_value
mock_client.predict.return_value = {
"messages": [{"role": "tool", "content": "some tool call res"}, {"role": "assistant", "content": "Agent response."}]
}

result = query_endpoint("dummy-endpoint", [{"role": "user", "content": "Hi"}], 400)
assert result == {"role": "assistant", "content": "Agent response."}
3 changes: 3 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest>=8.3.2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we can align the environment version with our current app runtime python packages for high fidelity testing https://src.dev.databricks.com/databricks-eng/universe@3cfd7ba4da9b32c458c0812898a482e03b127910/-/blob/apps/runtime/Dockerfile?L25-35

pytest-cov
dash[testing]==3.0.2