添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
坚韧的酸菜鱼  ·  周申_百度百科·  1 月前    · 
旅途中的硬盘  ·  時空長廊·  2 月前    · 
淡定的匕首  ·  无锡地铁_百度百科·  1 年前    · 

CLion 2023.2 Help

Google Test

Google Test and Google Mock are a pair of unit testing tools developed by Google.

Adding Google Test to your project

  1. Download Google Test from the official repository and extract the contents of googletest-main into an empty folder in your project (for example, Google_tests/lib ).

    Alternatively, clone Google Test as a git submodule or use CMake to download it (instructions below will not be applicable in the latter case).

  2. Create a CMakeLists.txt file inside the Google_tests folder: right-click it in the project tree and select New | CMakeLists.txt .

    Customize the following lines and add them into your script:

    # 'Google_test' is the subproject name project(Google_tests) # 'lib' is the folder with Google Test sources add_subdirectory(lib) include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) # 'Google_Tests_run' is the target name # 'test1.cpp test2.cpp' are source files with tests add_executable(Google_Tests_run test1.cpp test2.cpp) target_link_libraries(Google_Tests_run gtest gtest_main)
  3. In your root CMakeLists.txt script, add the add_subdirectory(Google_tests) command to the end, then reload the project.

  4. When writing tests, make sure to add #include "gtest/gtest.h" at the beginning of every .cpp file with your tests code.

Generate menu for Google Test

In the files with gtest included, you can generate code for tests and test fixtures using the Generate menu Alt+Insert .

When called from a fixture, this menu additionally includes SetUp Method and TearDown Method :

Generate menu for Google tests

Google Test run/debug configuration

Although Google Test provides the main() entry, and you can run tests as regular applications, we recommend using the dedicated Google Test run/debug configuration. It includes test-related settings and let you benefit from the built-in test runner , which is unavailable if you run tests as regular programs.

  1. To create a Google Test configuration, go to Run | Edit Configurations , click App general add and select Google Test from the list of templates.

  2. Specify the test or suite to be included in the configuration, or provide a pattern for filtering test names. Auto-completion is available in the fields to help you quickly fill them up:

    Completion in configuration fields

    Set wildcards to specify test patterns, for example:

    Pattern for tests

  3. In other fields of the configuration settings, you can set environment variables and command line options. For example, use Program arguments field to pass the --gtest_repeat flag and run a Google test multiple times:

    Test flags in program arguments

    The output will look as follows:

    Repeating all tests (iteration 1) ... Repeating all tests (iteration 2) ...