From c4f7c3357abce142dc300f22ecb2dfdce59f115b Mon Sep 17 00:00:00 2001
From: Marko Petric <marko.petric@cern.ch>
Date: Mon, 17 Feb 2020 13:38:11 +0100
Subject: [PATCH] Add import test for dd4hep python modules

---
 DDTest/CMakeLists.txt        | 10 +++++--
 DDTest/python/test_import.py | 53 ++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 3 deletions(-)
 create mode 100644 DDTest/python/test_import.py

diff --git a/DDTest/CMakeLists.txt b/DDTest/CMakeLists.txt
index c96f873c8..cd6d85c4c 100644
--- a/DDTest/CMakeLists.txt
+++ b/DDTest/CMakeLists.txt
@@ -45,6 +45,10 @@ foreach(TEST_NAME
   set_tests_properties(t_${TEST_NAME} PROPERTIES FAIL_REGULAR_EXPRESSION "TEST_FAILED")
 endforeach()
 
+ADD_TEST( t_test_python_import "${CMAKE_INSTALL_PREFIX}/bin/run_test.sh"
+  pytest ${PROJECT_SOURCE_DIR}/DDTest/python/test_import.py)
+SET_TESTS_PROPERTIES( t_test_python_import PROPERTIES FAIL_REGULAR_EXPRESSION  "Exception;EXCEPTION;ERROR;Error" )
+
 if (DD4HEP_USE_GEANT4)
   foreach(TEST_NAME
       test_EventReaders
@@ -58,10 +62,10 @@ if (DD4HEP_USE_GEANT4)
     set_tests_properties(t_${TEST_NAME} PROPERTIES FAIL_REGULAR_EXPRESSION "TEST_FAILED")
   endforeach(TEST_NAME)
 
-  ADD_TEST( test_ddsim "${CMAKE_INSTALL_PREFIX}/bin/run_test.sh"
+  ADD_TEST( t_test_ddsim "${CMAKE_INSTALL_PREFIX}/bin/run_test.sh"
     ddsim --compactFile=${CMAKE_INSTALL_PREFIX}/DDDetectors/compact/SiD.xml --runType=batch -G -N=2 --outputFile=testSid.root
     --part.userParticleHandler=)
-  SET_TESTS_PROPERTIES( test_ddsim PROPERTIES FAIL_REGULAR_EXPRESSION  "Exception;EXCEPTION;ERROR;Error" )
+  SET_TESTS_PROPERTIES( t_test_ddsim PROPERTIES FAIL_REGULAR_EXPRESSION  "Exception;EXCEPTION;ERROR;Error" )
 
 endif()
-install(DIRECTORY include/DD4hep DESTINATION include)
\ No newline at end of file
+install(DIRECTORY include/DD4hep DESTINATION include)
diff --git a/DDTest/python/test_import.py b/DDTest/python/test_import.py
new file mode 100644
index 000000000..b0df29c91
--- /dev/null
+++ b/DDTest/python/test_import.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+"""
+Some imports to make sure that the DD4hep environment is complete
+"""
+from __future__ import absolute_import, unicode_literals, print_function
+import traceback
+import warnings
+import pytest
+
+parametrize = pytest.mark.parametrize
+
+moduleNames = [
+    'DDDigi',
+    'DDG4',
+    'dd4hep',
+    'DDRec',
+    ]
+
+# List here the modules that are allowed to Fail.
+# Ideally, this should always be empty...
+ALLOWED_TO_FAIL = []
+
+# List of modules that need graphic libraries.
+# When failing, these tests are just marked as skipped with a warning
+GRAPHIC_MODULES = []
+
+
+@parametrize('moduleName', moduleNames)
+def test_module(moduleName):
+  """ Try to import a module from DD4hep.
+
+      Modules that are in the ALLOWED_TO_FAIL list are shown as skipped and generate a warning
+
+      Modules that require graphic libraries (GRAPHIC_MODULES) are skipped on container
+  """
+
+  try:
+    __import__(moduleName)
+
+    # Test whether it is correctly imported from DD4hep
+
+  except ImportError as e:
+    msg = "could not import %s: %s" % (moduleName, repr(e))
+    print(traceback.print_exc())
+
+    if moduleName in ALLOWED_TO_FAIL:
+      warnings.warn(msg)
+      pytest.skip("WARN: " + msg)
+    elif moduleName in GRAPHIC_MODULES:
+      warnings.warn(msg + "(Possibly due to system graphic libraries not present)")
+      pytest.skip("WARN: " + msg + "(Possibly due to system graphic libraries not present)")
+    else:
+      pytest.fail("ERROR: " + msg)
-- 
GitLab