Skip to content

Commit e344e1b

Browse files
committed
CmakeList add option UTONLY, option BUILD_TEST could be UNIT/PERF
1 parent cf949a8 commit e344e1b

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ SET(CMAKECONFIG_INSTALL_DIR "${LIB_INSTALL_DIR}/cmake/${PROJECT_NAME}")
1515

1616
find_package(Lua REQUIRED)
1717

18+
# BUILD_TEST could be: True/False constant like ON/OFF, or UNIT/PERF
1819
option(BUILD_TEST "Build test." OFF)
1920
option(NEED_VOLATILE "Support calling members by volatile objects." OFF)
2021

@@ -32,8 +33,12 @@ target_link_libraries(${PROJECT_NAME} INTERFACE ${LUA_LIBRARIES})
3233

3334
if (BUILD_TEST)
3435
enable_testing()
35-
add_subdirectory(test/unit_test)
36-
add_subdirectory(test/perf_test)
36+
if (${BUILD_TEST} OR (${BUILD_TEST} STREQUAL "UNIT"))
37+
add_subdirectory(test/unit_test)
38+
endif()
39+
if (${BUILD_TEST} OR (${BUILD_TEST} STREQUAL "PERF"))
40+
add_subdirectory(test/perf_test)
41+
endif()
3742
endif()
3843

3944
configure_file("${PROJECT_NAME}Config.cmake.in" "${PROJECT_NAME}Config.cmake"

run_test.sh

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,24 @@
1515

1616
cd $(dirname "$0") || exit
1717

18+
TEST=ON
19+
MYOSTREAM=OFF
20+
SEPARATE=OFF
21+
NEED_VOLATILE=OFF
22+
UTONLY=""
23+
1824
RUN_UNIT_TEST=0
1925
RUN_PERFORMANCE_TEST=0
20-
MYOSTREAM=OFF
2126
REBUILD=0
2227
CLEAR=0
23-
SEPARATE=OFF
24-
NEED_VOLATILE=OFF
28+
2529
while [ "$#" -gt 0 ]; do
2630
case $1 in
31+
-t)
32+
TEST=$2
33+
shift
34+
shift
35+
;;
2736
-ru)
2837
RUN_UNIT_TEST=1
2938
shift
@@ -57,8 +66,14 @@ while [ "$#" -gt 0 ]; do
5766
NEED_VOLATILE=ON
5867
shift
5968
;;
69+
-utonly)
70+
UTONLY=$2
71+
shift
72+
shift
73+
;;
6074
-h)
61-
echo "run_test.sh [-o] [-r|-ru|-rp] [--rebuild] [--clear] [--separate] [--volatile]"
75+
echo "run_test.sh [-t UNIT/PERF] [-o] [-r|-ru|-rp] [--rebuild] [--clear]"\
76+
"[--separate] [--volatile] [-utonly <file>]"
6277
exit 0
6378
;;
6479
*)
@@ -74,16 +89,21 @@ if [ ${REBUILD} -eq 1 ]; then
7489
fi
7590
mkdir -p build
7691
cd build
77-
cmake .. -DBUILD_TEST=TRUE \
92+
cmake .. -DBUILD_TEST=${TEST} \
7893
-DENABLE_MYOSTREAM_WATCH=${MYOSTREAM} \
7994
-DUNIT_TEST_SEPARATE=${SEPARATE} \
80-
-DNEED_VOLATILE=${NEED_VOLATILE}
95+
-DNEED_VOLATILE=${NEED_VOLATILE} \
96+
-DUTONLY=${UTONLY}
8197
make
8298

8399
if [ $? -eq 0 ]; then
84100
ctest --output-on-failure
85101
if [ ${RUN_UNIT_TEST} -eq 1 ]; then
86-
./test/unit_test/unit_test
102+
if [ -n "${UTONLY}" ]; then
103+
./test/unit_test/${UTONLY}
104+
else
105+
./test/unit_test/unit_test
106+
fi
87107
fi
88108
if [ ${RUN_PERFORMANCE_TEST} -eq 1 ]; then
89109
./test/perf_test/perf_test

test/unit_test/CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ find_package(GTest REQUIRED)
1212

1313
option(ENABLE_MYOSTREAM_WATCH "Use lib myostream to print variables to console." OFF)
1414
option(UNIT_TEST_SEPARATE "Whether build unit tests separately." OFF)
15+
option(UTONLY "Only build one unit test file." "")
1516

1617
if (ENABLE_MYOSTREAM_WATCH)
1718
find_package(MyOStream REQUIRED)
@@ -25,7 +26,7 @@ function(build_target TARGET)
2526
${LUA_INCLUDE_DIR}
2627
${MyOStream_INCLUDE_DIR})
2728
target_link_libraries(${TARGET} PUBLIC ${GTEST_LIBRARIES} ${LUA_LIBRARIES})
28-
add_test(NAME ${TARGET} COMMAND ${TARGET})
29+
# add_test(NAME ${TARGET} COMMAND ${TARGET})
2930
include(GoogleTest)
3031
gtest_discover_tests(${TARGET})
3132
endfunction()
@@ -34,8 +35,10 @@ file(GLOB SOURCE_FILES "*.cpp")
3435

3536
if (UNIT_TEST_SEPARATE)
3637
foreach(FILE ${SOURCE_FILES})
37-
get_filename_component(NAME ${FILE} NAME_WE)
38-
SET(TARGET ${NAME})
38+
get_filename_component(TARGET ${FILE} NAME_WE)
39+
if (NOT ${UTONLY} STREQUAL "" AND NOT ${TARGET} STREQUAL ${UTONLY})
40+
continue()
41+
endif()
3942
add_executable(${TARGET} ${FILE} "main.cpp")
4043
build_target(${TARGET})
4144
endforeach()

0 commit comments

Comments
 (0)