Skip to content

Commit 504ea69

Browse files
Abseil Teamcopybara-github
Abseil Team
authored andcommitted
Document unexpected interaction with death test error code and gmock.
If you have a death test that uses mock objects, it's very likely that those mock objects are not properly cleaned up before death. The gMock atexit leak detector will call `_exit(1)`, overriding your expected exit code. PiperOrigin-RevId: 713722657 Change-Id: I92a326f3923efc9f0e235d825616f3094eca995c
1 parent 4bbf808 commit 504ea69

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

docs/advanced.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ corruption, security holes, or worse. Hence it is vitally important to test that
416416
such assertion statements work as expected.
417417
418418
Since these precondition checks cause the processes to die, we call such tests
419-
_death tests_. More generally, any test that checks that a program terminates
419+
*death tests*. More generally, any test that checks that a program terminates
420420
(except by throwing an exception) in an expected fashion is also a death test.
421421
422422
Note that if a piece of code throws an exception, we don't consider it "death"
@@ -462,6 +462,12 @@ verifies that:
462462
exit with exit code 0, and
463463
* calling `KillProcess()` kills the process with signal `SIGKILL`.
464464

465+
{: .callout .warning}
466+
Warning: If your death test contains mocks and is expecting a specific exit
467+
code, then you must allow the mock objects to be leaked via `Mock::AllowLeak`.
468+
This is because the mock leak detector will exit with its own error code if it
469+
detects a leak.
470+
465471
The test function body may contain other assertions and statements as well, if
466472
necessary.
467473

@@ -727,7 +733,7 @@ Some tips on using `SCOPED_TRACE`:
727733
### Propagating Fatal Failures
728734

729735
A common pitfall when using `ASSERT_*` and `FAIL*` is not understanding that
730-
when they fail they only abort the _current function_, not the entire test. For
736+
when they fail they only abort the *current function*, not the entire test. For
731737
example, the following test will segfault:
732738

733739
```c++
@@ -2382,7 +2388,7 @@ IMPORTANT: The exact format of the JSON document is subject to change.
23822388

23832389
#### Detecting Test Premature Exit
23842390

2385-
Google Test implements the _premature-exit-file_ protocol for test runners to
2391+
Google Test implements the *premature-exit-file* protocol for test runners to
23862392
catch any kind of unexpected exits of test programs. Upon start, Google Test
23872393
creates the file which will be automatically deleted after all work has been
23882394
finished. Then, the test runner can check if this file exists. In case the file

0 commit comments

Comments
 (0)