Dedicated main for tests but with tests also written in production code #791
-
Hello, I'm struggling for more than a week now to implement this. I read a lot of documentation no how to achieve this functionality but I wasn't able to do so, and I'm not even sure anymore if this is possible. I already have read a lot of references, but failed to understand correctly if that was my case or not. Or even if I was able to implement the proposed solutions correctly: Dealing with shared libraries With that being said, here's my slim down source tree:
The project structure is based on this cmake_template. What I have now working:
What isn't working:
To achieve this here's my snippets from the files:
add_subdirectory(src)
include(CTest)
add_subdirectory(test)
# Build a library and a main binary that links to the library
add_library(cloysterhpc_object OBJECT ${SOURCE_FILES})
add_library(cloysterhpc_static STATIC $<TARGET_OBJECTS:cloysterhpc_object>)
add_executable(main main.cpp)
target_link_libraries(
main
PRIVATE
cloysterhpc_static
cloysterhpc::cloysterhpc_options
cloysterhpc::cloysterhpc_warnings)
target_link_system_libraries(
main
PRIVATE
...
doctest::doctest)
target_link_libraries(
cloysterhpc_object
PRIVATE
cloysterhpc::cloysterhpc_options
cloysterhpc::cloysterhpc_warnings)
target_link_system_libraries(
cloysterhpc_object
PRIVATE
...
doctest::doctest)
target_include_directories(cloysterhpc_object PRIVATE "${CMAKE_BINARY_DIR}/configured_files/include")
target_include_directories(cloysterhpc_object ${WARNING_GUARD} PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)
target_include_directories(main PRIVATE "${CMAKE_BINARY_DIR}/configured_files/include")
target_include_directories(main ${WARNING_GUARD} PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>)
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>
// This is all that is needed to compile a test-runner executable.
file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
message(STATUS "Test files found are: ${TEST_SOURCES}")
add_executable(${PROJECT_NAME} ${TEST_SOURCES})
target_link_libraries(
${PROJECT_NAME}
PRIVATE
cloysterhpc_static
cloysterhpc::cloysterhpc_warnings
cloysterhpc::cloysterhpc_options)
target_link_system_libraries(
${PROJECT_NAME}
PRIVATE
...
doctest::doctest)
target_include_directories(${PROJECT_NAME} ${WARNING_GUARD} PRIVATE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/../include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/../include>)
doctest_discover_tests(${PROJECT_NAME}) If I write any test in any For the tests on
#define DOCTEST_CONFIG_IMPLEMENT
#include <doctest/doctest.h>
TEST_SUITE("Load files")
{
TEST_CASE("Get information")
{
inifile ini;
std::filesystem::path path;
path = std::filesystem::current_path() / "sample/inifile.ini";
ini.loadFile(path);
std::string clusterName = ini.getValue("information", "cluster_name");
CHECK(clusterName == "cloyster");
}
} My goal was to be able to have all the tests, those written on production code and on separate unit files compiled to the same separate binary. I didn't touched my main function, because I've used I've added only snippets of the code I thought was relevant, but at this stage I'm not even sure what is relevant or not. The full code can be found on my repo if anyone if willing to look at it: https://github.com/viniciusferrao/cloysterhpc Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I was able to solve the issue recompiling every object two times. Yes I know that will increase the compilation time but it's a price that I'm willing to pay. With that I just added the following to my
So if Doing that I was able to achieve what I want, that was still keep the tests on the source files while maintaining a totally separate |
Beta Was this translation helpful? Give feedback.
I was able to solve the issue recompiling every object two times. Yes I know that will increase the compilation time but it's a price that I'm willing to pay.
With that I just added the following to my
.cpp
files:So if
BUILD_TESTING
is enabled during compile time it will not enableDOCTEST_CONFIG_DISABLE
.Doing that I was able to achieve what I want, that was still keep the tests on the source files while maintaining a totally separate
main.cpp
file and support for additional separate test files.