简体   繁体   English

设置 GoogleTest 以在一个可执行文件中运行所有测试

[英]Setting up GoogleTest to run all tests in one executable

In setting up my most recent project I am attempting to use GoogleTest.在设置我最近的项目时,我正在尝试使用 GoogleTest。 Currently I have this:目前我有这个:

macro(add_test_l TEST_NAME)

    # Create the executable and use the last arguments as source files.
    add_executable(${TEST_NAME} ${ARGN})

    # Link in the AMR and gtest libraries.
    target_link_libraries(${TEST_NAME}
        ${PROJECT_NAME}
        gtest
        gmock
        gtest_main
    )

    # Add the test to gtest.
    add_test(
        NAME ${TEST_NAME}
        COMMAND ${TEST_NAME}
    )

    list(APPEND TEST_SOURCE_FILES ${ARGN})

endmacro()

Then I can add my tests like add_test_l(Element Sets/Element.test.cpp) which is working and convenient.然后我可以添加我的测试,比如add_test_l(Element Sets/Element.test.cpp) ,它既有效又方便。 But this of course creates an executable for each test, which is not a bad thing as it allows for quick testing of a single file.但这当然会为每个测试创建一个可执行文件,这并不是一件坏事,因为它允许快速测试单个文件。

Though I do want the ability to run all the tests with a single exe (that way CI will be easier) so I have that list at the end of my macro and after adding all my individual tests I have:虽然我确实希望能够使用单个 exe 运行所有测试(这样 CI 会更容易),但我在宏的末尾有这个列表,并且在添加了我所有的个人测试之后:

add_executable(all_tests ${TEST_SOURCE_FILES})
target_link_libraries(all_tests
    ${PROJECT_NAME}
    gtest
    gmock
    gtest_main
)

Which creates an EXE to run all my test cases.它创建了一个 EXE 来运行我所有的测试用例。

This does not seem efficient as I compile all my files twice.这似乎效率不高,因为我将所有文件编译了两次。 Is there a better way for me to achieve the desired outcome?有没有更好的方法让我达到预期的结果? Perhaps I can just add an option to enable / disable individual vs all tests exes.也许我可以添加一个选项来启用/禁用单个测试与所有测试 exe。

It is unnecessary to have an executable per each file.每个文件都不需要一个可执行文件。 Build one executable for all tests and learn the gtest option --gtest_filter.为所有测试构建一个可执行文件并学习 gtest 选项 --gtest_filter。 You can run each test individually:您可以单独运行每个测试:

all_tests --gtest_filter=Element.Test

Or you can run all Element tests like the macro add_test_l does it:或者你可以像宏add_test_l那样运行所有元素测试:

all_tests --gtest_filter=Element.*

More info about command line options is available:有关命令行选项的更多信息可用:

all_tests --help

One of the useful commands:有用的命令之一:

all_tests --gtest_list_tests

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

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