Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

refactored test code architecture

Hugues Devimeuxc4ed7f2c141c
diff --git a/tests/conftest.py b/tests/conftest.py
index fc6d8f218..bac45335b 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -8,89 +8,32 @@ import sys
import logging
-class SceneTester:
- """Class used to test the animations.
-
- Parameters
- ----------
- scene_object: : class:`Scene`
- The scene to be tested
- config_scene : :class:`Dict`
- The configuration of the scene
- module_tested: :class: `str`
- The name of the module tested. i.e if we are testing functions of creation.py, the module will be "creation"
-
- Attributes
- -----------
- path_tests_medias : : class: `str`
- self-explanatory
- path_tests_data : : class: `str`
- self-explanatory
- scene : :class:`Scene`
- The scene tested
- """
-
- def __init__(self, scene_object, config_scene, module_tested, caching_needed=False):
- # Disable the the logs, (--quiet is broken) TODO
- logging.disable(logging.CRITICAL)
- self.path_tests_media = os.path.join(
- 'tests', 'tests_cache', module_tested)
- self.path_tests_data = os.path.join(
- 'tests', 'tests_data', module_tested)
-
- tex_dir, text_dir = None, None
- if caching_needed:
- text_dir = os.path.join(self.path_tests_media, scene_object.__name__, 'Text')
- tex_dir = os.path.join(self.path_tests_media, scene_object.__name__, 'Tex')
- conf_dirs = {'media_dir': None,
- 'video_dir': None,
- 'tex_dir': tex_dir,
- 'text_dir': text_dir,
- }
- # PROVISIONAL. To change when #98 is merged. TODO
- config.initialize_directories(conf_dirs)
- # By invoking this, the scene is rendered.
- self.scene = scene_object(**config_scene)
-
- def load_data(self):
- """Load the np.array of the last frame of a pre-rendered scene. If not found, throw FileNotFoundError."""
- with pytest.raises(FileNotFoundError) as e_info:
- data_loaded = np.load(os.path.join(
- self.path_tests_data, "{}.npy".format(str(self.scene))))
- raise FileNotFoundError('test_data not found !')
- assert (str(e_info.value) ==
- 'test_data not found !'), '{} does not seem have a pre-rendered frame for testing.'.format(str(self.scene).replace('Test', ''))
- return data_loaded
-
- def test(self):
- # self.scene.get_frame() gets the last frame of the animation (under the form of an numpy array).
- test_result = np.array_equal(self.scene.get_frame(), self.load_data())
- assert(test_result), "The frames don't match. {} has been modified. Please ignore if it was intended".format(
- str(self.scene).replace('Test', ''))
-
def pytest_addoption(parser):
- parser.addoption("--skip_end_to_end", action="store_true", default=False, help = "Will skip all the end-to-end tests. Useful when ffmpeg is not installed, e.g. on Windows jobs.")
+ parser.addoption("--skip_end_to_end", action="store_true", default=False,
+ help="Will skip all the end-to-end tests. Useful when ffmpeg is not installed, e.g. on Windows jobs.")
+
def pytest_configure(config):
- config.addinivalue_line("markers", "skip_end_to_end: mark test as end_to_end test")
+ config.addinivalue_line(
+ "markers", "skip_end_to_end: mark test as end_to_end test")
+
def pytest_collection_modifyitems(config, items):
if not config.getoption("--skip_end_to_end"):
return
- else:
- skip_end_to_end = pytest.mark.skip(reason="End to end test skipped due to --skip_end_to_end flag")
+ else:
+ skip_end_to_end = pytest.mark.skip(
+ reason="End to end test skipped due to --skip_end_to_end flag")
for item in items:
if "skip_end_to_end" in item.keywords:
item.add_marker(skip_end_to_end)
-def Tester():
- return SceneTester
[email protected](scope= "module")
-def python_version():
[email protected](scope="module")
+def python_version():
return "python3" if sys.platform == "darwin" else "python"
+
@pytest.fixture
def get_config_test():
"""Function used internally by pytest as a fixture. Return the Configuration for the scenes rendered. The config is the one used when
@@ -118,4 +61,3 @@ def get_config_test():
'start_at_animation_number': None
}
return CONFIG
-
diff --git a/tests/test_creation.py b/tests/test_creation.py
index 2d8492277..c9d97cad1 100644
--- a/tests/test_creation.py
+++ b/tests/test_creation.py
@@ -1,4 +1,5 @@
from manim import *
+from testing_utils import utils_test_scenes, get_scenes_to_test
class ShowCreationTest(Scene):
@@ -102,8 +103,5 @@ class ShrinkToCenterTest(Scene):
self.play(ShrinkToCenter(square))
-def test_scenes(get_config_test, Tester):
- CONFIG = get_config_test
- module_name = os.path.splitext(os.path.basename(__file__))[0].replace('test_', '')
- for _, scene_tested in inspect.getmembers(sys.modules[__name__], lambda m: inspect.isclass(m) and m.__module__ == __name__):
- Tester(scene_tested, CONFIG, module_name).test()
+def test_scenes(get_config_test):
+ utils_test_scenes(get_scenes_to_test(__name__), get_config_test, "creation")
diff --git a/tests/test_geometry.py b/tests/test_geometry.py
index 0a793cad1..e6116fbef 100644
--- a/tests/test_geometry.py
+++ b/tests/test_geometry.py
@@ -1,4 +1,5 @@
from manim import *
+from testing_utils import utils_test_scenes, get_scenes_to_test
class CoordinatesTest(Scene):
@@ -119,8 +120,5 @@ class RoundedRectangleTest(Scene):
self.play(Animation(a))
-def test_scenes(get_config_test, Tester):
- CONFIG = get_config_test
- module_name = os.path.splitext(os.path.basename(__file__))[0].replace('test_', '')
- for _, scene_tested in inspect.getmembers(sys.modules[__name__], lambda m: inspect.isclass(m) and m.__module__ == __name__):
- Tester(scene_tested, CONFIG, module_name).test()
\ No newline at end of file
+def test_scenes(get_config_test):
+ utils_test_scenes(get_scenes_to_test(__name__), get_config_test, "geometry")
\ No newline at end of file
diff --git a/tests/test_graph.py b/tests/test_graph.py
index 3825f0c36..72f317d29 100644
--- a/tests/test_graph.py
+++ b/tests/test_graph.py
@@ -1,4 +1,6 @@
from manim import *
+from testing_utils import utils_test_scenes, get_scenes_to_test
+
class PlotFunctions(GraphScene):
CONFIG = {
@@ -20,10 +22,5 @@ class PlotFunctions(GraphScene):
self.play(Animation(f))
-def test_scenes(get_config_test, Tester):
- CONFIG = get_config_test
- module_name = os.path.splitext(os.path.basename(__file__))[
- 0].replace('test_', '')
- for name, scene_tested in inspect.getmembers(sys.modules[__name__], lambda m: inspect.isclass(m) and m.__module__ == __name__):
- Tester(scene_tested, CONFIG, module_name, caching_needed=True).test()
-
\ No newline at end of file
+def test_scenes(get_config_test):
+ utils_test_scenes(get_scenes_to_test(__name__), get_config_test, "graph", caching_needed=True)
\ No newline at end of file
diff --git a/tests/test_indication.py b/tests/test_indication.py
index 8c64112ba..f4161ff83 100644
--- a/tests/test_indication.py
+++ b/tests/test_indication.py
@@ -1,4 +1,5 @@
from manim import *
+from testing_utils import utils_test_scenes, get_scenes_to_test
class FocusOnTest(Scene):
@@ -83,10 +84,7 @@ class TurnInsideOutTest(Scene):
self.play(TurnInsideOut(square))
-def test_scenes(get_config_test, Tester):
- CONFIG = get_config_test
- module_name = os.path.splitext(os.path.basename(__file__))[0].replace('test_', '')
- for _, scene_tested in inspect.getmembers(sys.modules[__name__], lambda m: inspect.isclass(m) and m.__module__ == __name__):
- Tester(scene_tested, CONFIG, module_name).test()
+def test_scenes(get_config_test):
+ utils_test_scenes(get_scenes_to_test(__name__), get_config_test, "indication")
diff --git a/tests/test_movements.py b/tests/test_movements.py
index 204534215..c1c6e148e 100644
--- a/tests/test_movements.py
+++ b/tests/test_movements.py
@@ -1,8 +1,10 @@
from manim import *
+from testing_utils import utils_test_scenes, get_scenes_to_test
-class HomotopyTest(Scene):
+
+class HomotopyTest(Scene):
def construct(self):
- def func(x,y,z,t):
+ def func(x, y, z, t):
norm = get_norm([x, y])
tau = interpolate(5, -5, t) + norm/FRAME_X_RADIUS
alpha = sigmoid(tau)
@@ -13,44 +15,43 @@ class HomotopyTest(Scene):
self.play(Homotopy(func, square))
-class PhaseFlowTest(Scene):
- def construct(self):
+class PhaseFlowTest(Scene):
+ def construct(self):
square = Square()
+
def func(t):
return t*0.5*UP
- self.play(PhaseFlow(func, square ))
+ self.play(PhaseFlow(func, square))
+
class MoveAlongPathTest(Scene):
- def construct(self):
+ def construct(self):
square = Square()
dot = Dot()
self.play(MoveAlongPath(dot, square))
-class RotateTest(Scene):
+
+class RotateTest(Scene):
def construct(self):
- square= Square()
+ square = Square()
self.play(Rotate(square, PI))
-class MoveToTest(Scene):
+
+class MoveToTest(Scene):
def construct(self):
square = Square()
self.play(square.move_to, np.array([1.0, 1.0, 0.0]))
-class ShiftTest(Scene):
+class ShiftTest(Scene):
def construct(self):
square = Square()
self.play(square.shift, UP)
-
-def test_scenes(get_config_test, Tester):
- CONFIG = get_config_test
- module_name = os.path.splitext(os.path.basename(__file__))[0].replace('test_', '')
- for _, scene_tested in inspect.getmembers(sys.modules[__name__], lambda m: inspect.isclass(m) and m.__module__ == __name__):
- Tester(scene_tested, CONFIG, module_name).test()
-
+def test_scenes(get_config_test):
+ utils_test_scenes(get_scenes_to_test(__name__), get_config_test, "movements")
diff --git a/tests/test_threed.py b/tests/test_threed.py
index 68c347cf7..ff75e983c 100644
--- a/tests/test_threed.py
+++ b/tests/test_threed.py
@@ -1,4 +1,5 @@
from manim import *
+from testing_utils import utils_test_scenes, get_scenes_to_test
class CubeTest(ThreeDScene):
@@ -30,9 +31,5 @@ class AmbientCameraMoveTest(ThreeDScene):
self.play(Animation(cube))
-def test_scenes(get_config_test, Tester):
- CONFIG = get_config_test
- module_name = os.path.splitext(os.path.basename(__file__))[
- 0].replace('test_', '')
- for _, scene_tested in inspect.getmembers(sys.modules[__name__], lambda m: inspect.isclass(m) and m.__module__ == __name__):
- Tester(scene_tested, CONFIG, module_name).test()
+def test_scenes(get_config_test):
+ utils_test_scenes(get_scenes_to_test(__name__), get_config_test, "threed")
diff --git a/tests/test_transform.py b/tests/test_transform.py
index f8b3b5148..9d4cebcaf 100644
--- a/tests/test_transform.py
+++ b/tests/test_transform.py
@@ -1,4 +1,5 @@
from manim import *
+from testing_utils import utils_test_scenes, get_scenes_to_test
class TransformTest(Scene):
@@ -130,9 +131,5 @@ class CyclicReplaceTest(Scene):
self.play(CyclicReplace(square, circle))
-def test_scenes(get_config_test, Tester):
- CONFIG = get_config_test
- module_name = os.path.splitext(os.path.basename(__file__))[
- 0].replace('test_', '')
- for _, scene_tested in inspect.getmembers(sys.modules[__name__], lambda m: inspect.isclass(m) and m.__module__ == __name__):
- Tester(scene_tested, CONFIG, module_name).test()
+def test_scenes(get_config_test):
+ utils_test_scenes(get_scenes_to_test(__name__), get_config_test, "transform")
\ No newline at end of file
diff --git a/tests/test_updaters.py b/tests/test_updaters.py
index 8f3895bff..ce6f4df65 100644
--- a/tests/test_updaters.py
+++ b/tests/test_updaters.py
@@ -1,4 +1,5 @@
from manim import *
+from testing_utils import utils_test_scenes, get_scenes_to_test
class UpdaterTest(Scene):
@@ -22,9 +23,5 @@ class ValueTrackerTest(Scene):
line_2.rotate(theta.get_value(), about_point=ORIGIN)
-def test_scenes(get_config_test, Tester):
- CONFIG = get_config_test
- module_name = os.path.splitext(os.path.basename(__file__))[
- 0].replace('test_', '')
- for _, scene_tested in inspect.getmembers(sys.modules[__name__], lambda m: inspect.isclass(m) and m.__module__ == __name__):
- Tester(scene_tested, CONFIG, module_name).test()
+def test_scenes(get_config_test):
+ utils_test_scenes(get_scenes_to_test(__name__), get_config_test, "updaters")
\ No newline at end of file
diff --git a/tests/test_writing.py b/tests/test_writing.py
index 9c13d9e9d..694f28245 100644
--- a/tests/test_writing.py
+++ b/tests/test_writing.py
@@ -1,4 +1,6 @@
from manim import *
+from testing_utils import utils_test_scenes, get_scenes_to_test
+
#NOTE : All of those tests use cached data (in /test_cache)
# Cache functionality is tested within test_CLI.
@@ -33,9 +35,6 @@ class TexMobjectTest(Scene):
self.play(Animation(t))
-def test_scenes(get_config_test, Tester):
- CONFIG = get_config_test
- module_name = os.path.splitext(os.path.basename(__file__))[
- 0].replace('test_', '')
- for name, scene_tested in inspect.getmembers(sys.modules[__name__], lambda m: inspect.isclass(m) and m.__module__ == __name__):
- Tester(scene_tested, CONFIG, module_name, caching_needed=True).test()
+
+def test_scenes(get_config_test):
+ utils_test_scenes(get_scenes_to_test(__name__), get_config_test, "writing", caching_needed=True)
diff --git a/tests/testing_utils.py b/tests/testing_utils.py
index d918110a1..6ae2a5b6b 100644
--- a/tests/testing_utils.py
+++ b/tests/testing_utils.py
@@ -1,13 +1,119 @@
import numpy as np
-import os
+import os
+import sys
import inspect
+import logging
+import pytest
-def set_test_scene(scene_object, CONFIG, module_name):
- """Function used to set up the test data for a new feature. Please Refer to the wiki."""
- scene = scene_object(**CONFIG)
+from manim import logger
+from manim import dirs
+from manim import config
+
+
+class SceneTester:
+ """Class used to test the animations.
+
+ Parameters
+ ----------
+ scene_object: : class:`Scene`
+ The scene to be tested
+ config_scene : :class:`Dict`
+ The configuration of the scene
+ module_tested: :class:`str`
+ The name of the module tested. i.e if we are testing functions of creation.py, the module will be "creation"
+
+ Attributes
+ -----------
+ path_tests_medias : : class:`str`
+ self-explanatory
+ path_tests_data : : class:`str`
+ self-explanatory
+ scene : :class:`Scene`
+ The scene tested
+ """
+
+ def __init__(self, scene_object, config_scene, module_tested, caching_needed=False):
+ # Disable the the logs, (--quiet is broken) TODO
+ logging.disable(logging.CRITICAL)
+ self.path_tests_media = os.path.join(
+ 'tests', 'tests_cache', module_tested)
+ self.path_tests_data = os.path.join(
+ 'tests', 'tests_data', module_tested)
+
+ tex_dir, text_dir = None, None
+ if caching_needed:
+ text_dir = os.path.join(
+ self.path_tests_media, scene_object.__name__, 'Text')
+ tex_dir = os.path.join(self.path_tests_media,
+ scene_object.__name__, 'Tex')
+ conf_dirs = {'media_dir': None,
+ 'video_dir': None,
+ 'tex_dir': tex_dir,
+ 'text_dir': text_dir,
+ }
+ # PROVISIONAL. To change when #98 is merged. TODO
+ config.initialize_directories(conf_dirs)
+ # By invoking this, the scene is rendered.
+ self.scene = scene_object(**config_scene)
+
+ def load_data(self):
+ """Load the np.array of the last frame of a pre-rendered scene. If not found, throw FileNotFoundError."""
+ with pytest.raises(FileNotFoundError) as e_info:
+ data_loaded = np.load(os.path.join(
+ self.path_tests_data, "{}.npy".format(str(self.scene))))
+ raise FileNotFoundError('test_data not found !')
+ assert (str(e_info.value) ==
+ 'test_data not found !'), f"{str(self.scene).replace('Test', '')} does not seem have a pre-rendered frame for testing, or it has not been found."
+ return data_loaded
+
+ def test(self):
+ # self.scene.get_frame() gets the last frame of the animation (under the form of an numpy array).
+ test_result = np.array_equal(self.scene.get_frame(), self.load_data())
+ assert(
+ test_result), f"The frames don't match. {str(self.scene).replace('Test', '')} has been modified. Please ignore if it was intended"
+
+
+def get_scenes_to_test(module_name):
+ """Get all Test classes of the module from which it is called."""
+ return inspect.getmembers(sys.modules[module_name], lambda m: inspect.isclass(m) and m.__module__ == module_name)
+
+
+def utils_test_scenes(scenes_to_test, CONFIG, module_name, caching_needed=False):
+ for _, scene_tested in scenes_to_test:
+ SceneTester(scene_tested, CONFIG, module_name,
+ caching_needed=caching_needed).test()
+
+
+def set_test_scene(scene_object, module_name):
+ """Function used to set up the test data for a new feature. Please refer to the wiki."""
+
+ CONFIG_TEST = {
+ 'camera_config': {
+ 'frame_rate': 15,
+ 'pixel_height': 480,
+ 'pixel_width': 854
+ },
+ 'end_at_animation_number': None,
+ 'file_writer_config': {
+ 'file_name': None,
+ 'input_file_path': 'test.py',
+ 'movie_file_extension': '.mp4',
+ 'png_mode': 'RGB',
+ 'save_as_gif': False,
+ 'save_last_frame': False,
+ 'save_pngs': False,
+ 'write_to_movie': False
+ },
+ 'leave_progress_bars': False,
+ 'skip_animations': True,
+ 'start_at_animation_number': None
+ }
+
+ scene = scene_object(**CONFIG_TEST)
data = scene.get_frame()
- path = os.path.join("manim", "tests","tests_data","{}".format(module_name))
+ path = os.path.join("manim", "tests", "tests_data",
+ "{}".format(module_name))
if not os.path.isdir(path):
os.makedirs(path)
np.save(os.path.join(path, str(scene)), data)
- logger.info('Saved in ' + path + '\n')
+ logger.info('Test data saved in ' + path + '\n')