Skip to content

Commit 3876050

Browse files
committed
test: setup ctest from root
1 parent 26bce38 commit 3876050

File tree

7 files changed

+149
-112
lines changed

7 files changed

+149
-112
lines changed

.github/workflows/build-macos.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ jobs:
9696
with:
9797
configurePreset: ${{ matrix.buildtype }}
9898
buildPreset: ${{ matrix.buildtype }}
99-
configurePresetAdditionalArgs: "['-DBUILD_TESTS=ON']"
10099

101100
- name: Create and Upload Artifact
102101
uses: actions/upload-artifact@main
@@ -107,10 +106,8 @@ jobs:
107106
108107
- name: Run Unit Tests
109108
run: |
110-
cd ${{ github.workspace }}/build/${{ matrix.buildtype }}/tests/unit
111-
ctest --verbose
109+
ctest -V -R unit
112110
113111
# - name: Run Integration Tests
114112
# run: |
115-
# cd ${{ github.workspace }}/build/${{ matrix.buildtype }}/tests/integration
116-
# ctest --verbose
113+
# ctest -V -R integration

.github/workflows/build-ubuntu.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ jobs:
101101
with:
102102
configurePreset: ${{ matrix.buildtype }}
103103
buildPreset: ${{ matrix.buildtype }}
104-
configurePresetAdditionalArgs: "['-DBUILD_TESTS=ON']"
105104

106105
- name: Create and Upload Artifact
107106
uses: actions/upload-artifact@main
@@ -112,10 +111,8 @@ jobs:
112111
113112
- name: Run Unit Tests
114113
run: |
115-
cd ${{ github.workspace }}/build/${{ matrix.buildtype }}/tests/unit
116-
ctest --verbose
114+
ctest -V -R unit
117115
118116
# - name: Run Integration Tests
119117
# run: |
120-
# cd ${{ github.workspace }}/build/${{ matrix.buildtype }}/tests/integration
121-
# ctest --verbose
118+
# ctest -V -R integration

CMakeLists.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,16 @@ if(OPTIONS_ENABLE_SCCACHE)
102102
endif()
103103
endif()
104104

105-
option(BUILD_TESTS "Build tests" OFF) # By default, tests will not be built
106-
option(RUN_TESTS_AFTER_BUILD "Run tests when building" OFF) # By default, tests will only run if requested
107-
108105
# *****************************************************************************
109106
# Add project
110107
# *****************************************************************************
111108
add_subdirectory(src)
112109

113-
if(BUILD_TESTS OR PACKAGE_TESTS)
110+
include(CTest)
111+
if(BUILD_TESTING)
114112
log_option_enabled("tests")
115113
add_subdirectory(tests)
116-
add_compile_definitions(BUILD_TESTS)
114+
enable_testing()
117115
else()
118116
log_option_disabled("tests")
119117
endif()

CMakePresets.json

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"BUILD_STATIC_LIBRARY": "ON",
1818
"SPEED_UP_BUILD_UNITY": "ON",
1919
"OPTIONS_ENABLE_SCCACHE": "ON",
20-
"CMAKE_BUILD_TYPE": "RelWithDebInfo"
20+
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
21+
"BUILD_TESTING": "OFF"
2122
}
2223
},
2324
{
@@ -26,7 +27,8 @@
2627
"displayName": "Windows - Release",
2728
"description": "Windows Release Build",
2829
"cacheVariables": {
29-
"VCPKG_TARGET_TRIPLET": "x64-windows-static"
30+
"VCPKG_TARGET_TRIPLET": "x64-windows-static",
31+
"BUILD_TESTING": "OFF"
3032
},
3133
"architecture": {
3234
"value": "x64",
@@ -42,7 +44,8 @@
4244
"ASAN_ENABLED": "ON",
4345
"DEBUG_LOG": "ON",
4446
"BUILD_STATIC_LIBRARY": "OFF",
45-
"VCPKG_TARGET_TRIPLET": "x64-windows"
47+
"VCPKG_TARGET_TRIPLET": "x64-windows",
48+
"BUILD_TESTING": "ON"
4649
}
4750
},
4851
{
@@ -56,7 +59,8 @@
5659
"ASAN_ENABLED": "OFF",
5760
"BUILD_STATIC_LIBRARY": "OFF",
5861
"SPEED_UP_BUILD_UNITY": "OFF",
59-
"VCPKG_TARGET_TRIPLET": "x64-windows"
62+
"VCPKG_TARGET_TRIPLET": "x64-windows",
63+
"BUILD_TESTING": "ON"
6064
}
6165
},
6266
{
@@ -69,7 +73,7 @@
6973
"value": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
7074
"type": "FILEPATH"
7175
},
72-
"RUN_TESTS_AFTER_BUILD": "OFF"
76+
"BUILD_TESTING": "OFF"
7377
},
7478
"condition": {
7579
"type": "equals",
@@ -80,34 +84,27 @@
8084
{
8185
"name": "linux-debug",
8286
"inherits": "linux-release",
83-
"displayName": "Linux - Debug Build",
87+
"displayName": "Linux - Debug",
8488
"description": "Build Debug Mode",
8589
"cacheVariables": {
8690
"CMAKE_BUILD_TYPE": "Debug",
8791
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
8892
"DEBUG_LOG": "ON",
89-
"SPEED_UP_BUILD_UNITY": "OFF"
93+
"SPEED_UP_BUILD_UNITY": "OFF",
94+
"BUILD_TESTING": "ON"
9095
}
9196
},
9297
{
9398
"name": "linux-debug-asan",
9499
"inherits": "linux-release",
95-
"displayName": "Linux - Debug Build",
100+
"displayName": "Linux - Debug w/ Address Sanitizer",
96101
"description": "Build Debug Mode With ASAN Enable",
97102
"cacheVariables": {
98103
"CMAKE_BUILD_TYPE": "Debug",
99104
"DEBUG_LOG": "ON",
100105
"ASAN_ENABLED": "ON",
101-
"SPEED_UP_BUILD_UNITY": "OFF"
102-
}
103-
},
104-
{
105-
"name": "linux-test",
106-
"inherits": "linux-release",
107-
"displayName": "Linux - Test Build",
108-
"description": "Build Tests",
109-
"cacheVariables": {
110-
"BUILD_TESTS": "ON"
106+
"SPEED_UP_BUILD_UNITY": "OFF",
107+
"BUILD_TESTING": "ON"
111108
}
112109
},
113110
{
@@ -120,7 +117,7 @@
120117
"CMAKE_C_COMPILER": "/usr/bin/clang",
121118
"DEBUG_LOG": "ON",
122119
"VCPKG_TARGET_TRIPLET": "arm64-osx",
123-
"RUN_TESTS_AFTER_BUILD": "OFF"
120+
"BUILD_TESTING": "OFF"
124121
},
125122
"condition": {
126123
"type": "equals",
@@ -131,62 +128,60 @@
131128
{
132129
"name": "macos-debug",
133130
"inherits": "macos-release",
134-
"displayName": "macOS - Debug Build",
131+
"displayName": "macOS - Debug",
135132
"description": "macOS Debug Build with Apple Clang",
136133
"cacheVariables": {
137134
"CMAKE_BUILD_TYPE": "Debug",
138135
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
139136
"DEBUG_LOG": "ON",
140137
"BUILD_STATIC_LIBRARY": "OFF",
141-
"SPEED_UP_BUILD_UNITY": "OFF"
142-
}
143-
},
144-
{
145-
"name": "macos-test",
146-
"inherits": "macos-release",
147-
"displayName": "macOS - Test Build",
148-
"description": "macOS Build with Tests",
149-
"cacheVariables": {
150-
"BUILD_TESTS": "ON"
138+
"SPEED_UP_BUILD_UNITY": "OFF",
139+
"BUILD_TESTING": "ON"
151140
}
152141
}
153142
],
154143
"buildPresets": [
144+
{ "name": "linux-release", "configurePreset": "linux-release" },
145+
{ "name": "linux-debug", "configurePreset": "linux-debug" },
146+
{ "name": "windows-release", "configurePreset": "windows-release" },
155147
{
156-
"name": "linux-release",
157-
"configurePreset": "linux-release"
158-
},
159-
{
160-
"name": "linux-debug",
161-
"configurePreset": "linux-debug"
162-
},
163-
{
164-
"name": "linux-test",
165-
"configurePreset": "linux-test"
166-
},
167-
{
168-
"name": "windows-release",
169-
"configurePreset": "windows-release"
148+
"name": "windows-release-asan",
149+
"configurePreset": "windows-release-asan"
170150
},
151+
{ "name": "windows-debug", "configurePreset": "windows-debug" },
152+
{ "name": "macos-release", "configurePreset": "macos-release" },
153+
{ "name": "macos-debug", "configurePreset": "macos-debug" }
154+
],
155+
"testPresets": [
171156
{
172157
"name": "windows-release-asan",
173-
"configurePreset": "windows-release-asan"
158+
"configurePreset": "windows-release-asan",
159+
"output": { "outputOnFailure": true },
160+
"execution": { "noTestsAction": "error" }
174161
},
175162
{
176-
"name": "windows-Xdebug",
177-
"configurePreset": "windows-debug"
163+
"name": "windows-debug",
164+
"configurePreset": "windows-debug",
165+
"output": { "outputOnFailure": true },
166+
"execution": { "noTestsAction": "error" }
178167
},
179168
{
180-
"name": "macos-release",
181-
"configurePreset": "macos-release"
169+
"name": "linux-debug",
170+
"configurePreset": "linux-debug",
171+
"output": { "outputOnFailure": true },
172+
"execution": { "noTestsAction": "error" }
182173
},
183174
{
184-
"name": "macos-debug",
185-
"configurePreset": "macos-debug"
175+
"name": "linux-debug-asan",
176+
"configurePreset": "linux-debug-asan",
177+
"output": { "outputOnFailure": true },
178+
"execution": { "noTestsAction": "error" }
186179
},
187180
{
188-
"name": "macos-test",
189-
"configurePreset": "macos-test"
181+
"name": "macos-debug",
182+
"configurePreset": "macos-debug",
183+
"output": { "outputOnFailure": true },
184+
"execution": { "noTestsAction": "error" }
190185
}
191186
]
192187
}

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,26 @@ our [customized tools](https://docs.opentibiabr.com/opentibiabr/downloads/tools)
1515

1616
## Getting Started
1717

18-
* [Gitbook](https://docs.opentibiabr.com/opentibiabr/projects/canary).
19-
* [Wiki](https://github.com/opentibiabr/canary/wiki).
18+
- [Gitbook](https://docs.opentibiabr.com/opentibiabr/projects/canary).
19+
- [Wiki](https://github.com/opentibiabr/canary/wiki).
20+
21+
## Running Tests
22+
23+
Tests can be run directly from the repository root using CMake test presets:
24+
25+
```bash
26+
# Configure and build tests for your platform
27+
cmake --preset linux-debug && cmake --build --preset linux-debug
28+
29+
# Run all tests
30+
ctest --preset linux-debug
31+
32+
# For other platforms use:
33+
# ctest --preset macos-debug
34+
# ctest --preset windows-debug
35+
```
36+
37+
For detailed testing information including adding tests and framework usage, see [tests/README.md](tests/README.md).
2038

2139
## Support
2240

@@ -26,8 +44,8 @@ If you need help, please visit our [discord](https://discord.gg/gvTj5sh9Mp). Our
2644

2745
Here are some ways you can contribute:
2846

29-
* [Issue Tracker](https://github.com/opentibiabr/canary/issues/new/choose).
30-
* [Pull Request](https://github.com/opentibiabr/canary/pulls).
47+
- [Issue Tracker](https://github.com/opentibiabr/canary/issues/new/choose).
48+
- [Pull Request](https://github.com/opentibiabr/canary/pulls).
3149

3250
You are subject to our code of conduct, read at [this link](https://github.com/opentibiabr/canary/blob/main/CODE_OF_CONDUCT.md).
3351

tests/CMakeLists.txt

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,67 @@
1-
find_package(ut CONFIG REQUIRED)
1+
if(NOT BUILD_TESTING)
2+
return()
3+
endif()
4+
5+
# --- boost::ut (handle common package/target name variants) ---
6+
# Try the canonical package name first (as used by the project),
7+
# then fall back to popular alt names in package managers.
8+
set(_ut_targets)
9+
find_package(Boost.UT CONFIG QUIET)
10+
if(Boost.UT_FOUND)
11+
list(APPEND _ut_targets Boost::ut)
12+
else()
13+
find_package(boost_ut CONFIG QUIET) # some distros
14+
if(boost_ut_FOUND)
15+
list(APPEND _ut_targets boost::ut)
16+
else()
17+
find_package(ut CONFIG QUIET) # your original
18+
if(ut_FOUND)
19+
# guessing an imported target name; adjust if your toolchain differs
20+
list(APPEND _ut_targets Boost::ut)
21+
endif()
22+
endif()
23+
endif()
24+
25+
if(NOT _ut_targets)
26+
message(
27+
FATAL_ERROR "Could not find boost::ut. Try installing via vcpkg (boost-ut) or add FetchContent in the root."
28+
)
29+
endif()
30+
31+
# Utility to pick the first found target (Boost::ut OR boost::ut)
32+
list(GET _ut_targets 0 UT_IMPORTED_TARGET)
233

334
enable_testing()
435

36+
# ---------------------------
37+
# Helper: define one test exe
38+
# ---------------------------
539
function(setup_test TARGET_NAME DIR)
6-
add_executable(${TARGET_NAME} main.cpp)
40+
add_executable(${TARGET_NAME} ${CMAKE_CURRENT_LIST_DIR}/main.cpp)
741

42+
# Your project-specific knobs
843
target_compile_definitions(${TARGET_NAME} PUBLIC -DDEBUG_LOG -DBUILD_TESTS)
9-
target_link_libraries(${TARGET_NAME} PRIVATE Boost::ut ${PROJECT_NAME}_lib)
44+
target_link_libraries(${TARGET_NAME} PRIVATE ${UT_IMPORTED_TARGET} ${PROJECT_NAME}_lib)
1045
target_include_directories(
11-
${TARGET_NAME}
12-
PRIVATE ${CMAKE_SOURCE_DIR}/tests/fixture
13-
PRIVATE ${CMAKE_SOURCE_DIR}/tests/${DIR}
46+
${TARGET_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/tests/fixture ${CMAKE_SOURCE_DIR}/tests/${DIR}
1447
)
48+
target_compile_features(${TARGET_NAME} PRIVATE cxx_std_20)
1549

50+
# Keep your hooks
1651
setup_target(${TARGET_NAME})
52+
configure_linking(${TARGET_NAME})
1753

54+
# Turn OFF unity/IPO for tests (tests benefit from fast rebuilds & debugging)
1855
set_target_properties(${TARGET_NAME} PROPERTIES UNITY_BUILD OFF INTERPROCEDURAL_OPTIMIZATION OFF)
1956
log_option_disabled("Build unity")
2057

21-
configure_linking(${TARGET_NAME})
22-
23-
add_test(NAME ${DIR} COMMAND ${TARGET_NAME} --reporter console --success)
24-
set_tests_properties(
25-
${DIR} PROPERTIES WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/tests/${DIR} ENVIRONMENT
26-
"CATCH_CONFIG_CONSOLE_WIDTH=160" TIMEOUT 120
27-
)
58+
# Register with ctest. boost::ut doesn’t use Catch2-style CLI; just run the binary.
59+
add_test(NAME ${DIR} COMMAND ${TARGET_NAME})
2860

29-
if(RUN_TESTS_AFTER_BUILD)
30-
add_custom_command(
31-
TARGET ${TARGET_NAME}
32-
POST_BUILD
33-
COMMAND ${CMAKE_CTEST_COMMAND} -C $<CONFIG> --output-on-failure --tests-regex "^${DIR}$"
34-
COMMENT "Running ctest ${DIR} after building ${TARGET_NAME}"
35-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
36-
)
37-
endif(RUN_TESTS_AFTER_BUILD)
61+
# Per-test runtime context
62+
set_tests_properties(${DIR} PROPERTIES WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/tests/${DIR} TIMEOUT 120)
3863
endfunction()
3964

65+
# Suites (each subdir calls setup_test(...) for its cases)
4066
add_subdirectory(unit)
4167
add_subdirectory(integration)

0 commit comments

Comments
 (0)