1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Find CxxTest unit testing framework.
10 Find the CxxTest suite and declare a helper macro for creating unit
11 tests and integrating them with CTest. For more details on CxxTest
12 see https://cxxtest.com
18 CXXTEST_USE_PYTHON [deprecated since 1.3]
19 Only used in the case both Python & Perl
20 are detected on the system to control
21 which CxxTest code generator is used.
22 Valid only for CxxTest version 3.
28 NOTE: In older versions of this Find Module,
29 this variable controlled if the Python test
30 generator was used instead of the Perl one,
31 regardless of which scripting language the
38 CXXTEST_TESTGEN_ARGS (since CMake 2.8.3)
39 Specify a list of options to pass to the CxxTest code
40 generator. If not defined, --error-printer is
50 True if the CxxTest framework was found
52 Where to find the CxxTest include directory
53 CXXTEST_PERL_TESTGEN_EXECUTABLE
54 The perl-based test generator
55 CXXTEST_PYTHON_TESTGEN_EXECUTABLE
56 The python-based test generator
57 CXXTEST_TESTGEN_EXECUTABLE (since CMake 2.8.3)
58 The test generator that is actually used (chosen using user preferences
59 and interpreters found in the system)
60 CXXTEST_TESTGEN_INTERPRETER (since CMake 2.8.3)
61 The full path to the Perl or Python executable on the system, on
62 platforms where the script cannot be executed using its shebang line.
66 MACROS for optional use by CMake users:
70 CXXTEST_ADD_TEST(<test_name> <gen_source_file> <input_files_to_testgen...>)
71 Creates a CxxTest runner and adds it to the CTest testing suite
73 test_name The name of the test
74 gen_source_file The generated source filename to be
76 input_files_to_testgen The list of header files containing the
77 CxxTest::TestSuite's to be included in
93 include_directories(${CXXTEST_INCLUDE_DIR})
100 CXXTEST_ADD_TEST(unittest_foo foo_test.cc
101 ${CMAKE_CURRENT_SOURCE_DIR}/foo_test.h)
102 target_link_libraries(unittest_foo foo) # as needed
109 This will (if CxxTest is found):
110 1. Invoke the testgen executable to autogenerate foo_test.cc in the
111 binary tree from "foo_test.h" in the current source directory.
112 2. Create an executable and test called unittest_foo.
125 #include <cxxtest/TestSuite.h>
131 class MyTestSuite : public CxxTest::TestSuite
134 void testAddition( void )
136 TS_ASSERT( 1 + 1 > 1 );
137 TS_ASSERT_EQUALS( 1 + 1, 2 );
140 #]=======================================================================]
142 # Version 1.4 (11/18/10) (CMake 2.8.4)
143 # Issue 11384: Added support to the CXX_ADD_TEST macro so header
144 # files (containing the tests themselves) show up in
145 # Visual Studio and other IDEs.
147 # Version 1.3 (8/19/10) (CMake 2.8.3)
148 # Included patch by Simone Rossetto to check if either Python or Perl
149 # are present in the system. Whichever interpreter that is detected
150 # is now used to run the test generator program. If both interpreters
151 # are detected, the CXXTEST_USE_PYTHON variable is obeyed.
153 # Also added support for CXXTEST_TESTGEN_ARGS, for manually specifying
154 # options to the CxxTest code generator.
155 # Version 1.2 (3/2/08)
156 # Included patch from Tyler Roscoe to have the perl & python binaries
157 # detected based on CXXTEST_INCLUDE_DIR
158 # Version 1.1 (2/9/08)
159 # Clarified example to illustrate need to call target_link_libraries()
160 # Changed commands to lowercase
161 # Added licensing info
162 # Version 1.0 (1/8/08)
163 # Fixed CXXTEST_INCLUDE_DIRS so it will work properly
164 # Eliminated superfluous CXXTEST_FOUND assignment
165 # Cleaned up and added more documentation
167 #=============================================================
168 # CXXTEST_ADD_TEST (public macro)
169 #=============================================================
170 macro(CXXTEST_ADD_TEST _cxxtest_testname _cxxtest_outfname)
171 set(_cxxtest_real_outfname ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_outfname})
174 OUTPUT ${_cxxtest_real_outfname}
176 COMMAND ${CXXTEST_TESTGEN_INTERPRETER}
177 ${CXXTEST_TESTGEN_EXECUTABLE} ${CXXTEST_TESTGEN_ARGS} -o ${_cxxtest_real_outfname} ${ARGN}
180 set_source_files_properties(${_cxxtest_real_outfname} PROPERTIES GENERATED true)
181 add_executable(${_cxxtest_testname} ${_cxxtest_real_outfname} ${ARGN})
183 if(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
184 add_test(${_cxxtest_testname} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${_cxxtest_testname})
185 elseif(EXECUTABLE_OUTPUT_PATH)
186 add_test(${_cxxtest_testname} ${EXECUTABLE_OUTPUT_PATH}/${_cxxtest_testname})
188 add_test(${_cxxtest_testname} ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_testname})
193 #=============================================================
195 #=============================================================
196 if(NOT DEFINED CXXTEST_TESTGEN_ARGS)
197 set(CXXTEST_TESTGEN_ARGS --error-printer)
200 find_package(Python QUIET)
201 find_package(Perl QUIET)
203 find_path(CXXTEST_INCLUDE_DIR cxxtest/TestSuite.h)
204 find_program(CXXTEST_PYTHON_TESTGEN_EXECUTABLE
205 NAMES cxxtestgen cxxtestgen.py
206 PATHS ${CXXTEST_INCLUDE_DIR})
207 find_program(CXXTEST_PERL_TESTGEN_EXECUTABLE cxxtestgen.pl
208 PATHS ${CXXTEST_INCLUDE_DIR})
210 if(PYTHON_FOUND OR PERL_FOUND)
211 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
213 if(PYTHON_FOUND AND (CXXTEST_USE_PYTHON OR NOT PERL_FOUND OR NOT DEFINED CXXTEST_USE_PYTHON))
214 set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PYTHON_TESTGEN_EXECUTABLE})
215 execute_process(COMMAND ${CXXTEST_PYTHON_TESTGEN_EXECUTABLE} --version
216 OUTPUT_VARIABLE _CXXTEST_OUT ERROR_VARIABLE _CXXTEST_OUT RESULT_VARIABLE _CXXTEST_RESULT)
217 if(_CXXTEST_RESULT EQUAL 0)
218 set(CXXTEST_TESTGEN_INTERPRETER "")
220 set(CXXTEST_TESTGEN_INTERPRETER ${Python_EXECUTABLE})
222 FIND_PACKAGE_HANDLE_STANDARD_ARGS(CxxTest DEFAULT_MSG
223 CXXTEST_INCLUDE_DIR CXXTEST_PYTHON_TESTGEN_EXECUTABLE)
226 set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PERL_TESTGEN_EXECUTABLE})
227 set(CXXTEST_TESTGEN_INTERPRETER ${PERL_EXECUTABLE})
228 FIND_PACKAGE_HANDLE_STANDARD_ARGS(CxxTest DEFAULT_MSG
229 CXXTEST_INCLUDE_DIR CXXTEST_PERL_TESTGEN_EXECUTABLE)
233 set(CXXTEST_INCLUDE_DIRS ${CXXTEST_INCLUDE_DIR})
238 set(CXXTEST_FOUND false)
239 if(NOT CxxTest_FIND_QUIETLY)
240 if(CxxTest_FIND_REQUIRED)
241 message(FATAL_ERROR "Neither Python nor Perl found, cannot use CxxTest, aborting!")
243 message(STATUS "Neither Python nor Perl found, CxxTest will not be used.")