diff --git a/aidge_export_cpp/unit_tests/test_export.py b/aidge_export_cpp/unit_tests/test_export.py index 0a34153cebfa405389dd7ccff20a5d359d7967a4..c32158ad746de905209d645249b9213674a07867 100644 --- a/aidge_export_cpp/unit_tests/test_export.py +++ b/aidge_export_cpp/unit_tests/test_export.py @@ -12,6 +12,9 @@ import shutil from aidge_core.utils import run_command from aidge_export_cpp import cpp_fuse_to_metaops, set_nodes_names +import pytest +from _pytest.unittest import TestCaseFunction + def initFiller(model): # Initialize parameters (weights and biases) @@ -64,15 +67,40 @@ def _np_init_ones(shape, default_value=0.01, dtype=np.float32): return data.reshape(shape).astype(dtype) +# Global dictionary to store test reports +test_reports = {} + +@pytest.hookimpl(hookwrapper=True) +def pytest_runtest_makereport(item, call): + # Execute all other hooks to obtain the report object + outcome = yield + rep = outcome.get_result() + # Store the report in the global dictionary + test_reports[item.nodeid] = rep + class test_operator_export(unittest.TestCase): def setUp(self): - # TODO change seed at each test ? RNG_SEED = 1234 np.random.seed(RNG_SEED) aidge_core.random.Generator.set_seed(RNG_SEED) + def tearDown(self): - pass + result = self._outcome.result + test_succeeded = True + if isinstance(result, TestCaseFunction): + print("Test ran with pytest cannot retrieve if the test was a success.") + else: + test_succeeded = not any(test is self for test, _ in result.failures + result.errors) + if test_succeeded: + shutil.rmtree(self.export_folder) + + # rep = test_reports.get(self.id()) + # if rep is not None and rep.passed: + # # Perform teardown actions only if the test passed + # print(g"Removing {self.export_folder}") + # shutil.rmtree(self.export_folder, ignore_errors=True) + def unit_test_export(self, graph_view, op_name, in_dims, random_inputs=True, random_weights=True, default_value=0.01): """ @@ -117,7 +145,7 @@ class test_operator_export(unittest.TestCase): # Note the convention ``<op_name>_test`` is useful for gitignore to avoid pushing generated export by accident. export_folder = op_name + "_test" - + self.export_folder = export_folder shutil.rmtree(export_folder, ignore_errors=True) # Export the model in C++ standalone @@ -624,10 +652,6 @@ class test_operator_export(unittest.TestCase): self.unit_test_export(model, "BatchNorm2DDenser", [[1, 3, 5, 7]], False, False) - - def test_cpp(self): - print("Export test to do") - def test_Conv(self): model = aidge_core.sequential([ aidge_core.Conv2D(1, 1, [3, 3], name="InputNode")