Added GTest (googletest) Example (#84)

* Added GTest (googletest) Example

The examples tests the Fibonacci library similar to the doctest example.

* Update examples/gtest/CMakeLists.txt

Co-Authored-By: Lars Melchior <TheLartians@users.noreply.github.com>
This commit is contained in:
Paul T
2019-10-09 08:50:27 -04:00
committed by Lars Melchior
parent 8625173d8f
commit 5191b4c703
2 changed files with 58 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(CPMExampleGtest)
# ---- Options ----
option(ENABLE_TEST_COVERAGE "Enable test coverage" OFF)
# ---- Dependencies ----
include(../../cmake/CPM.cmake)
CPMAddPackage(
NAME fibonacci
GIT_REPOSITORY https://github.com/TheLartians/Fibonacci.git
VERSION 1.0
)
CPMAddPackage(
NAME googletest
GITHUB_REPOSITORY google/googletest
GIT_TAG release-1.8.1
VERSION 1.8.1
OPTIONS
"INSTALL_GTEST OFF"
"gtest_force_shared_crt ON"
)
# ---- Create binary ----
add_executable(CPMExampleGtest main.cpp)
target_link_libraries(CPMExampleGtest fibonacci gtest gtest_main gmock)
set_target_properties(CPMExampleGtest PROPERTIES CXX_STANDARD 17)
# ---- Enable testing ----
enable_testing()
add_test(CPMExampleGtest CPMExampleGtest)
# ---- Add code coverage ----
if (${ENABLE_TEST_COVERAGE})
set_target_properties(CPMExampleGtest PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-O0 -g -fprofile-arcs -ftest-coverage --coverage")
target_link_options(CPMExampleGtest PUBLIC "--coverage")
endif()

13
examples/gtest/main.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include <gtest/gtest.h>
#include <fibonacci.h>
TEST(FibonacciTests, BasicChecks)
{
ASSERT_TRUE(fibonnacci(0) == 0);
ASSERT_TRUE(fibonnacci(1) == 1);
ASSERT_TRUE(fibonnacci(2) == 1);
ASSERT_TRUE(fibonnacci(3) == 2);
ASSERT_TRUE(fibonnacci(4) == 3);
ASSERT_TRUE(fibonnacci(5) == 5);
ASSERT_TRUE(fibonnacci(13) == 233);
}