Catch2 CLion error, "No tests were found" - c++

I have a folder structure like
and I am trying to get Catch2 setup, my CMake files look like:
the topmost CMake:
cmake_minimum_required(VERSION 3.21)
project(throwaway)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(src)
add_subdirectory(tests)
add_executable(foo_main main.cpp)
target_link_libraries(foo_main PUBLIC foo_lib)
my src CMake:
add_subdirectory(foo)
my src/foo CMake:
add_library(foo_lib Foo.cpp)
my tests CMake:
add_subdirectory(foo)
add_executable(foo_test catch_runner.cpp)
target_link_libraries(foo_test PUBLIC
foo_test_lib)
my tests/foo CMake
add_library(foo_test_lib
FooTests.cpp)
target_link_libraries(foo_test_lib PUBLIC
foo_lib)
From there, I used Clion's Catch2 integration to set up my run config as
so nothing crazy here
However, I get this error:
I discovered that the error goes away if I edit the tests CMake into
add_executable(foo_test catch_runner.cpp foo/FooTests.cpp)
target_link_libraries(foo_test PUBLIC foo_lib)
and the test works as expected. But I obviously don't want to manually add each file into an executable, I want to be able to make a library that I can just slap the catch_runner into.
I have no clue why the test doesn't work when I link it as a library, but works when I add it manually. Any ideas?

Figured out it from the links at How do you add separate test files with Catch2 and CMake?
in my tests/foo CMake I changed it to
add_library(foo_test_lib OBJECT
FooTests.cpp)
target_link_libraries(foo_test_lib PUBLIC
foo_lib)

Related

How to add_dependencies between projects in cmake

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} source)
project(abc)
#a_certain_source_file.cpp is a generated file built by another project.
add_library(${PROJECT_NAME} STATIC ${source} ${xyz_BIN_DIR}/a_certain_source_file.cpp)
add_dependencies(${PROJECT_NAME} xyz)
target_link_libraries(${PROJECT_NAME} PRIVATE xyz)
# include xyz_SOURCE_DIR directory to include a_certain_source_file.cpp
target_include_directories(${PROJECT_NAME} PRIVATE ${xyz_SOURCE_DIR})
# Installation
install(TARGETS ${PROJECT_NAME} DESTINATION ${DST_LIB_DIR})
I have a CMakeLists.txt as above. Trying to build project abc. But to build it, I also need "a_certain_source_file.cpp" which is an auto generated source file from another project called xyz. If xyz had been built from this same CMakeLists.txt, there would have been no problem in add_dependencies working. I am unable to get the dependency on "a_certain_source_file.cpp" resolved with the way i have my CMakeLists.txt right now. Any CMake Enthusiasts or specialists that can help ?
I also saw a close match here - cmake: add_dependencies does not work with external projects but I don't need anything downloaded. So am not sure if this is what I need.
Create custom command with add_custom_command marking ${xyz_BIN_DIR}/a_certain_source_file.cpp as OUTPUT and make it depend on a xyz target. This will teach CMake that ${xyz_BIN_DIR}/a_certain_source_file.cpp is a generated file, and what should it do to generate it.

google test with visual studio 2019 and cmake

I am trying to setup google test with visual studio 2019 and cmake.
This is my CMakeFileLists.txt content:
cmake_minimum_required(VERSION 3.0)
project(test_me)
# GTest
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
# Unit Tests
# Add test cpp file
add_executable( runUnitTests tests.cpp)
# Link test executable against gtest & gtest_main
target_link_libraries(runUnitTests ${GTEST_BOTH_LIBRARIES})
add_test( runUnitTests runUnitTests )
My tests.cpp file looks like this:
#include <gtest/gtest.h>
TEST(ABC, TEST1) {
EXPECT_EQ(true, true);
}
TEST(ABC, TEST2) {
ASSERT_TRUE(2 == 2);
}
this minimal example is from another stackoverflow question:
CMake file for integrated Visual Studio unit testing
Thats what I get after I have build the application:
a single Test with the name runUnitTests. However in the picture of the answer from the question above I would expect to see the name of each test function. Something like this:
runUnitTests
- ABC
- TEST1
- TEST2
I have tested it with a new visual studio solution and added a google unit test project. Pasting the test functions into this projects results into this picture:
So this works fine. It must have something to do with the open a local folder method which I am using to handle my cmake project.
Here is a possible answer to your question:
cmake_minimum_required(VERSION 3.10)
project(test_me)
enable_testing()
find_package(GTest REQUIRED)
include(GoogleTest)
add_executable(runUnitTests tests.cpp)
target_link_libraries(runUnitTests GTest::GTest GTest::Main)
gtest_discover_tests(runUnitTests)
The command that discovers the full list of tests in your test binary is gtest_discover_tests(), and it is part of the GoogleTest CMake module (you can see the docs locally with cmake --help-module GoogleTest). This module has been introduced with CMake 3.9, but the command gtest_discover_tests() was only added in CMake 3.10. You should note, however, that if your test binary has many test cases, this will slow things down significantly.

Building an external static library in CMake, and including the result

My C++ project has a dependency to a library, that I have the source for in a subdirectory. This external library has an option 'build_static_lib' for building it as a static library in its CMakeLists.txt file.
In my top directory CMakeLists.txt, my understanding is that this should be simple:
add_executable(myproject ${SOURCES})
set(build_static_lib ON)
add_subdirectory(${CMAKE_SOURCE_DIR}/subdirectory/external_project)
unset(build_static_lib)
....
target_link_libraries(myproject PRIVATE externalproject another_library)
When I run this, my sources are built, then during the make, I am given an error that it cannot find -lexternalproject.
The strange thing is, when I change CMakeLists.txt (or delete the CMakeCache), and run it again, suddenly it successfully builds the libexternalproject and links it correctly.
Am I missing something? My journey with CMake has been unbelievably painful and any suggestions would be appreciated. It seems like every online resource has a very different strategy, and none of them quite achieve this presumably simple case. Thanks!
EDIT:
The content of the external project's CMakeLists.txt is https://github.com/muflihun/easyloggingpp/blob/master/CMakeLists.txt:
if (build_static_lib)
if (lib_utc_datetime)
add_definitions(-DELPP_UTC_DATETIME)
endif()
require_cpp11()
add_library(easyloggingpp STATIC src/easylogging++.cc)
install(TARGETS
easyloggingpp
ARCHIVE DESTINATION lib)
endif()

Add/include a Cmake that finds a package into CMake

I found this CMake to find OpenBLAS but I can't find a way how to include that as an external file.
What I have in mind is like #include in C/C++. I tried googling bit i get the answer on how to include a project into CMake.
The main reason for this is that I want to have my CMake as clean and as small as possible since this is the fist time I am diving deeper in CMake world.
but I can't find a way how to include that as an external file.
You need:
Save the module (FindOpenBLAS.cmake) inside your project, for example:
Project
└── cmake
└── Modules
└── FindOpenBLAS.cmake
Add the path into CMake variable inside CMakeLists.txt:
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/Modules/")
Add find_package directive inside CMakeLists.txt:
find_package (OpenBLAS REQUIRED)
Use populated variables, for example inside CMakeLists.txt:
include_directories (${OpenBLAS_INCLUDE_DIR})
...
target_link_libraries (${OpenBLAS_LIB})

VTK CMakeLists.txt for test classes

A typical 'CMakeLists.txt' included with a VTK wiki example is shown below;
cmake_minimum_required(VERSION 2.8)
PROJECT(Arrow)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(Arrow MACOSX_BUNDLE Arrow)
if(VTK_LIBRARIES)
target_link_libraries(Arrow ${VTK_LIBRARIES})
else()
target_link_libraries(Arrow vtkHybrid)
endif()
I'm able to build the examples successfully in Windows 7 and Visual Studio 2012.
I would like to write a 'CMakeLists.txt' file so that I can build/run a test class (i.e. This one ReebGraph/Testing). I'm assuming I'm right in saying that they require different kinds of make files. The 'CMakeLists.txt' for 'TestReebGraph.cxx' is shown below.
vtk_add_test_cxx(TestReebGraph.cxx NO_DATA NO_VALID NO_OUTPUT)
vtk_test_cxx_executable(${vtk-module}CxxTests)
How would I write one for the testing class? Do I somehow have to merge the two?
The cmake commands vtk_* are for writing tests inside of the VTK CMake system. What you'll want is to first enable_testing() in your CMakeLists.txt. Then you'll have to change the function TestReebGraph to be called 'main' in TestReebGraph.cxx. You can then use add_test(TestReebGraph TestReebGraph.cxx) to build the test that you have presumably copied into your project directory.