Skip to content

Commit 8ce43f8

Browse files
committed
test examples
1 parent 16d36f2 commit 8ce43f8

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

flask_admin/tests/test_examples.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
Test suite for flask-admin examples.
3+
4+
This test suite validates that each example in the examples directory
5+
can be started successfully and that the /admin endpoint returns HTTP 200.
6+
7+
Each example is run using the env in examples/<example>/pyproject.toml.
8+
"""
9+
10+
import subprocess
11+
import time
12+
from pathlib import Path
13+
14+
import pytest
15+
import requests # type: ignore
16+
17+
EXAMPLES_DIR = Path(__file__).parent.parent.parent / "examples"
18+
19+
20+
def get_example_directories() -> list[tuple[str, Path]]:
21+
"""
22+
Get all subdirectories in the examples directory that contain a main.py file.
23+
24+
Returns:
25+
List of tuples (example_name, example_path)
26+
"""
27+
if not EXAMPLES_DIR.exists():
28+
return []
29+
30+
examples = []
31+
for item in EXAMPLES_DIR.iterdir():
32+
if item.is_dir():
33+
main_py = item / "main.py"
34+
if main_py.exists():
35+
examples.append((item.name, item))
36+
37+
return sorted(examples)
38+
39+
40+
@pytest.mark.parametrize("example_name,example_path", get_example_directories())
41+
def test_example_runs(example_name: str, example_path: Path):
42+
"""
43+
Test that the example can be started and that the /admin endpoint returns HTTP 200.
44+
45+
Args:
46+
example_name: Name of the example.
47+
example_path: Path to the example directory.
48+
"""
49+
process = subprocess.Popen(
50+
[
51+
"uv",
52+
"run",
53+
"-q",
54+
"main.py",
55+
], # -q filters out uv warnings about different venv for the example
56+
cwd=example_path,
57+
stdout=subprocess.PIPE,
58+
stderr=subprocess.PIPE,
59+
)
60+
61+
try:
62+
# Wait for the server to start
63+
time.sleep(5)
64+
65+
# Check if the /admin endpoint is reachable
66+
response = requests.get("http://localhost:5000/admin")
67+
assert (
68+
response.status_code == 200
69+
), f"/admin endpoint returned {response.status_code}"
70+
finally:
71+
process.terminate()
72+
process.wait()
73+
stdout, stderr = process.communicate()
74+
if process.returncode != 0:
75+
raise AssertionError(
76+
f"Example '{example_name}' failed to run."
77+
# f"STDOUT: {stdout.decode()}"
78+
f"STDERR: {stderr.decode()}"
79+
)
80+
else:
81+
print(f"Example '{example_name}' ran successfully.")

0 commit comments

Comments
 (0)