Cmake-4
Step4:安裝和測試
1)對于MathFunctions我們想安裝庫和頭文件,但是對于應用程序我們安裝可執行程序和配置頭文件。
MathFunctions/CMakeLists.txt的末尾增加如下:
install(TARGETS MathFunctions DESTINATION lib) install(FILES MathFunctions.h DESTINATION include)在上一級的CMakeList中增加:
install(TARGETS Tutorial DESTINATION bin) install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"DESTINATION include)生成的 Demo 文件和 MathFunctions 函數庫 libMathFunctions.o 文件將會被復制到 /usr/local/bin 中,而 MathFunctions.h 和生成的 config.h 文件則會被復制到 /usr/local/include 中
2)測試
enable_testing()# does the application run add_test(NAME Runs COMMAND Tutorial 25)# does the usage message work? add_test(NAME Usage COMMAND Tutorial) set_tests_properties(UsagePROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number")# define a function to simplify adding tests function(do_test target arg result)add_test(NAME Comp${arg} COMMAND ${target} ${arg})set_tests_properties(Comp${arg}PROPERTIES PASS_REGULAR_EXPRESSION ${result}) endfunction(do_test)# do a bunch of result based tests do_test(Tutorial 4 "4 is 2") do_test(Tutorial 9 "9 is 3") do_test(Tutorial 5 "5 is 2.236") do_test(Tutorial 7 "7 is 2.645") do_test(Tutorial 25 "25 is 5") do_test(Tutorial -25 "-25 is [-nan|nan|0]") do_test(Tutorial 0.0001 "0.0001 is 0.01")以下內容引用自:
原文鏈接:https://blog.csdn.net/qq_38450982/article/details/89156116
add_test():
運行的項目中添加測試。
add_test(<name> <command> [<arg>...])
?添加一個名為<name>的測試。測試名稱不包含空格、引號或CMake語法中特殊的其他字符。
<command>指定測試命令行。如果<command>指定了一個可執行目標(由add_executable()創建),它將被在構建時創建的可執行文件的位置自動替換。
<arg>為傳遞給<command>的參數。
set_tests_properties()對測試屬性進行設置,函數原型如下:
set_tests_properties(test1 [test2...] PROPERTIES prop1 value1 prop2 value2)
PROPERTIES 包括有WILL_FAIL、PASS_REGULAR_EXPRESSION、FAIL_REGULAR_EXPRESSION。代碼中用到的是PASS_REGULAR_EXPRESSION,如果設置了這個屬性,測試輸出將根據指定的正則表達式進行檢查,并且至少有一個正則表達式必須匹配,否則測試失敗。如我們以第一個測試為例:
add_test (TutorialComp25 Tutorial 25)
set_tests_properties (TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")
添加一個名為TutorialComp25的測試,調用可執行文件Tutorial,?Tutorial的功能是計算平方根,并且傳給它的參數為25。然后設置測試TutorialComp25的屬性,將輸出根據指定的正則表達式即“25 is 5”進行匹配,如果匹配成功,則測試成功,否則則失敗。
?
總結