Salome HOME
Rework building procedure and testing environment
[modules/shaper.git] / CMakeCommon / UnitTest.cmake
1 # Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 #
21 # SHAPER unit test system
22 # How to use:
23 # INCLUDE(UnitTest)
24 # ...
25 # ADD_UNIT_TESTS(TestUndoRedo.py
26 #                TestResultBody.py)
27 # or
28 # SET(PROJECT_UNIT_TESTS
29 #     TestUndoRedo.py
30 #     TestWhatever.py
31 #    )
32 # ADD_UNIT_TESTS(${PROJECT_UNIT_TESTS})
33 #
34 # Where "TestUndoRedo.py" and "TestResultBody.py" are names
35 # of python test scripts in the "./Test" directory.
36 # This macro will add "Subproject_" as prefix and
37 # remove ".py" for the test name automaticaly.
38 #
39 # Start building of the "RUN_TESTS" project in the Visual studio
40 # or run "make test" on linux stations to start the testing procedure.
41 #
42
43 ENABLE_TESTING()
44
45 function(GENERATE_TESTS PATH testdir TESTS)
46   SALOME_GENERATE_TESTS_ENVIRONMENT(tests_env)
47
48   foreach(eachFileName ${ARGN})
49     # Strip the ".py" suffix
50     GET_FILENAME_COMPONENT(aTestName ${eachFileName} NAME_WE)
51
52     # Add "SubprojectName_" prefix
53     GET_FILENAME_COMPONENT(aSubprojectName ${CMAKE_CURRENT_SOURCE_DIR} NAME)
54     SET(aTestName "${aSubprojectName}_${aTestName}")
55
56     # Full path to the python test file beeing executed
57     SET(aTestFileName "${testdir}/${eachFileName}")
58     IF(EXISTS ${aTestFileName})
59       ADD_TEST(NAME ${aTestName}
60                COMMAND ${PYTHON_EXECUTABLE} ${aTestFileName})
61       SET_TESTS_PROPERTIES(${aTestName} PROPERTIES
62                ENVIRONMENT "${tests_env};SHAPER_UNIT_TEST_IN_PROGRESS=1"
63                LABELS "${aSubprojectName}")
64       # Debug output...
65       #MESSAGE(STATUS "Test added: ${aTestName} file: ${aTestFileName}")
66     ELSE(EXISTS ${aTestFileName})
67       MESSAGE(WARNING "Can not find the test file: ${aTestFileName}")
68     ENDIF(EXISTS ${aTestFileName})
69   endforeach(eachFileName ${ARGN})
70 endfunction(GENERATE_TESTS)
71
72 function(ADD_UNIT_TESTS)
73   GENERATE_TESTS(PATH "${CMAKE_CURRENT_SOURCE_DIR}/Test" TESTS ${ARGN})
74 endfunction(ADD_UNIT_TESTS)
75
76 function(ADD_UNIT_TESTS_API)
77   GENERATE_TESTS(PATH "${CMAKE_CURRENT_SOURCE_DIR}" TESTS ${ARGN})
78 endfunction(ADD_UNIT_TESTS_API)
79
80
81 function(ADD_RESTRICTED_TESTS)
82   SET(RESTRICTED_ROOT_DIR $ENV{RESTRICTED_ROOT_DIR} CACHE PATH "Path to the restricted repository")
83
84   if (EXISTS ${RESTRICTED_ROOT_DIR})
85     GENERATE_TESTS(PATH "${RESTRICTED_ROOT_DIR}/SHAPER/bugs" TESTS ${ARGN})
86   else()
87     message(WARNING "RESTRICTED_ROOT_DIR is not specified to run restricted tests")
88   endif()
89 endfunction(ADD_RESTRICTED_TESTS)