简体   繁体   English

如何运行 RUN_ALL_TESTS()

[英]How to run RUN_ALL_TESTS()

I'm trying to figure it out Google Test framework + CLion.我试图弄清楚谷歌测试框架+ CLion。

I unpacked the archive into ~/Documents/Libraries/googletest-main/ .我将存档解压缩到~/Documents/Libraries/googletest-main/中。 Installed it, so that includes and libs are in /usr/local/include/ and /usr/local/lib/ , correspondingly.安装它,以便包含和库在/usr/local/include//usr/local/lib/中,相应地。

Then, created a project in CLion with two files:然后,在 CLion 中创建了一个包含两个文件的项目:

CMakeLists.txt 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测试.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.现在,作为 CLion 界面的一部分,我可以独立运行测试。 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 ).从框架文档和教程中,我知道我不能实现main() ,而是使用 gtest_main.cc 中实现的gtest_main.cc (对我来说,路径是~/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.) (通常,在教程中,框架文件位于项目文件夹中,因此可以运行 function RUN_ALL_TEST()。)

gtest_main links your code to google test where the main function is already written for you See here . gtest_main将您的代码链接到 google 测试,其中main的 function 已经为您编写,请参见此处

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 .如果你想自己写主要的 function,你应该链接到getst Your main function then can be as follows:你的主要 function 那么可以如下:

#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 .请参阅此实时示例: https://godbolt.org/z/6nvzf3TWa

Update :更新

You are linking your project to both gtest and gtest_main .您正在将您的项目链接到gtestgtest_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.如果你选择gtest_main ,你的项目将链接到这个已经有一个主 function 的文件 Otherwise, you should write the main function yourself.否则,你应该自己写主function。

Usually, you should create two projects.通常,您应该创建两个项目。 One for testing, which you have ( TestProject ), and one for production, which is your main project.一个用于测试,您拥有 ( TestProject ),另一个用于生产,这是您的主要项目。 This should be a new project and have its own main function and not be linked to gtest of gtest_main.这应该是一个新项目,并且有自己的main function 并且没有链接到 gtest_main 的 gtest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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