cmake例子:安装google-test
2020-05-07 本文已影响0人
gb_QA_log
其他工具见:
clang-format
cppcheck
clang-analyzer
单元测试工具:
# @file: top level cmakelist
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (google_test_example)
# Add an library for the example classes
add_library(example_google_test
Reverse.cpp
Palindrome.cpp
)
#############################################
# Unit tests
add_subdirectory(3rd_party/google-test)
# enable CTest testing
enable_testing()
# Add a testing executable
add_executable(unit_tests unit_tests.cpp)
target_link_libraries(unit_tests
example_google_test
GTest::GTest
GTest::Main
)
add_test(test_all unit_tests)
# @file: unit_tests.cpp
#include <string>
#include "Reverse.h"
#include "Palindrome.h"
#include <gtest/gtest.h>
class ReverseTests : public ::testing::Test
{
};
TEST_F(ReverseTests, simple )
{
std::string toRev = "Hello";
Reverse rev;
std::string res = rev.reverse(toRev);
EXPECT_EQ(res, "olleH" );
}
TEST_F(ReverseTests, empty )
{
std::string toRev;
Reverse rev;
std::string res = rev.reverse(toRev);
EXPECT_EQ(res, "" );
}
TEST_F(ReverseTests, is_palindrome )
{
std::string pal = "mom";
Palindrome pally;
EXPECT_TRUE(pally.isPalindrome(pal));
}
# @file: 3rd_party/google-test/CMakeLists.txt.in
# download a 3-parth lib, then aotu-install it
include(ExternalProject)
ExternalProject_Add(googletest
URL https://github.com/google/googletest/archive/bfc0ffc8a698072c794ae7299db9cb6866f4c0bc.tar.gz_
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
@file: 3rd_party/google-test/CMakeLists.txt
# Download and unpack googletest at configure time
# See: http://crascit.com/2015/07/25/cmake-gtest/
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
# Call CMake to download and Google Test自动选择生成器
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
# Build the downloaded google test
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Prevent installation of GTest with your project
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
set(INSTALL_GMOCK OFF CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build)
# Add aliases for GTest and GMock libraries
if(NOT TARGET GTest::GTest)
add_library(GTest::GTest ALIAS gtest)
add_library(GTest::Main ALIAS gtest_main)
endif()
if(NOT TARGET GTest::GMock)
add_library(GMock::GMock ALIAS gmock)
add_library(GMock::Main ALIAS gmock_main)
endif()