[Google Test] 選擇性的執行某些 Unit-test case

[Google Test] 選擇性的執行某些 Unit-test case

今天在跑 unit-testing 程式時,發現在跑到某個 test case 時會一直出錯,

但是如果每次都跑全部的 test case 又頗浪費時間,

於是來找找看 Google Test 如何選擇性的執行某些特定的 test case…


找了一下,原來 Google Test 編譯出來的 test program 本身就有提供這項功能,

而且用 -h 選項就可以列出所有支援的選項:

testuser@localhost ~ $ ./TestScan -h
This program contains tests written using Google Test. You can use the
following command line flags to control its behavior:
Test Selection:
List the names of all tests instead of running them. The name of
TEST(Foo, Bar) is "Foo.Bar".
Run only the tests whose name matches one of the positive patterns but
none of the negative patterns. '?' matches any single character; '*'
matches any substring; ':' separates two patterns.
Run all disabled tests too.
Test Execution:
Run the tests repeatedly; use a negative count to repeat forever.
Randomize tests' orders on every iteration.
      Random number seed to use for shuffling test orders (between 1 and
      99999, or 0 to use a seed based on the current time).
Test Output:
      Enable/disable colored output. The default is auto.
      Don't print the elapsed time of each test.
Generate an XML report in the given directory or with the given file
name. FILE_PATH defaults to test_details.xml.
Stream test results to the given server.
Assertion Behavior:
Set the default death test style.
Turn assertion failures into debugger break-points.
Turn assertion failures into C++ exceptions.
Do not report exceptions as test failures. Instead, allow them
to crash the program or throw a pop-up (on Windows).
Except for --gtest_list_tests, you can alternatively set the corresponding
environment variable of a flag (all letters in upper-case). For example, to
disable colored text output, you can either specify --gtest_color=no or set
the GTEST_COLOR environment variable to no.
For more information, please read the Google Test documentation at
http://code.google.com/p/googletest/. If you find a bug in Google Test
(not one in your own code or tests), please report it to


像是執行 –gtest_list_tests 就能列出這個測試程式裡所有的 test case:

testuser@localhost ~ $ ./TestScan --gtest_list_tests


而用 –gtest_filter 選項可以決定要執行的 test case,

例如下面的例子是執行 test case 名稱裡有 PostProcess 字眼的 test case:

testuser@localhost ~ $ ./TestScan --gtest_filter=*PostProcess*
Note: Google Test filter = *PostProcess*
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from ScanFileTest
[ RUN      ] ScanFileTest.PostProcess
[       OK ] ScanFileTest.PostProcess (0 ms)
[----------] 1 test from ScanFileTest (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1570 ms total)
[  PASSED  ] 1 test.



stackoverflow: How to run specific test cases in GoogleTest

Google Test: Running a Subset of the Tests


(本頁面已被瀏覽過 1,521 次)


發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料