Boost.TEST with CLion: "Test framework quit unexpectedly"

1.4k Views Asked by At

I am using CLion on mac os mohave. I tried to add Boost.TEST to my c++ project, but the IDE throws "Test framework quit unexpectedly".

Here is my CMakeLists:

cmake_minimum_required(VERSION 3.12)
project(sequences)

find_package(Boost COMPONENTS unit_test_framework REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

set(CMAKE_CXX_STANDARD 11)

add_executable(sequences main.cpp)

target_link_libraries(sequences ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})

enable_testing()

And a simple test:

#include <iostream>
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(test) {
    BOOST_CHECK_EQUAL(1, 1);
}

using namespace std;

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Instructions on jetbrains website are bogus about using Boost.TEST and all fixes for this issue I found online are outdated.

I use Boost v.1.67.0 installed with homebrew.

Console output:

Testing started at 09:48 ... .../sequences --run_test=test --logger=HRF,all --color_output=false --report_format=HRF --show_progress=no Hello, World! Process finished with exit code 0

All help and advice are appreciated!

Thanks for help. I ended up creating a template project for tests. I then copy this project into my the project with the actual code and add add_subdirectory(name_of_the_directory_with_boost_test_project) into CMakeFile.

After that you get the "run all test" functionality in CLion.

1

There are 1 best solutions below

6
On BEST ANSWER

Your program code and your test code should be separated. As stated in doc, the line

#define BOOST_TEST_MODULE My Test

creates the main entry point.

I don't know if it is possible to write test cases in main.cpp. In unit tests you check the interface of your components.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)
project(sequences)

find_package(Boost COMPONENTS unit_test_framework REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

set(CMAKE_CXX_STANDARD 11)

add_executable(main src/main.cpp)
add_executable(test1 src/test1.cpp)

target_link_libraries(test_suite1 ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})

enable_testing()
add_test(NAME test_suite1 COMMAND test_suite1)

main.cpp

#include "MyMath.h"

int main() {
    MyMath mm;
    mm.add(1,2);

    return 0;
}

MyMath.h:

#ifndef MY_MATH_H
#define MY_MATH_H

class MyMath {
public:
    int add(int l, int r) { return l + r; }
};

#endif

test1.cpp:

#define BOOST_TEST_MODULE My Test
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include "MyMath.h"
BOOST_AUTO_TEST_CASE(test) {
    MyMath mm;
    BOOST_CHECK_EQUAL(mm.add(1,2), 3);
}

Now I can build it with cmake and test with ctest.