From 463853dbd70029d873d35bc1cec8f0feecdfcbf2 Mon Sep 17 00:00:00 2001 From: Lukas-Heiligenbrunner <30468956+Lukas-Heiligenbrunner@users.noreply.github.com> Date: Thu, 7 May 2020 19:58:16 +0200 Subject: [PATCH] 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 --- .gitlab-ci.yml | 6 +++++- CMakeLists.txt | 45 ++++++++++++++++++++++++++++++++++++++++++--- tests/UnitTest.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 tests/UnitTest.cpp diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1c9025a..7d40b82 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -11,7 +11,11 @@ build: - cd build - make - make package + - make test + - make build-xml artifacts: paths: - "build/bin/*" - - "build/packages/*" \ No newline at end of file + - "build/packages/*" + reports: + junit: report.xml \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 72e16de..ed406c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 , or " 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) \ No newline at end of file +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 () \ No newline at end of file diff --git a/tests/UnitTest.cpp b/tests/UnitTest.cpp new file mode 100644 index 0000000..e1d5bcc --- /dev/null +++ b/tests/UnitTest.cpp @@ -0,0 +1,30 @@ +// +// Created by lukas on 06.05.20. +// + +#include +#include +#include +#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); + } +} \ No newline at end of file