C ++ Class Testing with Catch

I decided to make a small project, which I will tell with the help of tests, as far as I can. I use CLION (which uses CMake) and the Catch library for testing.

The problem is that I get the method undefined reference to TestClass::addwhile running the test class.

Here is my setup (this is dummy as I wanted to make sure everything works):

TestClass.h

#ifndef LLL_TESTCLASS_H
#define LLL_TESTCLASS_H

class TestClass {
public:
    int add(int a, int b);
};

#endif //LLL_TESTCLASS_H

Testclass.cpp

#include "TestClass.h"
int TestClass::add(int a, int b) {
    return a + b;
}

test.cpp - file with tests

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "../src/TestClass.h"

TEST_CASE("addition") {
    TestClass testClass;
    REQUIRE(testClass.add(2,3) == 5);
    REQUIRE(testClass.add(-1, 1) == 0);
    REQUIRE(testClass.add(2, 4) == 1);
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.2)
project(LLL)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(LIB_DIR "lib")
include_directories(${LIB_DIR}/Catch)
include_directories(${LIB_DIR}/Eigen)

set(SRC_DIR src)
set(SOURCE_FILES ${SRC_DIR}/main.cpp src/TestClass.h src/TestClass.cpp)
add_executable(LLL ${SOURCE_FILES})

set(TEST_DIR test)
set(TEST_FILES ${TEST_DIR}/test.cpp)
add_executable(LLL_TEST ${TEST_FILES})
+4
source share
2 answers

You did not specify TestClass. {. h, .cpp} to compile for LLL_TESTexecutable purpose:

set(TEST_FILES src/TestClass.h src/TestClass.cpp ${TEST_DIR}/test.cpp)

Or it’s better to move it to some kind of shared library and just connect to it.

, 2 : LLL LLL_TEST . . LLL target TestClass satistifed, LLL, ld . LLL_TEST , , - , .

+4

, .

, . , , , TDD.

cmake_minimum_required(VERSION 3.2)
project(LLL)

set(CMAKE_CXX_STANDARD 11) # adds -std=c++11
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(LIB_DIR "lib")
include_directories(${LIB_DIR}/Catch)
include_directories(${LIB_DIR}/Eigen)

add_library(LLL_LIB src/TestClass.h src/TestClass.cpp)

add_executable(LLL src/main.cpp)
target_link_libraries(LLL LLL_LIB)

add_executable(LLL_TEST test/test.cpp)
target_link_libraries(LLL_TEST LLL_LIB)

script LLL_LIB, . , :

add_library(LLL_LIB SHARED src/TestClass.h src/TestClass.cpp)

.

+4

All Articles