From ad2c429d8fe1ad3dcfdd863be8471deb0e0f0ff4 Mon Sep 17 00:00:00 2001 From: karliss Date: Sun, 15 Apr 2018 14:02:31 +0300 Subject: [PATCH] CMake: unittest improvements (#447) * Convert TestConfig to proper test environment. * Copy test data to build directory. * Get test recognized by ctest, but disable gtest discovery. Allowing ctest to run each test individually isn't currently practical because it is 50 times slower. --- CMakeLists.txt | 1 + test/CMakeLists.txt | 46 +++++++++++++++++++++++++--------------- test/CVcmiTestConfig.cpp | 4 ++-- test/CVcmiTestConfig.h | 8 ++++--- test/main.cpp | 2 +- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 069627ee8..26460b25d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -302,6 +302,7 @@ if(ENABLE_LAUNCHER) add_subdirectory(launcher) endif() if(ENABLE_TEST) + enable_testing() add_subdirectory(test) endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fda271eb4..13b889704 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,4 +1,6 @@ -enable_testing() +if(NOT ${CMAKE_VERSION} VERSION_LESS "3.10.0") + include(GoogleTest) +endif() set(googleTest_Dir ${CMAKE_CURRENT_SOURCE_DIR}/googletest) if(EXISTS ${googleTest_Dir}) @@ -98,26 +100,36 @@ add_subdirectory_with_folder("3rdparty" googletest EXCLUDE_FROM_ALL) add_executable(vcmitest ${test_SRCS} ${test_HEADERS} ${mock_HEADERS} ${GTestSrc}/src/gtest-all.cc ${GMockSrc}/src/gmock-all.cc) target_link_libraries(vcmitest vcmi ${RT_LIB} ${DL_LIB}) -add_test(vcmitest vcmitest) + +if(FALSE AND NOT ${CMAKE_VERSION} VERSION_LESS "3.10.0") + # Running tests one by one using ctest not recommended due to vcmi having + # slow global initialization. + gtest_discover_tests(vcmitest + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/bin/") +else() + add_test(NAME tests + COMMAND vcmitest + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/bin/") +endif() + vcmi_set_output_dir(vcmitest "") set_target_properties(vcmitest PROPERTIES ${PCH_PROPERTIES}) cotire(vcmitest) -# Files to copy to the build directory -set(vcmitest_FILES - testdata/TerrainViewTest.h3m - testdata/terrainViewMappings.json - testdata/ObjectPropertyTest/header.json - testdata/ObjectPropertyTest/objects.json - testdata/ObjectPropertyTest/surface_terrain.json - testdata/ObjectPropertyTest/underground_terrain.json -) - -foreach(file ${vcmitest_FILES}) - add_custom_command(TARGET vcmitest POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/${file}" ${CMAKE_CURRENT_BINARY_DIR} - ) +file (GLOB_RECURSE testdata "testdata/*.*") +foreach(resource ${testdata}) + get_filename_component(filename ${resource} NAME) + get_filename_component(dir ${resource} DIRECTORY) + get_filename_component(dirname ${dir} NAME) + set (output "") + while(NOT ${dirname} STREQUAL testdata) + get_filename_component(path_component ${dir} NAME) + set (output "${path_component}/${output}") + get_filename_component(dir ${dir} DIRECTORY) + get_filename_component(dirname ${dir} NAME) + endwhile() + set(output "${CMAKE_BINARY_DIR}/bin/test/testdata/${output}/${filename}") + configure_file(${resource} ${output} COPYONLY) endforeach() - diff --git a/test/CVcmiTestConfig.cpp b/test/CVcmiTestConfig.cpp index 0292cf0c9..d6fa1dafb 100644 --- a/test/CVcmiTestConfig.cpp +++ b/test/CVcmiTestConfig.cpp @@ -21,7 +21,7 @@ #include "../lib/filesystem/CFilesystemLoader.h" #include "../lib/filesystem/AdapterLoaders.h" -CVcmiTestConfig::CVcmiTestConfig() +void CVcmiTestConfig::SetUp() { console = new CConsoleHandler(); preinitDLL(console, true); @@ -39,7 +39,7 @@ CVcmiTestConfig::CVcmiTestConfig() } } -CVcmiTestConfig::~CVcmiTestConfig() +void CVcmiTestConfig::TearDown() { std::cout << "Ending global test tear-down." << std::endl; } diff --git a/test/CVcmiTestConfig.h b/test/CVcmiTestConfig.h index 666eb6203..5a1fb007d 100644 --- a/test/CVcmiTestConfig.h +++ b/test/CVcmiTestConfig.h @@ -9,10 +9,12 @@ */ #pragma once +#include "StdInc.h" + /// Global setup/tear down class for unit tests. -class CVcmiTestConfig +class CVcmiTestConfig : public ::testing::Environment { public: - CVcmiTestConfig(); - ~CVcmiTestConfig(); + virtual void SetUp() override; + virtual void TearDown() override; }; diff --git a/test/main.cpp b/test/main.cpp index 08da1f468..cc790d7fd 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -15,6 +15,6 @@ int main(int argc, char * argv[]) { ::testing::InitGoogleTest(&argc, argv); - CVcmiTestConfig test; + ::testing::AddGlobalTestEnvironment(new CVcmiTestConfig()); return RUN_ALL_TESTS(); }