// Returns true if the program exited normally with the given exit status code. ::testing::ExitedWithCode(exit_code);
// Returns true if the program was killed by the given signal. // Not available on Windows. ::testing::KilledBySignal(signal_number);
第三个参数在官网上的描述是这样的
The parameter matcher is either a matcher for a const std::string&, or a regular expression (see Regular Expression Syntax)—a bare string s (with no matcher) is treated as ContainsRegex(s), not Eq(s).
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EXIT [ OK ] EXPTEST.EXPTEST_EXIT (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
❯ g++ test.cpp -o test -lgtest && ./test test.cpp: In function 'void test_exit()': test.cpp:11:17: warning: division by zero [-Wdiv-by-zero] 11 | int ret = 10/0; | ~~^~ [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EXIT test.cpp:17: Failure Death test: test_exit() Result: died but not with expected exit code: Terminated by signal 8 Actual msg: [ DEATH ] test exit [ DEATH ]
[ FAILED ] EXPTEST.EXPTEST_EXIT (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 0 tests. [ FAILED ] 1 test, listed below: [ FAILED ] EXPTEST.EXPTEST_EXIT
❯ g++ test.cpp -o test -lgtest && ./test test.cpp: In function 'void test_exit()': test.cpp:11:17: warning: division by zero [-Wdiv-by-zero] 11 | int ret = 10/0; | ~~^~ [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EXIT [ OK ] EXPTEST.EXPTEST_EXIT (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
将第三个参数改为一个普通字符串,gtest会进行stderr输出是否包含该字符串的检查;
1 2 3 4 5 6 7 8 9 10 11
voidtest_exit() { std::cerr << "test exit\n"; int ret = 10/0; }
❯ g++ test.cpp -o test -lgtest && ./test test.cpp: In function 'void test_exit()': test.cpp:10:17: warning: division by zero [-Wdiv-by-zero] 10 | int ret = 10/0; | ~~^~ [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EXIT test.cpp:16: Failure Death test: test_exit() Result: died but not with expected error. Expected: contains regular expression "happy" Actual msg: [ DEATH ] test exit [ DEATH ]
[ FAILED ] EXPTEST.EXPTEST_EXIT (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 0 tests. [ FAILED ] 1 test, listed below: [ FAILED ] EXPTEST.EXPTEST_EXIT
1 FAILED TEST
如果在错误输出中包含happy字符串,则正常通过测试。
1 2 3 4 5 6 7 8 9 10 11
voidtest_exit() { std::cerr << "test exit happy\n"; // 包含happy字符串 int ret = 10/0; }
[==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EXIT [ OK ] EXPTEST.EXPTEST_EXIT (1 ms) [----------] 1 test from EXPTEST (1 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (1 ms total) [ PASSED ] 1 test.
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EXIT [ OK ] EXPTEST.EXPTEST_EXIT (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EXIT test.cpp:15: Failure Death test: test_exit() Result: died but not with expected exit code: Exited with exit status 0 Actual msg: [ DEATH ] test exit [ DEATH ]
[ FAILED ] EXPTEST.EXPTEST_EXIT (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 0 tests. [ FAILED ] 1 test, listed below: [ FAILED ] EXPTEST.EXPTEST_EXIT
1 FAILED TEST
注意,抛出异常并没有归结到DEATH和EXIT的判定范围内。
1 2 3 4 5 6 7 8 9 10 11
voidtest_exit() { std::cerr << "test exit happy\n"; // int ret = 10/0; throw std::runtime_error("123"); }
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EXIT test.cpp:16: Failure Death test: test_exit() Result: threw an exception. Error msg: [ DEATH ] test exit happy [ DEATH ] [ DEATH ] test.cpp:16:: Caught std::exception-derived exception escaping the death test statement. Exception message: 123 [ DEATH ]
[ FAILED ] EXPTEST.EXPTEST_EXIT (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 0 tests. [ FAILED ] 1 test, listed below: [ FAILED ] EXPTEST.EXPTEST_EXIT
1 FAILED TEST
4.3 ASSERT_DEBUG_DEATH
对于ASSERT_DEBUG_DEATH,官方文档是这么描述的:
In debug mode, behaves the same as EXPECT_DEATH. When not in debug mode (i.e. NDEBUG is defined), just executes statement.
/* void assert (int expression); If NDEBUG is defined, do nothing. If not, and EXPRESSION is zero, print an error message and abort. */
#ifdef NDEBUG
## define assert(expr) (__ASSERT_VOID_CAST (0))
/* void assert_perror (int errnum); If NDEBUG is defined, do nothing. If not, and ERRNUM is not zero, print an error message with the error text for ERRNUM and abort. (This is a GNU extension.) */
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EXIT [ OK ] EXPTEST.EXPTEST_EXIT (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EXIT test exit happy [ OK ] EXPTEST.EXPTEST_EXIT (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_THROW [ OK ] EXPTEST.EXPTEST_THROW (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
当抛出的异常类型不一致的时候会出错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_THROW test.cpp:12: Failure Expected: test_throw() throws an exception of type std::runtime_error. Actual: it throws std::length_error with description "123".
[ FAILED ] EXPTEST.EXPTEST_THROW (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 0 tests. [ FAILED ] 1 test, listed below: [ FAILED ] EXPTEST.EXPTEST_THROW
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EQ [ OK ] EXPTEST.EXPTEST_EQ (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
TEST(EXPTEST, EXPTEST_GE) { int ret = 10; ASSERT_GE(ret,5); ASSERT_LE(ret,29); }
1 2 3 4 5 6 7 8 9 10 11
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_EQ [ OK ] EXPTEST.EXPTEST_EQ (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
EXPECT_NEAR(val1,val2,abs_error) ASSERT_NEAR(val1,val2,abs_error) // Verifies that the difference between val1 and val2 does not exceed the absolute error bound abs_error.
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_NEAR [ OK ] EXPTEST.EXPTEST_NEAR (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_NEAR test.cpp:8: Failure The difference between 3.14 and 3.15 is 0.0099999999999997868, which exceeds 0.001, where 3.14 evaluates to 3.1400000000000001, 3.15 evaluates to 3.1499999999999999, and 0.001 evaluates to 0.001.
[ FAILED ] EXPTEST.EXPTEST_NEAR (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 0 tests. [ FAILED ] 1 test, listed below: [ FAILED ] EXPTEST.EXPTEST_NEAR
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_STR [ OK ] EXPTEST.EXPTEST_STR (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_TRUE [ OK ] EXPTEST.EXPTEST_TRUE (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
❯ g++ test.cpp -o test -lgtest && ./test In file included from /usr/local/include/gtest/gtest-printers.h:122, from /usr/local/include/gtest/gtest-matchers.h:49, from /usr/local/include/gtest/internal/gtest-death-test-internal.h:47, from /usr/local/include/gtest/gtest-death-test.h:43, from /usr/local/include/gtest/gtest.h:65, from test.cpp:1: test.cpp: In member function 'virtual void EXPTEST_EXPTEST_TRUE_Test::TestBody()': test.cpp:8:5: error: converting to 'bool' from 'std::nullptr_t' requires direct-initialization [-fpermissive] 8 | ASSERT_FALSE(nullptr); | ^~~~~~~~~~~~
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_TRUE test.cpp:13: Failure Failed
add.hpp:20: Failure Failed
[ FAILED ] EXPTEST.EXPTEST_TRUE (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 0 tests. [ FAILED ] 1 test, listed below: [ FAILED ] EXPTEST.EXPTEST_TRUE
1 FAILED TEST
这里也能看到,ADD_FAILURE后,单元测试会像EXPECT宏一样继续往后运行。
4.11 ASSERT_THAT
和ASSERT_THAT相关的matcher选项在gtest中提供了,注意需要引用gmock头文件
1 2 3 4 5 6 7
#include<gmock/gmock.h>
using ::testing::AllOf; using ::testing::Gt; using ::testing::Lt; using ::testing::MatchesRegex; using ::testing::StartsWith;
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_THAT [ OK ] EXPTEST.EXPTEST_THAT (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 1 test.
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 1 test from 1 test suite. [----------] Global test environment set-up. [----------] 1 test from EXPTEST [ RUN ] EXPTEST.EXPTEST_PRED test.cpp:14: Failure is_eq(1, 2) evaluates to false, where 1 evaluates to 1 2 evaluates to 2
[ FAILED ] EXPTEST.EXPTEST_PRED (0 ms) [----------] 1 test from EXPTEST (0 ms total)
[----------] Global test environment tear-down [==========] 1 test from 1 test suite ran. (0 ms total) [ PASSED ] 0 tests. [ FAILED ] 1 test, listed below: [ FAILED ] EXPTEST.EXPTEST_PRED
❯ g++ test.cpp -o test -lgtest && ./test [==========] Running 2 tests from 1 test suite. [----------] Global test environment set-up. [----------] 2 tests from MyClassTest [ RUN ] MyClassTest.MyClassTestAddX set up for MyClass tear down for MyClass [ OK ] MyClassTest.MyClassTestAddX (0 ms) [ RUN ] MyClassTest.MyClassTestAddY set up for MyClass tear down for MyClass [ OK ] MyClassTest.MyClassTestAddY (0 ms) [----------] 2 tests from MyClassTest (0 ms total)
[----------] Global test environment tear-down [==========] 2 tests from 1 test suite ran. (0 ms total) [ PASSED ] 2 tests.