How to build c++ project with make when tests fail

Viewed 707

I'm using cmake for build configuration and gtests as testing framework. Currently make fails if tests fail and running ctest or make test after successful build says "No tests were found!!!". I would like to be able to build project even if some tests are failing and to run tests with ctest/make test command

My CMakeLists.txt (tests added at the end of file):

    cmake_minimum_required(VERSION 3.10.0)
    project(ExampleProject VERSION 0.1.0)
    add_subdirectory("${PROJECT_SOURCE_DIR}/src/models")
    add_subdirectory("${PROJECT_SOURCE_DIR}/src/logic")
    add_subdirectory("${PROJECT_SOURCE_DIR}/src/executable")
    include(CTest)
    enable_testing()
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY build)
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY build)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY build)
    set(CPACK_PROJECT_NAME ${PROJECT_NAME})
    set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
    include(CPack)


    ################################################
    # GOOGLE TEST
    ################################################
    # Download and unpack googletest at configure time
    configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
    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()
    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)

    # 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
                     EXCLUDE_FROM_ALL)

    # The gtest/gtest_main targets carry header search path
    # dependencies automatically when using CMake 2.8.11 or
    # later. Otherwise we have to add them here ourselves.
    if (CMAKE_VERSION VERSION_LESS 2.8.11)
      include_directories("${gtest_SOURCE_DIR}/include")
    endif()

    # # Now simply link against gtest or gtest_main as needed. Eg
    # add_executable(example example.cpp)
    # target_link_libraries(example gtest_main)
    # add_test(NAME example_test COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/example)
    # ################################################
    include(GoogleTest)
    add_executable(example example.cpp)
    target_link_libraries(example gtest)
    gtest_discover_tests(example)

example.cpp:

    #include "gtest/gtest.h"
    using namespace std;
    int main()
    {
        testing::InitGoogleTest();
        auto a = RUN_ALL_TESTS();
        return a;
    }

    TEST(MYTEST,MYTEST1)
    {
    EXPECT_EQ(1,1);
    }

    TEST(MYTEST,MYTEST2)
    {
    EXPECT_EQ(2,2);
    }

    TEST(MYTEST3,MYTEST12)
    {
    EXPECT_EQ(1,1);
    }

    TEST(MYTEST4,MYTEST22)
    {
    EXPECT_EQ(2,122);
    }

make output:

make
[ 16%] Built target gtest
Scanning dependencies of target example
[ 25%] Building CXX object CMakeFiles/example.dir/example.cpp.o
[ 33%] Linking CXX executable build/example
CMake Error at /usr/share/cmake-3.10/Modules/GoogleTestAddTests.cmake:39 (message):
  Error running test executable.

    Path: '/PathToExampleProject/build/example'
    Result: 1
    Output:
      [==========] Running 4 tests from 3 test suites.
      [----------] Global test environment set-up.
      [----------] 2 tests from MYTEST
      [ RUN      ] MYTEST.MYTEST1
      [       OK ] MYTEST.MYTEST1 (0 ms)
      [ RUN      ] MYTEST.MYTEST2
      [       OK ] MYTEST.MYTEST2 (0 ms)
      [----------] 2 tests from MYTEST (0 ms total)

      [----------] 1 test from MYTEST3
      [ RUN      ] MYTEST3.MYTEST12
      [       OK ] MYTEST3.MYTEST12 (0 ms)
      [----------] 1 test from MYTEST3 (0 ms total)

      [----------] 1 test from MYTEST4
      [ RUN      ] MYTEST4.MYTEST22
      /PathToExampleProject/example.cpp:27: Failure
      Expected equality of these values:
        2
        122
      [  FAILED  ] MYTEST4.MYTEST22 (0 ms)
      [----------] 1 test from MYTEST4 (0 ms total)

      [----------] Global test environment tear-down
      [==========] 4 tests from 3 test suites ran. (0 ms total)
      [  PASSED  ] 3 tests.
      [  FAILED  ] 1 test, listed below:
      [  FAILED  ] MYTEST4.MYTEST22

       1 FAILED TEST




CMakeFiles/example.dir/build.make:95: recipe for target 'build/example' failed
make[2]: *** [build/example] Error 1
make[2]: *** Deleting file 'build/example'
CMakeFiles/Makefile2:867: recipe for target 'CMakeFiles/example.dir/all' failed
make[1]: *** [CMakeFiles/example.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
0 Answers
Related