...
Code Block |
---|
language | cpp |
---|
linenumbers | true |
---|
|
add_library(src operations.cpp operations.h)
add_executable(
src_main main.cpp operations.cpp
)
target_sources(src
PRIVATE
main.cpp
PUBLIC
main.h
)
target_include_directories(src
PUBLIC
.
)
|
Last but not least, the ./tst/CMakeLists.txt file which is a bit more elaborate and needs some explanation:
Code Block |
---|
language | cpp |
---|
linenumbers | true |
---|
|
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/4ec4cd23f486bf70efcc5d2caa40f24368f752e3.zip
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_executable(
teststest_operations
run_tests.cpp
test_operations.cpp
)
target_link_libraries(teststest_operations
PRIVATE
src
gtest_main
)
# Auto-discovery of the tests
include(GoogleTest)
gtest_discover_tests(teststest_operations
PROPERTIES
LABELS "unit"
DISCOVERY_TIMEOUT
240) |
...
Code Block |
---|
language | bash |
---|
linenumbers | true |
---|
|
../TDD-cpp/Calculator/ $: mkdir -p ./built/
../TDD-cpp/Calculator/ $: cmake -S . -B ./built/
../TDD-cpp/Calculator/ $: cmake --build ./built/ |
Then both executables, source and tests, the test executable can be found in the built directory under the appropriate directories - src and tstbuild directory, respectively built/tst/ named test_operations, as specified in CMakeLists.txt, and run:
Code Block |
---|
language | bash |
---|
linenumbers | true |
---|
|
../TDD-cpp/Calculator/ $: ./built/src/src_main
../TDD-cpp/Calculator/ $:
../TDD-cpp/Calculator/ $: ./built/tst/test_operations
[==========] Running 3 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 3 tests from DivideOperation
[ RUN ] DivideOperation.PositiveInput
[ OK ] DivideOperation.PositiveInput (0 ms)
[ RUN ] DivideOperation.NegitiveInput
[ OK ] DivideOperation.NegitiveInput (0 ms)
[ RUN ] DivideOperation.ZerioInput
[ OK ] DivideOperation.ZerioInput (0 ms)
[----------] 3 tests from DivideOperation (0 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 1 test suite ran. (0 ms total)
[ PASSED ] 3 tests. |