Search code examples
c++unit-testinggoogletest

Selectively executing unit tests with googletest


Let's say I have classes A, B, C and each has its own unit test in gtest.

How can I run only A test? I have this main.cpp for executing all the unit tests available.

This page recommends to use DISABLED_ in front of the class name, but it requires me to change many parts of test code.

I guess there could be a better way to control which test to run or not.

#include <iostream>
#include <gtest/gtest.h>

int main(int argc,  char ** argv)
{
    std::cout << "Running main() from gtest_main.cc\n";

    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS(); // <-- return "RUN_XYZ_TEST()" ???
    
    return 0;
}

Solution

  • When you compile this program, you will get an executable that I will call program.

    Now, if you call program with --gtest_list_tests (https://github.com/google/googletest/blob/master/docs/advanced.md#listing-test-names) you will get all tests that are enable to run. If you want to run a specific one (as in your example, test A), just call your application as (https://github.com/google/googletest/blob/master/docs/advanced.md#running-a-subset-of-the-tests):

    program --gtest_filter=A

    Note that you can apply wildcards too. To get a list of all options, try:

    program --help