Unit test framework (#10)

* add first Unit Test
with gtest and cmake dependencies

* optionally build tests or not
added some realistic tests

* added ci compatible run configuration

* added test dependencies -lpthread -lm to work in debian correctly

* added some docs
and option infos
This commit is contained in:
Lukas-Heiligenbrunner 2020-05-07 19:58:16 +02:00 committed by GitHub
parent 335bcf1259
commit 463853dbd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 4 deletions

View File

@ -11,7 +11,11 @@ build:
- cd build
- make
- make package
- make test
- make build-xml
artifacts:
paths:
- "build/bin/*"
- "build/packages/*"
- "build/packages/*"
reports:
junit: report.xml

View File

@ -18,6 +18,7 @@ SET(LIB_METHOD STATIC) #SHARED / STATIC
option(BUILD_DOC "Build documentation" OFF) # additional dependency for Doxygen
option(PACKAGING "Allow Packaging to <exe>, <deb> or <rpm>" ON) # additional dependencies for RPMbuild,dpkg or NSIS
option(TESTS "Build Tests" ON) # additional dependencies for GTEST - to build tests
set(WinBuild false)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
@ -151,12 +152,11 @@ add_library(logger ${LIB_METHOD}
SET(SOURCE
src/main.cpp
src/IPRefresher.cpp
src/Config.cpp
src/IpHelper.cpp)
add_executable(iprefresher ${SOURCE})
add_executable(iprefresher src/main.cpp ${SOURCE})
# LINK generated LIBS #
target_link_libraries(iprefresher api logger ${CURL_LIBRARIES} ${LIBCONFIG++_LIBRARIES})
@ -293,4 +293,43 @@ if (BUILD_DOC)
message(STATUS "Doxygen need to be installed to generate the doxygen documentation")
endif (DOXYGEN_FOUND)
message("")
endif (BUILD_DOC)
endif (BUILD_DOC)
# Test Cases
if (TESTS)
include(GoogleTest)
mark_as_advanced(
BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS
gmock_build_tests gtest_build_samples gtest_build_tests
gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols
)
enable_testing()
macro(package_add_test TESTNAME)
# create an exectuable in which the tests will be stored
add_executable(${TESTNAME} ${ARGN})
# link the Google test infrastructure, mocking library, and a default main fuction to
target_link_libraries(${TESTNAME} gtest gtest_main -lpthread -lm api logger ${CURL_LIBRARIES} ${LIBCONFIG++_LIBRARIES})
# see https://cmake.org/cmake/help/v3.10/module/GoogleTest.html for more options to pass to it
gtest_discover_tests(${TESTNAME}
WORKING_DIRECTORY ${PROJECT_DIR}
EXTRA_ARGS --gtest_output=xml:report.xml -VV
PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_DIR}"
)
set_target_properties(${TESTNAME} PROPERTIES FOLDER tests)
endmacro()
package_add_test(test1 tests/UnitTest.cpp ${SOURCE})
add_custom_target(build-test
"${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target test
DEPENDS ${PROJECT_NAME}
COMMENT "Packing ${PROJECT_NAME}")
add_custom_target(build-xml
"bin/test1" --gtest_output="xml:report.xml"
DEPENDS ${PROJECT_NAME}
COMMENT "Packing ${PROJECT_NAME}")
ENDIF ()

30
tests/UnitTest.cpp Normal file
View File

@ -0,0 +1,30 @@
//
// Created by lukas on 06.05.20.
//
#include <FileLogger.h>
#include <api/IPAPI.h>
#include <climits>
#include "gtest/gtest.h"
/**
* Test if default ip is 0.0.0.0 when last ip file doesn't exist.
*/
TEST(ReadIp, testzeroIpIfNotExists) {
FileLogger logger;
std::string oldip = logger.readip();
ASSERT_EQ(oldip, "0.0.0.0");
}
/**
* Test if default ip is 0.0.0.0 when last ip file doesn't exist.
*/
TEST(IPAPI, testIpAPIcheckIPSyntax) {
IPAPI ipapi;
std::string ip = ipapi.getGlobalIp();
if (ip.find('.') == ULONG_MAX) {
// error when ip doesn't contain a .
ASSERT_TRUE(false);
} else {
ASSERT_TRUE(true);
}
}