Partilhar via


the usage of running a subset of tests in google test framework:--gtest_filter

Google provides an easy-to-use open source alternative for developing unit tests in C++ on multiple platforms, such as Linux, windows, max etc. Here I only want to introduce the simple usage to run specific tests. Google test enables this by providing --gtest_filter=<test string> option. In the google testing framework, the test is specified by test case name and test name. Below is an example using this framework.

#include "gtest/gtest.h"  //header file

TEST(IsPrimerTest, PositiveNumber)  //TEST(test_case_name, test_name)

{

    EXPECT_EQ(1, IsPrimer(5));

}

To run tests, whose test_case name or test_name contains string "str", execute  main.exe --gtest_filter=*str*

To run tests, whose test_case_name contains string "str", execute main.exe --gtest_filter=*str*.*

To run tests, whose test_name contains string "str" or "str2", execute main.exe --gtest_filter=*.*str*:*.*str2*

To run tests, whose test_name doesn't contain string "str", execute main.exe --gtest_filter=*.*-*str*

To run tests, whose test_case_name or test_name doesn't contain either "str" or "str2", execute main.exe --gtest_filter=-*str*:*str2*

================================================================================================

*      match any string

?     match any single character

:      separated list of patterns, if followed by '-', negative patterns, otherwise positive patterns.

-      exclude. e.g. -a means excluding a