2

I'm trying to figure it out Google Test framework + CLion.

I unpacked the archive into ~/Documents/Libraries/googletest-main/. Installed it, so that includes and libs are in /usr/local/include/ and /usr/local/lib/, correspondingly.

Then, created a project in CLion with two files:

CMakeLists.txt

cmake_minimum_required(VERSION 3.23)
project(TestProject)

set(CMAKE_CXX_STANDARD 14)

add_executable(TestProject test.cpp)

target_link_libraries(TestProject gtest gtest_main)

test.cpp

#include "gtest/gtest.h"

TEST(BasicTests, testName) {
    EXPECT_EQ(1, 2);
}

TEST(BasicTests, testName2) {
    ASSERT_EQ(2, 2);
}

TEST(BasicTests, testName3) {
    ASSERT_EQ(3, 3);
}

TEST(BasicTests, testName4) {
    ASSERT_EQ(4, 4);
}

Now, as a part of the CLion interface, I can run tests, but independently. From the framework documentation and tutorials, I know that I can not implement main(), but use the function implemented in gtest_main.cc (for me, the path is ~/Documents/Libraries/googletest-main/googletest/scr/gtest_main.cc).

What needs to be done to run all the tests at once? (Often, in tutorials, the framework files lie inside the project folder, so the function RUN_ALL_TEST() can be run.)

Arseniy Spiridonov
  • 337
  • 1
  • 2
  • 16

1 Answers1

0

gtest_main links your code to google test where the main function is already written for you See here.

Once you build the target, an executable should be created that you can run to see the result of the tests.

If you want to write the main function yourself, you should link to getst. Your main function then can be as follows:

#include "gtest/gtest.h"

TEST(BasicTests, testName) {
    EXPECT_EQ(1, 2);
}

TEST(BasicTests, testName2) {
    ASSERT_EQ(2, 2);
}

TEST(BasicTests, testName3) {
    ASSERT_EQ(3, 3);
}

TEST(BasicTests, testName4) {
    ASSERT_EQ(4, 4);
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

See this live example: https://godbolt.org/z/6nvzf3TWa.

Update:

You are linking your project to both gtest and gtest_main.

target_link_libraries(TestProject gtest gtest_main)

My point was that you should pick one. If you pick gtest_main, your project will be linked to this file which already has a main function. Otherwise, you should write the main function yourself.

Usually, you should create two projects. One for testing, which you have (TestProject), and one for production, which is your main project. This should be a new project and have its own main function and not be linked to gtest of gtest_main.

Ari
  • 7,251
  • 11
  • 40
  • 70
  • Well, it is clear that it needs to add the call of main() with running all test. I guess there is no way to call it from the file 'gtest_main.cc', right? – Arseniy Spiridonov Aug 01 '22 at 16:36
  • Another question about organising the project. Suppose that there are files of project and files connected with testing. I should have two main calls and run one or another depending on debug or release mode (if yes, so how to divide these calls?). Or there is one main call where all test are run and then other actions happen? – Arseniy Spiridonov Aug 01 '22 at 16:45
  • Thanks for update. I explored the link you gave in the answer. And I have the same situation described there: the size of `libgtest.a` is much bigger than `libgtest_main.a`. So, if I link only `libgtest_main` and remove main function, then I get a bunch of errors "Undefined symbols for architecture x86_64", that is the functions in `gtest_main.cc` are unknown. In other words, `libgtest_main` does not contain the definitions as 'libgtest' has. However, if I link both libs and do not write main function, then I can run only one of the tests. – Arseniy Spiridonov Aug 02 '22 at 08:36
  • Can you explain creating two projects, the testing and production ones, and their configuration within CLion? Is it multi-objective project or these are separate projects with common source files (implementations and headers)? – Arseniy Spiridonov Aug 02 '22 at 08:38