Skip to content

Video editing with Python

Remove tests with graphical interface when no gui available

osaajani82b170039c3e
diff --git a/tests/test_examples.py b/tests/test_examples.py
new file mode 100644
index 00000000..2c3fe228
--- /dev/null
+++ b/tests/test_examples.py
@@ -0,0 +1,43 @@
+"""MoviePy examples tests."""
+
+import os
+
+import numpy as np
+
+import pytest
+
+from moviepy.video.io.bindings import mplfig_to_npimage
+from moviepy.video.VideoClip import VideoClip
+
+
+try:
+ import matplotlib.pyplot
+except ImportError:
+ matplotlib = None
+else:
+ matplotlib = True
+
+
[email protected](not matplotlib, reason="no matplotlib")
+def test_matplotlib_simple_example(util):
+ import matplotlib.pyplot as plt
+
+ plt.switch_backend("Agg")
+
+ x = np.linspace(-2, 2, 200)
+ duration = 0.5
+
+ fig, ax = plt.subplots()
+
+ def make_frame(t):
+ ax.clear()
+ ax.plot(x, np.sinc(x**2) + np.sin(x + 2 * np.pi / duration * t), lw=3)
+ ax.set_ylim(-1.5, 2.5)
+ return mplfig_to_npimage(fig)
+
+ animation = VideoClip(make_frame, duration=duration)
+
+ filename = os.path.join(util.TMP_DIR, "matplotlib.gif")
+ animation.write_gif(filename, fps=20)
+
+ assert os.path.isfile(filename)
diff --git a/tests/test_preview.py b/tests/test_preview.py
new file mode 100644
index 00000000..98bf5e76
--- /dev/null
+++ b/tests/test_preview.py
@@ -0,0 +1,74 @@
+"""MoviePy editor tests."""
+
+import importlib
+import io
+import sys
+from contextlib import redirect_stdout
+
+import pytest
+
+from moviepy.audio.AudioClip import AudioClip
+from moviepy.video.VideoClip import VideoClip
+
+
+def test_preview_methods():
+ stdout = io.StringIO()
+ with redirect_stdout(stdout):
+ try:
+ preview_module = importlib.import_module("moviepy.video.io.preview")
+ assert preview_module.preview.__hash__() != VideoClip.preview.__hash__()
+ except ImportError:
+ editor_module = importlib.import_module("moviepy.editor")
+ with pytest.raises(ImportError) as exc:
+ VideoClip.preview(True)
+ assert str(exc.value) == "clip.preview requires Pygame installed"
+
+ with pytest.raises(ImportError) as exc:
+ VideoClip.show(True)
+ assert str(exc.value) == "clip.show requires Pygame installed"
+
+ with pytest.raises(ImportError) as exc:
+ AudioClip.preview(True)
+ assert str(exc.value) == "clip.preview requires Pygame installed"
+ else:
+ editor_module = importlib.import_module("moviepy.editor")
+ assert (
+ editor_module.VideoClip.preview.__hash__()
+ == preview_module.preview.__hash__()
+ )
+ finally:
+ if "moviepy.editor" in sys.modules:
+ del sys.modules["moviepy.editor"]
+
+ try:
+ importlib.import_module("matplotlib.pyplot")
+ except ImportError:
+ editor_module = importlib.import_module("moviepy.editor")
+ with pytest.raises(ImportError) as exc:
+ editor_module.sliders()
+
+ assert str(exc.value) == "sliders requires matplotlib installed"
+
+ del sys.modules["moviepy.editor"]
+ else:
+ del sys.modules["matplotlib.pyplot"]
+
+ del sys.modules["moviepy"]
+
+
+def test__init__preview_methods():
+ moviepy_module = importlib.import_module("moviepy")
+
+ with pytest.raises(ImportError) as exc:
+ moviepy_module.VideoClip.preview(True)
+ assert str(exc.value) == "clip.preview requires importing from moviepy.editor"
+
+ with pytest.raises(ImportError) as exc:
+ moviepy_module.VideoClip.show(True)
+ assert str(exc.value) == "clip.show requires importing from moviepy.editor"
+
+ del sys.modules["moviepy"]
+
+
+if __name__ == "__main__":
+ pytest.main()