1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

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.
This commit is contained in:
karliss 2018-04-15 14:02:31 +03:00 committed by ArseniyShestakov
parent 9da3f48274
commit ad2c429d8f
5 changed files with 38 additions and 23 deletions

View File

@ -302,6 +302,7 @@ if(ENABLE_LAUNCHER)
add_subdirectory(launcher) add_subdirectory(launcher)
endif() endif()
if(ENABLE_TEST) if(ENABLE_TEST)
enable_testing()
add_subdirectory(test) add_subdirectory(test)
endif() endif()

View File

@ -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) set(googleTest_Dir ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
if(EXISTS ${googleTest_Dir}) 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) 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}) 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 "") vcmi_set_output_dir(vcmitest "")
set_target_properties(vcmitest PROPERTIES ${PCH_PROPERTIES}) set_target_properties(vcmitest PROPERTIES ${PCH_PROPERTIES})
cotire(vcmitest) cotire(vcmitest)
# Files to copy to the build directory file (GLOB_RECURSE testdata "testdata/*.*")
set(vcmitest_FILES foreach(resource ${testdata})
testdata/TerrainViewTest.h3m get_filename_component(filename ${resource} NAME)
testdata/terrainViewMappings.json get_filename_component(dir ${resource} DIRECTORY)
testdata/ObjectPropertyTest/header.json get_filename_component(dirname ${dir} NAME)
testdata/ObjectPropertyTest/objects.json set (output "")
testdata/ObjectPropertyTest/surface_terrain.json while(NOT ${dirname} STREQUAL testdata)
testdata/ObjectPropertyTest/underground_terrain.json get_filename_component(path_component ${dir} NAME)
) set (output "${path_component}/${output}")
get_filename_component(dir ${dir} DIRECTORY)
foreach(file ${vcmitest_FILES}) get_filename_component(dirname ${dir} NAME)
add_custom_command(TARGET vcmitest POST_BUILD endwhile()
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/${file}" ${CMAKE_CURRENT_BINARY_DIR} set(output "${CMAKE_BINARY_DIR}/bin/test/testdata/${output}/${filename}")
) configure_file(${resource} ${output} COPYONLY)
endforeach() endforeach()

View File

@ -21,7 +21,7 @@
#include "../lib/filesystem/CFilesystemLoader.h" #include "../lib/filesystem/CFilesystemLoader.h"
#include "../lib/filesystem/AdapterLoaders.h" #include "../lib/filesystem/AdapterLoaders.h"
CVcmiTestConfig::CVcmiTestConfig() void CVcmiTestConfig::SetUp()
{ {
console = new CConsoleHandler(); console = new CConsoleHandler();
preinitDLL(console, true); preinitDLL(console, true);
@ -39,7 +39,7 @@ CVcmiTestConfig::CVcmiTestConfig()
} }
} }
CVcmiTestConfig::~CVcmiTestConfig() void CVcmiTestConfig::TearDown()
{ {
std::cout << "Ending global test tear-down." << std::endl; std::cout << "Ending global test tear-down." << std::endl;
} }

View File

@ -9,10 +9,12 @@
*/ */
#pragma once #pragma once
#include "StdInc.h"
/// Global setup/tear down class for unit tests. /// Global setup/tear down class for unit tests.
class CVcmiTestConfig class CVcmiTestConfig : public ::testing::Environment
{ {
public: public:
CVcmiTestConfig(); virtual void SetUp() override;
~CVcmiTestConfig(); virtual void TearDown() override;
}; };

View File

@ -15,6 +15,6 @@
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleTest(&argc, argv);
CVcmiTestConfig test; ::testing::AddGlobalTestEnvironment(new CVcmiTestConfig());
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
} }