I'm trying to setup Catch2 test framework for a library I have built. I'm using CMake and Visual Studio 2017.
My project structure is:
executable-project/
|-- library
|--include/
| |--SUT.h
|--src/
| |--SUT.cpp
|--tests/
| |--catch.hpp
| |--SUTTest.cpp
|CMakeLists.txt
|include/
|src/
| |--main.cpp
|CMakeLists.txt
SUT.h
, SUT.cpp
and SUTTest.cpp
are just testing a factorial function defined based on the example.
My CMakeLists.txt file in the library is
cmake_minimum_required (VERSION 3.8)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
file(GLOB HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
file(GLOB TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp")
add_library(MY_LIBRARY ${HEADER_FILES} ${SOURCE_FILES} ${TEST_FILES})
target_include_directories(MY_LIBRARY PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
source_group("Test Files" FILES ${TEST_FILES})
The SUTTest.cpp
file is:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "SUT.h"
TEST_CASE("Factorials are computed", "[factorial]")
{
SUT sut;
REQUIRE(sut.Factorial(0) == 0);
REQUIRE(sut.Factorial(1) == 1);
REQUIRE(sut.Factorial(2) == 2);
REQUIRE(sut.Factorial(3) == 6);
REQUIRE(sut.Factorial(10) == 3628800);
}
The "executable-project" simply makes use of the library (it's the actual application). The CMakeLists.txt
in that project dynamically links the library to it.
When I build this solution in Visual Studio, build works fine.
However, the test isn't failing despite asserting that the Factorial(0) == 0
. Also, Visual Studio is not discovering the tests in the tests folder. What I'm trying to achieve are:
When I click on Build in Visual Studio, the tests to be run as part of the build.
(Optional) to be able for Visual Studio to discover the tests.
EDIT:
cmake_minimum_required (VERSION 3.8)
project("My Project")
file(GLOB HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
add_subdirectory(library)
add_executable(MY_APP main.cpp ${HEADER_FILES} ${SOURCE_FILES})
target_include_directories(MY_APP PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(MY_APP MY_LIBRARY)