简体   繁体   中英

How to run RUN_ALL_TESTS()

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.)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM