【CMake】创建一个简单的单元测试(ctest)
项目文件C++源码:
1 /** 2 * @file sum_integers.hpp 3 * @date 2022-08-05 21:03 4 */ 5 #ifndef SUM_INTEGERS_H 6 #define SUM_INTEGERS_H 7 8 #include <vector> 9 int sum_integers(const std::vector<int> integers); 10 11 #endif // !SUM_INTEGERS_H
1 /** 2 * @file sum_integers.cpp 3 * @date 2022-08-05 21:04 4 */ 5 #include "sum_integers.hpp" 6 #include <vector> 7 int sum_integers(const std::vector<int> integers) 8 { 9 auto sum = 0; 10 for (auto i : integers) 11 { 12 sum += i; 13 } 14 return sum; 15 }
1 /** 2 * @file main.cpp 3 * @date 2022-08-05 21:04 4 */ 5 #include "sum_integers.hpp" 6 #include <iostream> 7 #include <string> 8 #include <vector> 9 10 int main(int argc, char *argv[]) 11 { 12 std::vector<int> integers; 13 for (auto i = 1; i < argc; i++) 14 { 15 integers.push_back(std::stoi(argv[i])); 16 } 17 auto sum = sum_integers(integers); 18 std::cout << sum << std::endl; 19 }
CMakeLists.txt文件:
1 cmake_minimum_required(VERSION 3.5 FATAL_ERROR) 2 project(CmakeTest LANGUAGES CXX) 3 4 set(CMAKE_CXX_STANDARD 11) 5 set(CMAKE_CXX_EXTENSIONS OFF) 6 set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 8 find_package(PythonInterp REQUIRED) 9 find_package(Python COMPONENTS Interpreter Development REQUIRED) 10 find_program(BASH_EXECUTABLE NAMES bash REQUIRED) 11 12 message(STATUS "Python Include: ${Python_INCLUDE_DIRS}") 13 14 # 示例动态库 15 add_library(sum_integers sum_integers.cpp) 16 # m主程序 17 add_executable(${PROJECT_NAME} main.cpp) 18 target_link_libraries(${PROJECT_NAME} sum_integers) 19 # 测试动态库 20 add_executable(cpp_test test.cpp) 21 target_link_libraries(cpp_test sum_integers) 22 # 打开测试功能 23 enable_testing() 24 25 add_test( 26 NAME bash_test 27 COMMAND ${BASH_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test.sh $<TARGET_FILE:${PROJECT_NAME}> 28 ) 29 add_test( 30 NAME cpp_test 31 COMMAND $<TARGET_FILE:cpp_test> 32 ) 33 add_test( 34 NAME python_test_long 35 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test.py --executable $<TARGET_FILE:${PROJECT_NAME}> 36 ) 37 add_test( 38 NAME python_test_short 39 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test.py --short --executable $<TARGET_FILE:${PROJECT_NAME}> 40 )
cpp测试代码:
1 /** 2 * @file test.cpp 3 * @date 2022-08-05 21:06 4 */ 5 #include "sum_integers.hpp" 6 #include <vector> 7 int main() 8 { 9 auto integers = {1, 2, 3, 4, 5}; 10 if (sum_integers(integers) == 15) 11 { 12 return 0; 13 } 14 else 15 { 16 return 1; 17 } 18 }
bash测试代码:
1 #!/usr/bin/env bash 2 # test.sh 3 EXECUTABLE=$1 4 OUTPUT=$($EXECUTABLE 1 2 3 4) 5 if [ "$OUTPUT" = "10" ] 6 then 7 exit 0 8 else 9 exit 1 10 fi
python测试代码:
1 import subprocess 2 import argparse 3 # test script expects the executable as argument 4 parser = argparse.ArgumentParser() 5 parser.add_argument('--executable', 6 help='full path to executable') 7 parser.add_argument('--short', 8 default=False, 9 action='store_true', 10 help='run a shorter test') 11 args = parser.parse_args() 12 13 14 def execute_cpp_code(integers): 15 result = subprocess.check_output([args.executable] + integers) 16 return int(result) 17 18 19 if args.short: 20 # we collect [1, 2, ..., 100] as a list of strings 21 result = execute_cpp_code([str(i) for i in range(1, 101)]) 22 assert result == 5050, 'summing up to 100 failed' 23 else: 24 # we collect [1, 2, ..., 1000] as a list of strings 25 result = execute_cpp_code([str(i) for i in range(1, 1001)]) 26 assert result == 500500, 'summing up to 1000 failed'