1. Installation test
CMake can also specify installation rules and add tests. These two functions can be executed by using make install and make test after generating makefile. In GNU Makefile, you may need to write install and test pseudo targets and corresponding rules for this, but in CMake, such work also needs to simply call a few commands.
1.1 custom installation rules
First, first math/CMakeLists.txt Add the following two lines to the file: # Specify the installation path of the MathFunctions library install (TARGETS MathFunctions DESTINATION bin) install (FILES MathFunctions.h DESTINATION include)
Indicates the installation path of the MathFunctions library. After that, also modify the CMakeLists file in the root directory and add the following lines at the end:
# Specify the installation path install (TARGETS Demo DESTINATION bin) install (FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include)
Through the above customization, the generated Demo file and MathFunctions library libmathfunctions O files will be copied to / usr/local/bin, and MathFunctions H and generated config H file will be copied to / usr/local/include. We can verify (incidentally, / usr/local / here is the root directory of the default installation. You can specify which root directory these files should be copied to by modifying the value of CMAKE_INSTALL_PREFIX variable):
[root@hackett demo5]# make install Consolidate compiler generated dependencies of target MathFunctions [ 50%] Built target MathFunctions Consolidate compiler generated dependencies of target demo [100%] Built target demo Install the project... -- Install configuration: "" -- Installing: /usr/local/bin/demo -- Installing: /usr/local/include/config.h -- Installing: /usr/local/bin/libMathFunctions.a -- Installing: /usr/local/include/myMath.h [root@hackett demo5]# ls /usr/local/bin/ demo libMathFunctions.a [root@iZwz97bu0gr8vx0j8l6kkzZ demo5]# ls /usr/local/include/ config.h myMath.h
1.2 project addition test
Adding tests is also simple. CMake provides a testing tool called CTest. All we have to do is to call a series of add_ in the CMakeLists file of the project root directory. Test command.
CMakeLists.txt cmake_minimum_required(VERSION 3.10) # set the project name project(demo5) # Add a configuration header file to handle CMake's setting of the source code configure_file ( "${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/config.h" ) # Do you want to use your own MathFunctions library option (USE_MYMATH "Use provided math implementation" ON) # Add MathFunctions library if (USE_MYMATH) include_directories ("${PROJECT_SOURCE_DIR}/math") add_subdirectory (math) set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions) endif (USE_MYMATH) aux_source_directory(. DIR_SRCS) # Specify build target add_executable(demo ${DIR_SRCS}) target_link_libraries(demo ${EXTRA_LIBS}) # Specify the installation path install (TARGETS demo DESTINATION bin) install (FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include) enable_testing() # Test whether the program runs successfully add_test (test_run demo 3 2) add_test (test_35_2 demo 35 2) set_tests_properties (test_35_2 PROPERTIES PASS_REGULAR_EXPRESSION "37") add_test (test_5_2 demo 5 2) set_tests_properties (test_5_2 PROPERTIES PASS_REGULAR_EXPRESSION "7") add_test (test_2_3 demo 2 3) set_tests_properties (test_2_3 PROPERTIES PASS_REGULAR_EXPRESSION "5")
The above code contains four tests. First test_run is used to test whether the program runs successfully and returns a value of 0. The remaining three tests are used to test whether 35 + 2, 5 + 2 and 2 + 3 can get the correct results. Where PASS_REGULAR_EXPRESSION is used to test whether the output contains the following string.
Test results: [root@hackett demo5]# make Consolidate compiler generated dependencies of target MathFunctions [ 50%] Built target MathFunctions Consolidate compiler generated dependencies of target demo [ 75%] Building CXX object CMakeFiles/demo.dir/main.cpp.o [100%] Linking CXX executable demo [100%] Built target demo [root@hackett demo5]# make test Running tests... Test project /root/workspace/cmake/demo5 Start 1: test_run 1/4 Test #1: test_run ......................... Passed 0.00 sec Start 2: test_35_2 2/4 Test #2: test_35_2 ........................ Passed 0.00 sec Start 3: test_5_2 3/4 Test #3: test_5_2 ......................... Passed 0.00 sec Start 4: test_2_3 4/4 Test #4: test_2_3 ......................... Passed 0.00 sec 100% tests passed, 0 tests failed out of 4 Total Test time (real) = 0.01 sec
If you want to test more input data, writing test cases one by one like the above is too cumbersome. This can be achieved by writing macros:
# Define a macro to simplify testing macro (do_test arg1 arg2 result) add_test (test_${arg1}_${arg2} demo ${arg1} ${arg2}) set_tests_properties (test_${arg1}_${arg2} PROPERTIES PASS_REGULAR_EXPRESSION ${result}) endmacro (do_test) # Use this macro for a series of data tests do_test (35 2 "37") do_test (5 52 "7") do_test (2 3 "5")
For more detailed usage of CTest, you can refer to the documentation of CTest through man 1 ctest.