Skip to content
Merged
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
13 changes: 13 additions & 0 deletions examples/morphdemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
default=0.5,
help="Delay between updates in seconds (default: 0.5). Set to 0 for no delay.",
)
parser.add_argument(
"-n",
"--frames",
type=int,
default=None,
help="Stop after this many updates of the output image (default: process every pixel).",
)
# add -m, --movie option to save the animation as a movie file
parser.add_argument(
"-m",
Expand Down Expand Up @@ -121,12 +128,18 @@ def save_frame() -> None:
save_frame()

# loop over all pixels in the input image, excluding the border
updates = 0
for v in range(1, img.shape[0] - 1):
if quit_requested or not plt.fignum_exists(fig.number):
break
if args.frames is not None and updates >= args.frames:
break
for u in range(1, img.shape[1] - 1):
if quit_requested or not plt.fignum_exists(fig.number):
break
if args.frames is not None and updates >= args.frames:
break
updates += 1
W = window.move(u, v) # highlight the window

# window is the 3x3 window centered on (u,v)
Expand Down
65 changes: 65 additions & 0 deletions tests/test_examples_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python
"""Run the scripts in the examples folder headless, as a smoke test.

Each example runs in its own Python process with the non-interactive ``Agg``
matplotlib backend, so ``plt.show`` and ``block=True`` return at once. A
``DeprecationWarning`` is turned into an error, so an example that uses a removed
or deprecated Toolbox API fails here rather than in front of a reader.

Examples that need a window, hardware, a server or large optional packages are
listed in ``SKIP`` with the reason. An example that is not listed is run, so a
new example is tested by default.
"""

import os
import shutil
import subprocess
import sys
from pathlib import Path

import pytest

EXAMPLES = Path(__file__).resolve().parents[1] / "examples"

SKIP = {
"bunny.py": "opens an Open3D window",
"showzip.py": "opens OpenCV windows and waits for key presses",
"randomdots.py": "interactive threshold window",
"ros_stream.py": "needs a rosbridge server",
"torch-yolo.py": "needs ultralytics and downloads a model",
"torch_seg.py": "needs torchvision and downloads a model",
}

# examples that fetch from the internet: a network failure skips, not fails
NETWORK = {"monalisa.py"}
NETWORK_ERRORS = ("URLError", "ConnectionError", "TimeoutError", "HTTPError", "gaierror")

# examples that run an external program: skipped when it is not installed
NEEDS_PROGRAM = {"blobs2.py": "dot"} # Graphviz

# morphdemo redraws the figure for every pixel, which is slow on a CI runner; a
# few frames exercise the same code as all of them
ARGS = {"morphdemo.py": ["--delay", "0", "--frames", "5"]}


@pytest.mark.parametrize("script", sorted(p.name for p in EXAMPLES.glob("*.py")))
def test_example_runs(script):
if script in SKIP:
pytest.skip(SKIP[script])
if script in NEEDS_PROGRAM and shutil.which(NEEDS_PROGRAM[script]) is None:
pytest.skip(f"needs {NEEDS_PROGRAM[script]} on the PATH")

# UTF-8 output, since examples print maths symbols that the Windows default cannot encode
env = dict(os.environ, MPLBACKEND="Agg", PYTHONUTF8="1", PYTHONIOENCODING="utf-8")
result = subprocess.run(

Check failure on line 54 in tests/test_examples_run.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/test_examples_run.py#L54

Detected subprocess function 'run' without a static string.
[sys.executable, "-W", "error::DeprecationWarning", script, *ARGS.get(script, [])],
cwd=EXAMPLES,
env=env,
capture_output=True,
text=True,
timeout=120,
)
if result.returncode != 0:
if script in NETWORK and any(e in result.stderr for e in NETWORK_ERRORS):
pytest.skip("network not available")
pytest.fail(f"{script} exited with {result.returncode}:\n{result.stderr[-2000:]}")
Loading