Skip to content

Commit 607f926

Browse files
Add two testing features (#424)
* Add the loading of the test file as a test in itself. Currently when loading unittest (for python) into gtest (c++), a failure while loading the test in python does not generates a test failure which is problematic as load time error will be unnoticed. The introduced changes add the loading as an individual test. * Transform error as a warning as we now have a better way to makes a test fail. * Remove BOM from an utf8 file (makes tests fails otherwise) * Add method import_sofa_python_scene(path_to_scene : str) in the python package Sofa The introduced method loads dynamically python module containing a scene and returns it. This allows to do: ```python scene = import_sofa_python_scene("myscene.py") root = scene.createScene(Sofa.Core.Node("root")) ... root.addObject(....) ``` * Update bindings/Sofa/package/__init__.py Co-authored-by: Hugo <hugo.talbot@sofa-framework.org> * Example of use of Sofa.import_python_scene --------- Co-authored-by: Hugo <hugo.talbot@sofa-framework.org>
1 parent 95b7a14 commit 607f926

5 files changed

Lines changed: 31 additions & 2 deletions

File tree

Testing/src/SofaPython3Testing/PythonTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void PythonTest::run( const PythonTestData& data )
119119
py::list testSuiteList = py::cast<py::list>(testSuite);
120120
if(!testSuiteList.size())
121121
{
122-
msg_error() << "There doesn't seem to be any test in " << filename;
122+
msg_warning() << "There doesn't seem to be any test in " << filename;
123123
return ;
124124
}
125125

Testing/src/SofaPython3Testing/PythonTestExtractor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ std::vector<PythonTestData> PythonTestExtractor::extract () const
119119
} catch(const std::exception& e) {
120120
msg_error("PythonTestExtractor") << "File skipped: " << (test.path+"/"+test.filename) << msgendl
121121
<< e.what();
122+
list.emplace_back(PythonTestData(filepath(test.path,test.filename), "loading_", {}));
122123
}
123124
}
124125

bindings/Sofa/package/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,15 @@ def doReInit(self):
384384
SofaPrefabF.__dict__["__original__"] = f
385385
return SofaPrefabF
386386

387+
def import_sofa_python_scene(path_to_scene : str):
388+
"""Return a python module containing a SOFA scene"""
389+
spec_from_location = importlib.util.spec_from_file_location("sofa.scene", path_to_scene)
390+
module_name = importlib.util.module_from_spec(spec_from_location)
391+
sys.modules["module.name"] = module_name
392+
spec_from_location.loader.exec_module(module_name)
393+
394+
if not hasattr(foo, "createScene"):
395+
raise Exception("Unable to find 'createScene' in module "+path_to_scene)
396+
397+
return foo
398+

bindings/Sofa/tests/Core/Mass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# coding: utf8
1+
# coding: utf8
22

33
import unittest
44
import Sofa

examples/import_python_scene.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Sofa
2+
3+
4+
def createScene(root):
5+
"""Demonstrates the use of Sofa.import_sofa_python_scene"""
6+
7+
# loads the python modules "liver.py"
8+
liver = Sofa.import_sofa_python_scene("liver.py")
9+
10+
# Creates the scene and attach it to A
11+
a = root.addChild("A")
12+
liver.createScene(a)
13+
14+
# Feel free to add any other objects to the scene (eg: controllers manipulating something in the liver)
15+
...
16+

0 commit comments

Comments
 (0)