xref: /aosp_15_r20/external/googletest/docs/faq.md (revision 481dde660366d6f317d242b6974ef1b20adb843c)
1*481dde66SAndroid Build Coastguard Worker# GoogleTest FAQ
2*481dde66SAndroid Build Coastguard Worker
3*481dde66SAndroid Build Coastguard Worker## Why should test suite names and test names not contain underscore?
4*481dde66SAndroid Build Coastguard Worker
5*481dde66SAndroid Build Coastguard Worker{: .callout .note}
6*481dde66SAndroid Build Coastguard WorkerNote: GoogleTest reserves underscore (`_`) for special-purpose keywords, such as
7*481dde66SAndroid Build Coastguard Worker[the `DISABLED_` prefix](advanced.md#temporarily-disabling-tests), in addition
8*481dde66SAndroid Build Coastguard Workerto the following rationale.
9*481dde66SAndroid Build Coastguard Worker
10*481dde66SAndroid Build Coastguard WorkerUnderscore (`_`) is special, as C++ reserves the following to be used by the
11*481dde66SAndroid Build Coastguard Workercompiler and the standard library:
12*481dde66SAndroid Build Coastguard Worker
13*481dde66SAndroid Build Coastguard Worker1.  any identifier that starts with an `_` followed by an upper-case letter, and
14*481dde66SAndroid Build Coastguard Worker2.  any identifier that contains two consecutive underscores (i.e. `__`)
15*481dde66SAndroid Build Coastguard Worker    *anywhere* in its name.
16*481dde66SAndroid Build Coastguard Worker
17*481dde66SAndroid Build Coastguard WorkerUser code is *prohibited* from using such identifiers.
18*481dde66SAndroid Build Coastguard Worker
19*481dde66SAndroid Build Coastguard WorkerNow let's look at what this means for `TEST` and `TEST_F`.
20*481dde66SAndroid Build Coastguard Worker
21*481dde66SAndroid Build Coastguard WorkerCurrently `TEST(TestSuiteName, TestName)` generates a class named
22*481dde66SAndroid Build Coastguard Worker`TestSuiteName_TestName_Test`. What happens if `TestSuiteName` or `TestName`
23*481dde66SAndroid Build Coastguard Workercontains `_`?
24*481dde66SAndroid Build Coastguard Worker
25*481dde66SAndroid Build Coastguard Worker1.  If `TestSuiteName` starts with an `_` followed by an upper-case letter (say,
26*481dde66SAndroid Build Coastguard Worker    `_Foo`), we end up with `_Foo_TestName_Test`, which is reserved and thus
27*481dde66SAndroid Build Coastguard Worker    invalid.
28*481dde66SAndroid Build Coastguard Worker2.  If `TestSuiteName` ends with an `_` (say, `Foo_`), we get
29*481dde66SAndroid Build Coastguard Worker    `Foo__TestName_Test`, which is invalid.
30*481dde66SAndroid Build Coastguard Worker3.  If `TestName` starts with an `_` (say, `_Bar`), we get
31*481dde66SAndroid Build Coastguard Worker    `TestSuiteName__Bar_Test`, which is invalid.
32*481dde66SAndroid Build Coastguard Worker4.  If `TestName` ends with an `_` (say, `Bar_`), we get
33*481dde66SAndroid Build Coastguard Worker    `TestSuiteName_Bar__Test`, which is invalid.
34*481dde66SAndroid Build Coastguard Worker
35*481dde66SAndroid Build Coastguard WorkerSo clearly `TestSuiteName` and `TestName` cannot start or end with `_`
36*481dde66SAndroid Build Coastguard Worker(Actually, `TestSuiteName` can start with `_`—as long as the `_` isn't followed
37*481dde66SAndroid Build Coastguard Workerby an upper-case letter. But that's getting complicated. So for simplicity we
38*481dde66SAndroid Build Coastguard Workerjust say that it cannot start with `_`.).
39*481dde66SAndroid Build Coastguard Worker
40*481dde66SAndroid Build Coastguard WorkerIt may seem fine for `TestSuiteName` and `TestName` to contain `_` in the
41*481dde66SAndroid Build Coastguard Workermiddle. However, consider this:
42*481dde66SAndroid Build Coastguard Worker
43*481dde66SAndroid Build Coastguard Worker```c++
44*481dde66SAndroid Build Coastguard WorkerTEST(Time, Flies_Like_An_Arrow) { ... }
45*481dde66SAndroid Build Coastguard WorkerTEST(Time_Flies, Like_An_Arrow) { ... }
46*481dde66SAndroid Build Coastguard Worker```
47*481dde66SAndroid Build Coastguard Worker
48*481dde66SAndroid Build Coastguard WorkerNow, the two `TEST`s will both generate the same class
49*481dde66SAndroid Build Coastguard Worker(`Time_Flies_Like_An_Arrow_Test`). That's not good.
50*481dde66SAndroid Build Coastguard Worker
51*481dde66SAndroid Build Coastguard WorkerSo for simplicity, we just ask the users to avoid `_` in `TestSuiteName` and
52*481dde66SAndroid Build Coastguard Worker`TestName`. The rule is more constraining than necessary, but it's simple and
53*481dde66SAndroid Build Coastguard Workereasy to remember. It also gives GoogleTest some wiggle room in case its
54*481dde66SAndroid Build Coastguard Workerimplementation needs to change in the future.
55*481dde66SAndroid Build Coastguard Worker
56*481dde66SAndroid Build Coastguard WorkerIf you violate the rule, there may not be immediate consequences, but your test
57*481dde66SAndroid Build Coastguard Workermay (just may) break with a new compiler (or a new version of the compiler you
58*481dde66SAndroid Build Coastguard Workerare using) or with a new version of GoogleTest. Therefore it's best to follow
59*481dde66SAndroid Build Coastguard Workerthe rule.
60*481dde66SAndroid Build Coastguard Worker
61*481dde66SAndroid Build Coastguard Worker## Why does GoogleTest support `EXPECT_EQ(NULL, ptr)` and `ASSERT_EQ(NULL, ptr)` but not `EXPECT_NE(NULL, ptr)` and `ASSERT_NE(NULL, ptr)`?
62*481dde66SAndroid Build Coastguard Worker
63*481dde66SAndroid Build Coastguard WorkerFirst of all, you can use `nullptr` with each of these macros, e.g.
64*481dde66SAndroid Build Coastguard Worker`EXPECT_EQ(ptr, nullptr)`, `EXPECT_NE(ptr, nullptr)`, `ASSERT_EQ(ptr, nullptr)`,
65*481dde66SAndroid Build Coastguard Worker`ASSERT_NE(ptr, nullptr)`. This is the preferred syntax in the style guide
66*481dde66SAndroid Build Coastguard Workerbecause `nullptr` does not have the type problems that `NULL` does.
67*481dde66SAndroid Build Coastguard Worker
68*481dde66SAndroid Build Coastguard WorkerDue to some peculiarity of C++, it requires some non-trivial template meta
69*481dde66SAndroid Build Coastguard Workerprogramming tricks to support using `NULL` as an argument of the `EXPECT_XX()`
70*481dde66SAndroid Build Coastguard Workerand `ASSERT_XX()` macros. Therefore we only do it where it's most needed
71*481dde66SAndroid Build Coastguard Worker(otherwise we make the implementation of GoogleTest harder to maintain and more
72*481dde66SAndroid Build Coastguard Workererror-prone than necessary).
73*481dde66SAndroid Build Coastguard Worker
74*481dde66SAndroid Build Coastguard WorkerHistorically, the `EXPECT_EQ()` macro took the *expected* value as its first
75*481dde66SAndroid Build Coastguard Workerargument and the *actual* value as the second, though this argument order is now
76*481dde66SAndroid Build Coastguard Workerdiscouraged. It was reasonable that someone wanted
77*481dde66SAndroid Build Coastguard Workerto write `EXPECT_EQ(NULL, some_expression)`, and this indeed was requested
78*481dde66SAndroid Build Coastguard Workerseveral times. Therefore we implemented it.
79*481dde66SAndroid Build Coastguard Worker
80*481dde66SAndroid Build Coastguard WorkerThe need for `EXPECT_NE(NULL, ptr)` wasn't nearly as strong. When the assertion
81*481dde66SAndroid Build Coastguard Workerfails, you already know that `ptr` must be `NULL`, so it doesn't add any
82*481dde66SAndroid Build Coastguard Workerinformation to print `ptr` in this case. That means `EXPECT_TRUE(ptr != NULL)`
83*481dde66SAndroid Build Coastguard Workerworks just as well.
84*481dde66SAndroid Build Coastguard Worker
85*481dde66SAndroid Build Coastguard WorkerIf we were to support `EXPECT_NE(NULL, ptr)`, for consistency we'd have to
86*481dde66SAndroid Build Coastguard Workersupport `EXPECT_NE(ptr, NULL)` as well. This means using the template meta
87*481dde66SAndroid Build Coastguard Workerprogramming tricks twice in the implementation, making it even harder to
88*481dde66SAndroid Build Coastguard Workerunderstand and maintain. We believe the benefit doesn't justify the cost.
89*481dde66SAndroid Build Coastguard Worker
90*481dde66SAndroid Build Coastguard WorkerFinally, with the growth of the gMock matcher library, we are encouraging people
91*481dde66SAndroid Build Coastguard Workerto use the unified `EXPECT_THAT(value, matcher)` syntax more often in tests. One
92*481dde66SAndroid Build Coastguard Workersignificant advantage of the matcher approach is that matchers can be easily
93*481dde66SAndroid Build Coastguard Workercombined to form new matchers, while the `EXPECT_NE`, etc, macros cannot be
94*481dde66SAndroid Build Coastguard Workereasily combined. Therefore we want to invest more in the matchers than in the
95*481dde66SAndroid Build Coastguard Worker`EXPECT_XX()` macros.
96*481dde66SAndroid Build Coastguard Worker
97*481dde66SAndroid Build Coastguard Worker## I need to test that different implementations of an interface satisfy some common requirements. Should I use typed tests or value-parameterized tests?
98*481dde66SAndroid Build Coastguard Worker
99*481dde66SAndroid Build Coastguard WorkerFor testing various implementations of the same interface, either typed tests or
100*481dde66SAndroid Build Coastguard Workervalue-parameterized tests can get it done. It's really up to you the user to
101*481dde66SAndroid Build Coastguard Workerdecide which is more convenient for you, depending on your particular case. Some
102*481dde66SAndroid Build Coastguard Workerrough guidelines:
103*481dde66SAndroid Build Coastguard Worker
104*481dde66SAndroid Build Coastguard Worker*   Typed tests can be easier to write if instances of the different
105*481dde66SAndroid Build Coastguard Worker    implementations can be created the same way, modulo the type. For example,
106*481dde66SAndroid Build Coastguard Worker    if all these implementations have a public default constructor (such that
107*481dde66SAndroid Build Coastguard Worker    you can write `new TypeParam`), or if their factory functions have the same
108*481dde66SAndroid Build Coastguard Worker    form (e.g. `CreateInstance<TypeParam>()`).
109*481dde66SAndroid Build Coastguard Worker*   Value-parameterized tests can be easier to write if you need different code
110*481dde66SAndroid Build Coastguard Worker    patterns to create different implementations' instances, e.g. `new Foo` vs
111*481dde66SAndroid Build Coastguard Worker    `new Bar(5)`. To accommodate for the differences, you can write factory
112*481dde66SAndroid Build Coastguard Worker    function wrappers and pass these function pointers to the tests as their
113*481dde66SAndroid Build Coastguard Worker    parameters.
114*481dde66SAndroid Build Coastguard Worker*   When a typed test fails, the default output includes the name of the type,
115*481dde66SAndroid Build Coastguard Worker    which can help you quickly identify which implementation is wrong.
116*481dde66SAndroid Build Coastguard Worker    Value-parameterized tests only show the number of the failed iteration by
117*481dde66SAndroid Build Coastguard Worker    default. You will need to define a function that returns the iteration name
118*481dde66SAndroid Build Coastguard Worker    and pass it as the third parameter to INSTANTIATE_TEST_SUITE_P to have more
119*481dde66SAndroid Build Coastguard Worker    useful output.
120*481dde66SAndroid Build Coastguard Worker*   When using typed tests, you need to make sure you are testing against the
121*481dde66SAndroid Build Coastguard Worker    interface type, not the concrete types (in other words, you want to make
122*481dde66SAndroid Build Coastguard Worker    sure `implicit_cast<MyInterface*>(my_concrete_impl)` works, not just that
123*481dde66SAndroid Build Coastguard Worker    `my_concrete_impl` works). It's less likely to make mistakes in this area
124*481dde66SAndroid Build Coastguard Worker    when using value-parameterized tests.
125*481dde66SAndroid Build Coastguard Worker
126*481dde66SAndroid Build Coastguard WorkerI hope I didn't confuse you more. :-) If you don't mind, I'd suggest you to give
127*481dde66SAndroid Build Coastguard Workerboth approaches a try. Practice is a much better way to grasp the subtle
128*481dde66SAndroid Build Coastguard Workerdifferences between the two tools. Once you have some concrete experience, you
129*481dde66SAndroid Build Coastguard Workercan much more easily decide which one to use the next time.
130*481dde66SAndroid Build Coastguard Worker
131*481dde66SAndroid Build Coastguard Worker## My death test modifies some state, but the change seems lost after the death test finishes. Why?
132*481dde66SAndroid Build Coastguard Worker
133*481dde66SAndroid Build Coastguard WorkerDeath tests (`EXPECT_DEATH`, etc.) are executed in a sub-process s.t. the
134*481dde66SAndroid Build Coastguard Workerexpected crash won't kill the test program (i.e. the parent process). As a
135*481dde66SAndroid Build Coastguard Workerresult, any in-memory side effects they incur are observable in their respective
136*481dde66SAndroid Build Coastguard Workersub-processes, but not in the parent process. You can think of them as running
137*481dde66SAndroid Build Coastguard Workerin a parallel universe, more or less.
138*481dde66SAndroid Build Coastguard Worker
139*481dde66SAndroid Build Coastguard WorkerIn particular, if you use mocking and the death test statement invokes some mock
140*481dde66SAndroid Build Coastguard Workermethods, the parent process will think the calls have never occurred. Therefore,
141*481dde66SAndroid Build Coastguard Workeryou may want to move your `EXPECT_CALL` statements inside the `EXPECT_DEATH`
142*481dde66SAndroid Build Coastguard Workermacro.
143*481dde66SAndroid Build Coastguard Worker
144*481dde66SAndroid Build Coastguard Worker## EXPECT_EQ(htonl(blah), blah_blah) generates weird compiler errors in opt mode. Is this a GoogleTest bug?
145*481dde66SAndroid Build Coastguard Worker
146*481dde66SAndroid Build Coastguard WorkerActually, the bug is in `htonl()`.
147*481dde66SAndroid Build Coastguard Worker
148*481dde66SAndroid Build Coastguard WorkerAccording to `'man htonl'`, `htonl()` is a *function*, which means it's valid to
149*481dde66SAndroid Build Coastguard Workeruse `htonl` as a function pointer. However, in opt mode `htonl()` is defined as
150*481dde66SAndroid Build Coastguard Workera *macro*, which breaks this usage.
151*481dde66SAndroid Build Coastguard Worker
152*481dde66SAndroid Build Coastguard WorkerWorse, the macro definition of `htonl()` uses a `gcc` extension and is *not*
153*481dde66SAndroid Build Coastguard Workerstandard C++. That hacky implementation has some ad hoc limitations. In
154*481dde66SAndroid Build Coastguard Workerparticular, it prevents you from writing `Foo<sizeof(htonl(x))>()`, where `Foo`
155*481dde66SAndroid Build Coastguard Workeris a template that has an integral argument.
156*481dde66SAndroid Build Coastguard Worker
157*481dde66SAndroid Build Coastguard WorkerThe implementation of `EXPECT_EQ(a, b)` uses `sizeof(... a ...)` inside a
158*481dde66SAndroid Build Coastguard Workertemplate argument, and thus doesn't compile in opt mode when `a` contains a call
159*481dde66SAndroid Build Coastguard Workerto `htonl()`. It is difficult to make `EXPECT_EQ` bypass the `htonl()` bug, as
160*481dde66SAndroid Build Coastguard Workerthe solution must work with different compilers on various platforms.
161*481dde66SAndroid Build Coastguard Worker
162*481dde66SAndroid Build Coastguard Worker## The compiler complains about "undefined references" to some static const member variables, but I did define them in the class body. What's wrong?
163*481dde66SAndroid Build Coastguard Worker
164*481dde66SAndroid Build Coastguard WorkerIf your class has a static data member:
165*481dde66SAndroid Build Coastguard Worker
166*481dde66SAndroid Build Coastguard Worker```c++
167*481dde66SAndroid Build Coastguard Worker// foo.h
168*481dde66SAndroid Build Coastguard Workerclass Foo {
169*481dde66SAndroid Build Coastguard Worker  ...
170*481dde66SAndroid Build Coastguard Worker  static const int kBar = 100;
171*481dde66SAndroid Build Coastguard Worker};
172*481dde66SAndroid Build Coastguard Worker```
173*481dde66SAndroid Build Coastguard Worker
174*481dde66SAndroid Build Coastguard Workeryou also need to define it *outside* of the class body in `foo.cc`:
175*481dde66SAndroid Build Coastguard Worker
176*481dde66SAndroid Build Coastguard Worker```c++
177*481dde66SAndroid Build Coastguard Workerconst int Foo::kBar;  // No initializer here.
178*481dde66SAndroid Build Coastguard Worker```
179*481dde66SAndroid Build Coastguard Worker
180*481dde66SAndroid Build Coastguard WorkerOtherwise your code is **invalid C++**, and may break in unexpected ways. In
181*481dde66SAndroid Build Coastguard Workerparticular, using it in GoogleTest comparison assertions (`EXPECT_EQ`, etc.)
182*481dde66SAndroid Build Coastguard Workerwill generate an "undefined reference" linker error. The fact that "it used to
183*481dde66SAndroid Build Coastguard Workerwork" doesn't mean it's valid. It just means that you were lucky. :-)
184*481dde66SAndroid Build Coastguard Worker
185*481dde66SAndroid Build Coastguard WorkerIf the declaration of the static data member is `constexpr` then it is
186*481dde66SAndroid Build Coastguard Workerimplicitly an `inline` definition, and a separate definition in `foo.cc` is not
187*481dde66SAndroid Build Coastguard Workerneeded:
188*481dde66SAndroid Build Coastguard Worker
189*481dde66SAndroid Build Coastguard Worker```c++
190*481dde66SAndroid Build Coastguard Worker// foo.h
191*481dde66SAndroid Build Coastguard Workerclass Foo {
192*481dde66SAndroid Build Coastguard Worker  ...
193*481dde66SAndroid Build Coastguard Worker  static constexpr int kBar = 100;  // Defines kBar, no need to do it in foo.cc.
194*481dde66SAndroid Build Coastguard Worker};
195*481dde66SAndroid Build Coastguard Worker```
196*481dde66SAndroid Build Coastguard Worker
197*481dde66SAndroid Build Coastguard Worker## Can I derive a test fixture from another?
198*481dde66SAndroid Build Coastguard Worker
199*481dde66SAndroid Build Coastguard WorkerYes.
200*481dde66SAndroid Build Coastguard Worker
201*481dde66SAndroid Build Coastguard WorkerEach test fixture has a corresponding and same named test suite. This means only
202*481dde66SAndroid Build Coastguard Workerone test suite can use a particular fixture. Sometimes, however, multiple test
203*481dde66SAndroid Build Coastguard Workercases may want to use the same or slightly different fixtures. For example, you
204*481dde66SAndroid Build Coastguard Workermay want to make sure that all of a GUI library's test suites don't leak
205*481dde66SAndroid Build Coastguard Workerimportant system resources like fonts and brushes.
206*481dde66SAndroid Build Coastguard Worker
207*481dde66SAndroid Build Coastguard WorkerIn GoogleTest, you share a fixture among test suites by putting the shared logic
208*481dde66SAndroid Build Coastguard Workerin a base test fixture, then deriving from that base a separate fixture for each
209*481dde66SAndroid Build Coastguard Workertest suite that wants to use this common logic. You then use `TEST_F()` to write
210*481dde66SAndroid Build Coastguard Workertests using each derived fixture.
211*481dde66SAndroid Build Coastguard Worker
212*481dde66SAndroid Build Coastguard WorkerTypically, your code looks like this:
213*481dde66SAndroid Build Coastguard Worker
214*481dde66SAndroid Build Coastguard Worker```c++
215*481dde66SAndroid Build Coastguard Worker// Defines a base test fixture.
216*481dde66SAndroid Build Coastguard Workerclass BaseTest : public ::testing::Test {
217*481dde66SAndroid Build Coastguard Worker protected:
218*481dde66SAndroid Build Coastguard Worker  ...
219*481dde66SAndroid Build Coastguard Worker};
220*481dde66SAndroid Build Coastguard Worker
221*481dde66SAndroid Build Coastguard Worker// Derives a fixture FooTest from BaseTest.
222*481dde66SAndroid Build Coastguard Workerclass FooTest : public BaseTest {
223*481dde66SAndroid Build Coastguard Worker protected:
224*481dde66SAndroid Build Coastguard Worker  void SetUp() override {
225*481dde66SAndroid Build Coastguard Worker    BaseTest::SetUp();  // Sets up the base fixture first.
226*481dde66SAndroid Build Coastguard Worker    ... additional set-up work ...
227*481dde66SAndroid Build Coastguard Worker  }
228*481dde66SAndroid Build Coastguard Worker
229*481dde66SAndroid Build Coastguard Worker  void TearDown() override {
230*481dde66SAndroid Build Coastguard Worker    ... clean-up work for FooTest ...
231*481dde66SAndroid Build Coastguard Worker    BaseTest::TearDown();  // Remember to tear down the base fixture
232*481dde66SAndroid Build Coastguard Worker                           // after cleaning up FooTest!
233*481dde66SAndroid Build Coastguard Worker  }
234*481dde66SAndroid Build Coastguard Worker
235*481dde66SAndroid Build Coastguard Worker  ... functions and variables for FooTest ...
236*481dde66SAndroid Build Coastguard Worker};
237*481dde66SAndroid Build Coastguard Worker
238*481dde66SAndroid Build Coastguard Worker// Tests that use the fixture FooTest.
239*481dde66SAndroid Build Coastguard WorkerTEST_F(FooTest, Bar) { ... }
240*481dde66SAndroid Build Coastguard WorkerTEST_F(FooTest, Baz) { ... }
241*481dde66SAndroid Build Coastguard Worker
242*481dde66SAndroid Build Coastguard Worker... additional fixtures derived from BaseTest ...
243*481dde66SAndroid Build Coastguard Worker```
244*481dde66SAndroid Build Coastguard Worker
245*481dde66SAndroid Build Coastguard WorkerIf necessary, you can continue to derive test fixtures from a derived fixture.
246*481dde66SAndroid Build Coastguard WorkerGoogleTest has no limit on how deep the hierarchy can be.
247*481dde66SAndroid Build Coastguard Worker
248*481dde66SAndroid Build Coastguard WorkerFor a complete example using derived test fixtures, see
249*481dde66SAndroid Build Coastguard Worker[sample5_unittest.cc](https://github.com/google/googletest/blob/main/googletest/samples/sample5_unittest.cc).
250*481dde66SAndroid Build Coastguard Worker
251*481dde66SAndroid Build Coastguard Worker## My compiler complains "void value not ignored as it ought to be." What does this mean?
252*481dde66SAndroid Build Coastguard Worker
253*481dde66SAndroid Build Coastguard WorkerYou're probably using an `ASSERT_*()` in a function that doesn't return `void`.
254*481dde66SAndroid Build Coastguard Worker`ASSERT_*()` can only be used in `void` functions, due to exceptions being
255*481dde66SAndroid Build Coastguard Workerdisabled by our build system. Please see more details
256*481dde66SAndroid Build Coastguard Worker[here](advanced.md#assertion-placement).
257*481dde66SAndroid Build Coastguard Worker
258*481dde66SAndroid Build Coastguard Worker## My death test hangs (or seg-faults). How do I fix it?
259*481dde66SAndroid Build Coastguard Worker
260*481dde66SAndroid Build Coastguard WorkerIn GoogleTest, death tests are run in a child process and the way they work is
261*481dde66SAndroid Build Coastguard Workerdelicate. To write death tests you really need to understand how they work—see
262*481dde66SAndroid Build Coastguard Workerthe details at [Death Assertions](reference/assertions.md#death) in the
263*481dde66SAndroid Build Coastguard WorkerAssertions Reference.
264*481dde66SAndroid Build Coastguard Worker
265*481dde66SAndroid Build Coastguard WorkerIn particular, death tests don't like having multiple threads in the parent
266*481dde66SAndroid Build Coastguard Workerprocess. So the first thing you can try is to eliminate creating threads outside
267*481dde66SAndroid Build Coastguard Workerof `EXPECT_DEATH()`. For example, you may want to use mocks or fake objects
268*481dde66SAndroid Build Coastguard Workerinstead of real ones in your tests.
269*481dde66SAndroid Build Coastguard Worker
270*481dde66SAndroid Build Coastguard WorkerSometimes this is impossible as some library you must use may be creating
271*481dde66SAndroid Build Coastguard Workerthreads before `main()` is even reached. In this case, you can try to minimize
272*481dde66SAndroid Build Coastguard Workerthe chance of conflicts by either moving as many activities as possible inside
273*481dde66SAndroid Build Coastguard Worker`EXPECT_DEATH()` (in the extreme case, you want to move everything inside), or
274*481dde66SAndroid Build Coastguard Workerleaving as few things as possible in it. Also, you can try to set the death test
275*481dde66SAndroid Build Coastguard Workerstyle to `"threadsafe"`, which is safer but slower, and see if it helps.
276*481dde66SAndroid Build Coastguard Worker
277*481dde66SAndroid Build Coastguard WorkerIf you go with thread-safe death tests, remember that they rerun the test
278*481dde66SAndroid Build Coastguard Workerprogram from the beginning in the child process. Therefore make sure your
279*481dde66SAndroid Build Coastguard Workerprogram can run side-by-side with itself and is deterministic.
280*481dde66SAndroid Build Coastguard Worker
281*481dde66SAndroid Build Coastguard WorkerIn the end, this boils down to good concurrent programming. You have to make
282*481dde66SAndroid Build Coastguard Workersure that there are no race conditions or deadlocks in your program. No silver
283*481dde66SAndroid Build Coastguard Workerbullet - sorry!
284*481dde66SAndroid Build Coastguard Worker
285*481dde66SAndroid Build Coastguard Worker## Should I use the constructor/destructor of the test fixture or SetUp()/TearDown()? {#CtorVsSetUp}
286*481dde66SAndroid Build Coastguard Worker
287*481dde66SAndroid Build Coastguard WorkerThe first thing to remember is that GoogleTest does **not** reuse the same test
288*481dde66SAndroid Build Coastguard Workerfixture object across multiple tests. For each `TEST_F`, GoogleTest will create
289*481dde66SAndroid Build Coastguard Workera **fresh** test fixture object, immediately call `SetUp()`, run the test body,
290*481dde66SAndroid Build Coastguard Workercall `TearDown()`, and then delete the test fixture object.
291*481dde66SAndroid Build Coastguard Worker
292*481dde66SAndroid Build Coastguard WorkerWhen you need to write per-test set-up and tear-down logic, you have the choice
293*481dde66SAndroid Build Coastguard Workerbetween using the test fixture constructor/destructor or `SetUp()`/`TearDown()`.
294*481dde66SAndroid Build Coastguard WorkerThe former is usually preferred, as it has the following benefits:
295*481dde66SAndroid Build Coastguard Worker
296*481dde66SAndroid Build Coastguard Worker*   By initializing a member variable in the constructor, we have the option to
297*481dde66SAndroid Build Coastguard Worker    make it `const`, which helps prevent accidental changes to its value and
298*481dde66SAndroid Build Coastguard Worker    makes the tests more obviously correct.
299*481dde66SAndroid Build Coastguard Worker*   In case we need to subclass the test fixture class, the subclass'
300*481dde66SAndroid Build Coastguard Worker    constructor is guaranteed to call the base class' constructor *first*, and
301*481dde66SAndroid Build Coastguard Worker    the subclass' destructor is guaranteed to call the base class' destructor
302*481dde66SAndroid Build Coastguard Worker    *afterward*. With `SetUp()/TearDown()`, a subclass may make the mistake of
303*481dde66SAndroid Build Coastguard Worker    forgetting to call the base class' `SetUp()/TearDown()` or call them at the
304*481dde66SAndroid Build Coastguard Worker    wrong time.
305*481dde66SAndroid Build Coastguard Worker
306*481dde66SAndroid Build Coastguard WorkerYou may still want to use `SetUp()/TearDown()` in the following cases:
307*481dde66SAndroid Build Coastguard Worker
308*481dde66SAndroid Build Coastguard Worker*   C++ does not allow virtual function calls in constructors and destructors.
309*481dde66SAndroid Build Coastguard Worker    You can call a method declared as virtual, but it will not use dynamic
310*481dde66SAndroid Build Coastguard Worker    dispatch. It will use the definition from the class the constructor of which
311*481dde66SAndroid Build Coastguard Worker    is currently executing. This is because calling a virtual method before the
312*481dde66SAndroid Build Coastguard Worker    derived class constructor has a chance to run is very dangerous - the
313*481dde66SAndroid Build Coastguard Worker    virtual method might operate on uninitialized data. Therefore, if you need
314*481dde66SAndroid Build Coastguard Worker    to call a method that will be overridden in a derived class, you have to use
315*481dde66SAndroid Build Coastguard Worker    `SetUp()/TearDown()`.
316*481dde66SAndroid Build Coastguard Worker*   In the body of a constructor (or destructor), it's not possible to use the
317*481dde66SAndroid Build Coastguard Worker    `ASSERT_xx` macros. Therefore, if the set-up operation could cause a fatal
318*481dde66SAndroid Build Coastguard Worker    test failure that should prevent the test from running, it's necessary to
319*481dde66SAndroid Build Coastguard Worker    use `abort` and abort the whole test
320*481dde66SAndroid Build Coastguard Worker    executable, or to use `SetUp()` instead of a constructor.
321*481dde66SAndroid Build Coastguard Worker*   If the tear-down operation could throw an exception, you must use
322*481dde66SAndroid Build Coastguard Worker    `TearDown()` as opposed to the destructor, as throwing in a destructor leads
323*481dde66SAndroid Build Coastguard Worker    to undefined behavior and usually will kill your program right away. Note
324*481dde66SAndroid Build Coastguard Worker    that many standard libraries (like STL) may throw when exceptions are
325*481dde66SAndroid Build Coastguard Worker    enabled in the compiler. Therefore you should prefer `TearDown()` if you
326*481dde66SAndroid Build Coastguard Worker    want to write portable tests that work with or without exceptions.
327*481dde66SAndroid Build Coastguard Worker*   The GoogleTest team is considering making the assertion macros throw on
328*481dde66SAndroid Build Coastguard Worker    platforms where exceptions are enabled (e.g. Windows, Mac OS, and Linux
329*481dde66SAndroid Build Coastguard Worker    client-side), which will eliminate the need for the user to propagate
330*481dde66SAndroid Build Coastguard Worker    failures from a subroutine to its caller. Therefore, you shouldn't use
331*481dde66SAndroid Build Coastguard Worker    GoogleTest assertions in a destructor if your code could run on such a
332*481dde66SAndroid Build Coastguard Worker    platform.
333*481dde66SAndroid Build Coastguard Worker
334*481dde66SAndroid Build Coastguard Worker## The compiler complains "no matching function to call" when I use `ASSERT_PRED*`. How do I fix it?
335*481dde66SAndroid Build Coastguard Worker
336*481dde66SAndroid Build Coastguard WorkerSee details for [`EXPECT_PRED*`](reference/assertions.md#EXPECT_PRED) in the
337*481dde66SAndroid Build Coastguard WorkerAssertions Reference.
338*481dde66SAndroid Build Coastguard Worker
339*481dde66SAndroid Build Coastguard Worker## My compiler complains about "ignoring return value" when I call RUN_ALL_TESTS(). Why?
340*481dde66SAndroid Build Coastguard Worker
341*481dde66SAndroid Build Coastguard WorkerSome people had been ignoring the return value of `RUN_ALL_TESTS()`. That is,
342*481dde66SAndroid Build Coastguard Workerinstead of
343*481dde66SAndroid Build Coastguard Worker
344*481dde66SAndroid Build Coastguard Worker```c++
345*481dde66SAndroid Build Coastguard Worker  return RUN_ALL_TESTS();
346*481dde66SAndroid Build Coastguard Worker```
347*481dde66SAndroid Build Coastguard Worker
348*481dde66SAndroid Build Coastguard Workerthey write
349*481dde66SAndroid Build Coastguard Worker
350*481dde66SAndroid Build Coastguard Worker```c++
351*481dde66SAndroid Build Coastguard Worker  RUN_ALL_TESTS();
352*481dde66SAndroid Build Coastguard Worker```
353*481dde66SAndroid Build Coastguard Worker
354*481dde66SAndroid Build Coastguard WorkerThis is **wrong and dangerous**. The testing services needs to see the return
355*481dde66SAndroid Build Coastguard Workervalue of `RUN_ALL_TESTS()` in order to determine if a test has passed. If your
356*481dde66SAndroid Build Coastguard Worker`main()` function ignores it, your test will be considered successful even if it
357*481dde66SAndroid Build Coastguard Workerhas a GoogleTest assertion failure. Very bad.
358*481dde66SAndroid Build Coastguard Worker
359*481dde66SAndroid Build Coastguard WorkerWe have decided to fix this (thanks to Michael Chastain for the idea). Now, your
360*481dde66SAndroid Build Coastguard Workercode will no longer be able to ignore `RUN_ALL_TESTS()` when compiled with
361*481dde66SAndroid Build Coastguard Worker`gcc`. If you do so, you'll get a compiler error.
362*481dde66SAndroid Build Coastguard Worker
363*481dde66SAndroid Build Coastguard WorkerIf you see the compiler complaining about you ignoring the return value of
364*481dde66SAndroid Build Coastguard Worker`RUN_ALL_TESTS()`, the fix is simple: just make sure its value is used as the
365*481dde66SAndroid Build Coastguard Workerreturn value of `main()`.
366*481dde66SAndroid Build Coastguard Worker
367*481dde66SAndroid Build Coastguard WorkerBut how could we introduce a change that breaks existing tests? Well, in this
368*481dde66SAndroid Build Coastguard Workercase, the code was already broken in the first place, so we didn't break it. :-)
369*481dde66SAndroid Build Coastguard Worker
370*481dde66SAndroid Build Coastguard Worker## My compiler complains that a constructor (or destructor) cannot return a value. What's going on?
371*481dde66SAndroid Build Coastguard Worker
372*481dde66SAndroid Build Coastguard WorkerDue to a peculiarity of C++, in order to support the syntax for streaming
373*481dde66SAndroid Build Coastguard Workermessages to an `ASSERT_*`, e.g.
374*481dde66SAndroid Build Coastguard Worker
375*481dde66SAndroid Build Coastguard Worker```c++
376*481dde66SAndroid Build Coastguard Worker  ASSERT_EQ(1, Foo()) << "blah blah" << foo;
377*481dde66SAndroid Build Coastguard Worker```
378*481dde66SAndroid Build Coastguard Worker
379*481dde66SAndroid Build Coastguard Workerwe had to give up using `ASSERT*` and `FAIL*` (but not `EXPECT*` and
380*481dde66SAndroid Build Coastguard Worker`ADD_FAILURE*`) in constructors and destructors. The workaround is to move the
381*481dde66SAndroid Build Coastguard Workercontent of your constructor/destructor to a private void member function, or
382*481dde66SAndroid Build Coastguard Workerswitch to `EXPECT_*()` if that works. This
383*481dde66SAndroid Build Coastguard Worker[section](advanced.md#assertion-placement) in the user's guide explains it.
384*481dde66SAndroid Build Coastguard Worker
385*481dde66SAndroid Build Coastguard Worker## My SetUp() function is not called. Why?
386*481dde66SAndroid Build Coastguard Worker
387*481dde66SAndroid Build Coastguard WorkerC++ is case-sensitive. Did you spell it as `Setup()`?
388*481dde66SAndroid Build Coastguard Worker
389*481dde66SAndroid Build Coastguard WorkerSimilarly, sometimes people spell `SetUpTestSuite()` as `SetupTestSuite()` and
390*481dde66SAndroid Build Coastguard Workerwonder why it's never called.
391*481dde66SAndroid Build Coastguard Worker
392*481dde66SAndroid Build Coastguard Worker## I have several test suites which share the same test fixture logic; do I have to define a new test fixture class for each of them? This seems pretty tedious.
393*481dde66SAndroid Build Coastguard Worker
394*481dde66SAndroid Build Coastguard WorkerYou don't have to. Instead of
395*481dde66SAndroid Build Coastguard Worker
396*481dde66SAndroid Build Coastguard Worker```c++
397*481dde66SAndroid Build Coastguard Workerclass FooTest : public BaseTest {};
398*481dde66SAndroid Build Coastguard Worker
399*481dde66SAndroid Build Coastguard WorkerTEST_F(FooTest, Abc) { ... }
400*481dde66SAndroid Build Coastguard WorkerTEST_F(FooTest, Def) { ... }
401*481dde66SAndroid Build Coastguard Worker
402*481dde66SAndroid Build Coastguard Workerclass BarTest : public BaseTest {};
403*481dde66SAndroid Build Coastguard Worker
404*481dde66SAndroid Build Coastguard WorkerTEST_F(BarTest, Abc) { ... }
405*481dde66SAndroid Build Coastguard WorkerTEST_F(BarTest, Def) { ... }
406*481dde66SAndroid Build Coastguard Worker```
407*481dde66SAndroid Build Coastguard Worker
408*481dde66SAndroid Build Coastguard Workeryou can simply `typedef` the test fixtures:
409*481dde66SAndroid Build Coastguard Worker
410*481dde66SAndroid Build Coastguard Worker```c++
411*481dde66SAndroid Build Coastguard Workertypedef BaseTest FooTest;
412*481dde66SAndroid Build Coastguard Worker
413*481dde66SAndroid Build Coastguard WorkerTEST_F(FooTest, Abc) { ... }
414*481dde66SAndroid Build Coastguard WorkerTEST_F(FooTest, Def) { ... }
415*481dde66SAndroid Build Coastguard Worker
416*481dde66SAndroid Build Coastguard Workertypedef BaseTest BarTest;
417*481dde66SAndroid Build Coastguard Worker
418*481dde66SAndroid Build Coastguard WorkerTEST_F(BarTest, Abc) { ... }
419*481dde66SAndroid Build Coastguard WorkerTEST_F(BarTest, Def) { ... }
420*481dde66SAndroid Build Coastguard Worker```
421*481dde66SAndroid Build Coastguard Worker
422*481dde66SAndroid Build Coastguard Worker## GoogleTest output is buried in a whole bunch of LOG messages. What do I do?
423*481dde66SAndroid Build Coastguard Worker
424*481dde66SAndroid Build Coastguard WorkerThe GoogleTest output is meant to be a concise and human-friendly report. If
425*481dde66SAndroid Build Coastguard Workeryour test generates textual output itself, it will mix with the GoogleTest
426*481dde66SAndroid Build Coastguard Workeroutput, making it hard to read. However, there is an easy solution to this
427*481dde66SAndroid Build Coastguard Workerproblem.
428*481dde66SAndroid Build Coastguard Worker
429*481dde66SAndroid Build Coastguard WorkerSince `LOG` messages go to stderr, we decided to let GoogleTest output go to
430*481dde66SAndroid Build Coastguard Workerstdout. This way, you can easily separate the two using redirection. For
431*481dde66SAndroid Build Coastguard Workerexample:
432*481dde66SAndroid Build Coastguard Worker
433*481dde66SAndroid Build Coastguard Worker```shell
434*481dde66SAndroid Build Coastguard Worker$ ./my_test > gtest_output.txt
435*481dde66SAndroid Build Coastguard Worker```
436*481dde66SAndroid Build Coastguard Worker
437*481dde66SAndroid Build Coastguard Worker## Why should I prefer test fixtures over global variables?
438*481dde66SAndroid Build Coastguard Worker
439*481dde66SAndroid Build Coastguard WorkerThere are several good reasons:
440*481dde66SAndroid Build Coastguard Worker
441*481dde66SAndroid Build Coastguard Worker1.  It's likely your test needs to change the states of its global variables.
442*481dde66SAndroid Build Coastguard Worker    This makes it difficult to keep side effects from escaping one test and
443*481dde66SAndroid Build Coastguard Worker    contaminating others, making debugging difficult. By using fixtures, each
444*481dde66SAndroid Build Coastguard Worker    test has a fresh set of variables that's different (but with the same
445*481dde66SAndroid Build Coastguard Worker    names). Thus, tests are kept independent of each other.
446*481dde66SAndroid Build Coastguard Worker2.  Global variables pollute the global namespace.
447*481dde66SAndroid Build Coastguard Worker3.  Test fixtures can be reused via subclassing, which cannot be done easily
448*481dde66SAndroid Build Coastguard Worker    with global variables. This is useful if many test suites have something in
449*481dde66SAndroid Build Coastguard Worker    common.
450*481dde66SAndroid Build Coastguard Worker
451*481dde66SAndroid Build Coastguard Worker## What can the statement argument in ASSERT_DEATH() be?
452*481dde66SAndroid Build Coastguard Worker
453*481dde66SAndroid Build Coastguard Worker`ASSERT_DEATH(statement, matcher)` (or any death assertion macro) can be used
454*481dde66SAndroid Build Coastguard Workerwherever *`statement`* is valid. So basically *`statement`* can be any C++
455*481dde66SAndroid Build Coastguard Workerstatement that makes sense in the current context. In particular, it can
456*481dde66SAndroid Build Coastguard Workerreference global and/or local variables, and can be:
457*481dde66SAndroid Build Coastguard Worker
458*481dde66SAndroid Build Coastguard Worker*   a simple function call (often the case),
459*481dde66SAndroid Build Coastguard Worker*   a complex expression, or
460*481dde66SAndroid Build Coastguard Worker*   a compound statement.
461*481dde66SAndroid Build Coastguard Worker
462*481dde66SAndroid Build Coastguard WorkerSome examples are shown here:
463*481dde66SAndroid Build Coastguard Worker
464*481dde66SAndroid Build Coastguard Worker```c++
465*481dde66SAndroid Build Coastguard Worker// A death test can be a simple function call.
466*481dde66SAndroid Build Coastguard WorkerTEST(MyDeathTest, FunctionCall) {
467*481dde66SAndroid Build Coastguard Worker  ASSERT_DEATH(Xyz(5), "Xyz failed");
468*481dde66SAndroid Build Coastguard Worker}
469*481dde66SAndroid Build Coastguard Worker
470*481dde66SAndroid Build Coastguard Worker// Or a complex expression that references variables and functions.
471*481dde66SAndroid Build Coastguard WorkerTEST(MyDeathTest, ComplexExpression) {
472*481dde66SAndroid Build Coastguard Worker  const bool c = Condition();
473*481dde66SAndroid Build Coastguard Worker  ASSERT_DEATH((c ? Func1(0) : object2.Method("test")),
474*481dde66SAndroid Build Coastguard Worker               "(Func1|Method) failed");
475*481dde66SAndroid Build Coastguard Worker}
476*481dde66SAndroid Build Coastguard Worker
477*481dde66SAndroid Build Coastguard Worker// Death assertions can be used anywhere in a function.  In
478*481dde66SAndroid Build Coastguard Worker// particular, they can be inside a loop.
479*481dde66SAndroid Build Coastguard WorkerTEST(MyDeathTest, InsideLoop) {
480*481dde66SAndroid Build Coastguard Worker  // Verifies that Foo(0), Foo(1), ..., and Foo(4) all die.
481*481dde66SAndroid Build Coastguard Worker  for (int i = 0; i < 5; i++) {
482*481dde66SAndroid Build Coastguard Worker    EXPECT_DEATH_M(Foo(i), "Foo has \\d+ errors",
483*481dde66SAndroid Build Coastguard Worker                   ::testing::Message() << "where i is " << i);
484*481dde66SAndroid Build Coastguard Worker  }
485*481dde66SAndroid Build Coastguard Worker}
486*481dde66SAndroid Build Coastguard Worker
487*481dde66SAndroid Build Coastguard Worker// A death assertion can contain a compound statement.
488*481dde66SAndroid Build Coastguard WorkerTEST(MyDeathTest, CompoundStatement) {
489*481dde66SAndroid Build Coastguard Worker  // Verifies that at lease one of Bar(0), Bar(1), ..., and
490*481dde66SAndroid Build Coastguard Worker  // Bar(4) dies.
491*481dde66SAndroid Build Coastguard Worker  ASSERT_DEATH({
492*481dde66SAndroid Build Coastguard Worker    for (int i = 0; i < 5; i++) {
493*481dde66SAndroid Build Coastguard Worker      Bar(i);
494*481dde66SAndroid Build Coastguard Worker    }
495*481dde66SAndroid Build Coastguard Worker  },
496*481dde66SAndroid Build Coastguard Worker  "Bar has \\d+ errors");
497*481dde66SAndroid Build Coastguard Worker}
498*481dde66SAndroid Build Coastguard Worker```
499*481dde66SAndroid Build Coastguard Worker
500*481dde66SAndroid Build Coastguard Worker## I have a fixture class `FooTest`, but `TEST_F(FooTest, Bar)` gives me error ``"no matching function for call to `FooTest::FooTest()'"``. Why?
501*481dde66SAndroid Build Coastguard Worker
502*481dde66SAndroid Build Coastguard WorkerGoogleTest needs to be able to create objects of your test fixture class, so it
503*481dde66SAndroid Build Coastguard Workermust have a default constructor. Normally the compiler will define one for you.
504*481dde66SAndroid Build Coastguard WorkerHowever, there are cases where you have to define your own:
505*481dde66SAndroid Build Coastguard Worker
506*481dde66SAndroid Build Coastguard Worker*   If you explicitly declare a non-default constructor for class `FooTest`
507*481dde66SAndroid Build Coastguard Worker    (`DISALLOW_EVIL_CONSTRUCTORS()` does this), then you need to define a
508*481dde66SAndroid Build Coastguard Worker    default constructor, even if it would be empty.
509*481dde66SAndroid Build Coastguard Worker*   If `FooTest` has a const non-static data member, then you have to define the
510*481dde66SAndroid Build Coastguard Worker    default constructor *and* initialize the const member in the initializer
511*481dde66SAndroid Build Coastguard Worker    list of the constructor. (Early versions of `gcc` doesn't force you to
512*481dde66SAndroid Build Coastguard Worker    initialize the const member. It's a bug that has been fixed in `gcc 4`.)
513*481dde66SAndroid Build Coastguard Worker
514*481dde66SAndroid Build Coastguard Worker## Why does ASSERT_DEATH complain about previous threads that were already joined?
515*481dde66SAndroid Build Coastguard Worker
516*481dde66SAndroid Build Coastguard WorkerWith the Linux pthread library, there is no turning back once you cross the line
517*481dde66SAndroid Build Coastguard Workerfrom a single thread to multiple threads. The first time you create a thread, a
518*481dde66SAndroid Build Coastguard Workermanager thread is created in addition, so you get 3, not 2, threads. Later when
519*481dde66SAndroid Build Coastguard Workerthe thread you create joins the main thread, the thread count decrements by 1,
520*481dde66SAndroid Build Coastguard Workerbut the manager thread will never be killed, so you still have 2 threads, which
521*481dde66SAndroid Build Coastguard Workermeans you cannot safely run a death test.
522*481dde66SAndroid Build Coastguard Worker
523*481dde66SAndroid Build Coastguard WorkerThe new NPTL thread library doesn't suffer from this problem, as it doesn't
524*481dde66SAndroid Build Coastguard Workercreate a manager thread. However, if you don't control which machine your test
525*481dde66SAndroid Build Coastguard Workerruns on, you shouldn't depend on this.
526*481dde66SAndroid Build Coastguard Worker
527*481dde66SAndroid Build Coastguard Worker## Why does GoogleTest require the entire test suite, instead of individual tests, to be named `*DeathTest` when it uses `ASSERT_DEATH`?
528*481dde66SAndroid Build Coastguard Worker
529*481dde66SAndroid Build Coastguard WorkerGoogleTest does not interleave tests from different test suites. That is, it
530*481dde66SAndroid Build Coastguard Workerruns all tests in one test suite first, and then runs all tests in the next test
531*481dde66SAndroid Build Coastguard Workersuite, and so on. GoogleTest does this because it needs to set up a test suite
532*481dde66SAndroid Build Coastguard Workerbefore the first test in it is run, and tear it down afterwards. Splitting up
533*481dde66SAndroid Build Coastguard Workerthe test case would require multiple set-up and tear-down processes, which is
534*481dde66SAndroid Build Coastguard Workerinefficient and makes the semantics unclean.
535*481dde66SAndroid Build Coastguard Worker
536*481dde66SAndroid Build Coastguard WorkerIf we were to determine the order of tests based on test name instead of test
537*481dde66SAndroid Build Coastguard Workercase name, then we would have a problem with the following situation:
538*481dde66SAndroid Build Coastguard Worker
539*481dde66SAndroid Build Coastguard Worker```c++
540*481dde66SAndroid Build Coastguard WorkerTEST_F(FooTest, AbcDeathTest) { ... }
541*481dde66SAndroid Build Coastguard WorkerTEST_F(FooTest, Uvw) { ... }
542*481dde66SAndroid Build Coastguard Worker
543*481dde66SAndroid Build Coastguard WorkerTEST_F(BarTest, DefDeathTest) { ... }
544*481dde66SAndroid Build Coastguard WorkerTEST_F(BarTest, Xyz) { ... }
545*481dde66SAndroid Build Coastguard Worker```
546*481dde66SAndroid Build Coastguard Worker
547*481dde66SAndroid Build Coastguard WorkerSince `FooTest.AbcDeathTest` needs to run before `BarTest.Xyz`, and we don't
548*481dde66SAndroid Build Coastguard Workerinterleave tests from different test suites, we need to run all tests in the
549*481dde66SAndroid Build Coastguard Worker`FooTest` case before running any test in the `BarTest` case. This contradicts
550*481dde66SAndroid Build Coastguard Workerwith the requirement to run `BarTest.DefDeathTest` before `FooTest.Uvw`.
551*481dde66SAndroid Build Coastguard Worker
552*481dde66SAndroid Build Coastguard Worker## But I don't like calling my entire test suite `*DeathTest` when it contains both death tests and non-death tests. What do I do?
553*481dde66SAndroid Build Coastguard Worker
554*481dde66SAndroid Build Coastguard WorkerYou don't have to, but if you like, you may split up the test suite into
555*481dde66SAndroid Build Coastguard Worker`FooTest` and `FooDeathTest`, where the names make it clear that they are
556*481dde66SAndroid Build Coastguard Workerrelated:
557*481dde66SAndroid Build Coastguard Worker
558*481dde66SAndroid Build Coastguard Worker```c++
559*481dde66SAndroid Build Coastguard Workerclass FooTest : public ::testing::Test { ... };
560*481dde66SAndroid Build Coastguard Worker
561*481dde66SAndroid Build Coastguard WorkerTEST_F(FooTest, Abc) { ... }
562*481dde66SAndroid Build Coastguard WorkerTEST_F(FooTest, Def) { ... }
563*481dde66SAndroid Build Coastguard Worker
564*481dde66SAndroid Build Coastguard Workerusing FooDeathTest = FooTest;
565*481dde66SAndroid Build Coastguard Worker
566*481dde66SAndroid Build Coastguard WorkerTEST_F(FooDeathTest, Uvw) { ... EXPECT_DEATH(...) ... }
567*481dde66SAndroid Build Coastguard WorkerTEST_F(FooDeathTest, Xyz) { ... ASSERT_DEATH(...) ... }
568*481dde66SAndroid Build Coastguard Worker```
569*481dde66SAndroid Build Coastguard Worker
570*481dde66SAndroid Build Coastguard Worker## GoogleTest prints the LOG messages in a death test's child process only when the test fails. How can I see the LOG messages when the death test succeeds?
571*481dde66SAndroid Build Coastguard Worker
572*481dde66SAndroid Build Coastguard WorkerPrinting the LOG messages generated by the statement inside `EXPECT_DEATH()`
573*481dde66SAndroid Build Coastguard Workermakes it harder to search for real problems in the parent's log. Therefore,
574*481dde66SAndroid Build Coastguard WorkerGoogleTest only prints them when the death test has failed.
575*481dde66SAndroid Build Coastguard Worker
576*481dde66SAndroid Build Coastguard WorkerIf you really need to see such LOG messages, a workaround is to temporarily
577*481dde66SAndroid Build Coastguard Workerbreak the death test (e.g. by changing the regex pattern it is expected to
578*481dde66SAndroid Build Coastguard Workermatch). Admittedly, this is a hack. We'll consider a more permanent solution
579*481dde66SAndroid Build Coastguard Workerafter the fork-and-exec-style death tests are implemented.
580*481dde66SAndroid Build Coastguard Worker
581*481dde66SAndroid Build Coastguard Worker## The compiler complains about `no match for 'operator<<'` when I use an assertion. What gives?
582*481dde66SAndroid Build Coastguard Worker
583*481dde66SAndroid Build Coastguard WorkerIf you use a user-defined type `FooType` in an assertion, you must make sure
584*481dde66SAndroid Build Coastguard Workerthere is an `std::ostream& operator<<(std::ostream&, const FooType&)` function
585*481dde66SAndroid Build Coastguard Workerdefined such that we can print a value of `FooType`.
586*481dde66SAndroid Build Coastguard Worker
587*481dde66SAndroid Build Coastguard WorkerIn addition, if `FooType` is declared in a name space, the `<<` operator also
588*481dde66SAndroid Build Coastguard Workerneeds to be defined in the *same* name space. See
589*481dde66SAndroid Build Coastguard Worker[Tip of the Week #49](https://abseil.io/tips/49) for details.
590*481dde66SAndroid Build Coastguard Worker
591*481dde66SAndroid Build Coastguard Worker## How do I suppress the memory leak messages on Windows?
592*481dde66SAndroid Build Coastguard Worker
593*481dde66SAndroid Build Coastguard WorkerSince the statically initialized GoogleTest singleton requires allocations on
594*481dde66SAndroid Build Coastguard Workerthe heap, the Visual C++ memory leak detector will report memory leaks at the
595*481dde66SAndroid Build Coastguard Workerend of the program run. The easiest way to avoid this is to use the
596*481dde66SAndroid Build Coastguard Worker`_CrtMemCheckpoint` and `_CrtMemDumpAllObjectsSince` calls to not report any
597*481dde66SAndroid Build Coastguard Workerstatically initialized heap objects. See MSDN for more details and additional
598*481dde66SAndroid Build Coastguard Workerheap check/debug routines.
599*481dde66SAndroid Build Coastguard Worker
600*481dde66SAndroid Build Coastguard Worker## How can my code detect if it is running in a test?
601*481dde66SAndroid Build Coastguard Worker
602*481dde66SAndroid Build Coastguard WorkerIf you write code that sniffs whether it's running in a test and does different
603*481dde66SAndroid Build Coastguard Workerthings accordingly, you are leaking test-only logic into production code and
604*481dde66SAndroid Build Coastguard Workerthere is no easy way to ensure that the test-only code paths aren't run by
605*481dde66SAndroid Build Coastguard Workermistake in production. Such cleverness also leads to
606*481dde66SAndroid Build Coastguard Worker[Heisenbugs](https://en.wikipedia.org/wiki/Heisenbug). Therefore we strongly
607*481dde66SAndroid Build Coastguard Workeradvise against the practice, and GoogleTest doesn't provide a way to do it.
608*481dde66SAndroid Build Coastguard Worker
609*481dde66SAndroid Build Coastguard WorkerIn general, the recommended way to cause the code to behave differently under
610*481dde66SAndroid Build Coastguard Workertest is [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection). You can inject
611*481dde66SAndroid Build Coastguard Workerdifferent functionality from the test and from the production code. Since your
612*481dde66SAndroid Build Coastguard Workerproduction code doesn't link in the for-test logic at all (the
613*481dde66SAndroid Build Coastguard Worker[`testonly`](https://docs.bazel.build/versions/master/be/common-definitions.html#common.testonly) attribute for BUILD targets helps to ensure
614*481dde66SAndroid Build Coastguard Workerthat), there is no danger in accidentally running it.
615*481dde66SAndroid Build Coastguard Worker
616*481dde66SAndroid Build Coastguard WorkerHowever, if you *really*, *really*, *really* have no choice, and if you follow
617*481dde66SAndroid Build Coastguard Workerthe rule of ending your test program names with `_test`, you can use the
618*481dde66SAndroid Build Coastguard Worker*horrible* hack of sniffing your executable name (`argv[0]` in `main()`) to know
619*481dde66SAndroid Build Coastguard Workerwhether the code is under test.
620*481dde66SAndroid Build Coastguard Worker
621*481dde66SAndroid Build Coastguard Worker## How do I temporarily disable a test?
622*481dde66SAndroid Build Coastguard Worker
623*481dde66SAndroid Build Coastguard WorkerIf you have a broken test that you cannot fix right away, you can add the
624*481dde66SAndroid Build Coastguard Worker`DISABLED_` prefix to its name. This will exclude it from execution. This is
625*481dde66SAndroid Build Coastguard Workerbetter than commenting out the code or using `#if 0`, as disabled tests are
626*481dde66SAndroid Build Coastguard Workerstill compiled (and thus won't rot).
627*481dde66SAndroid Build Coastguard Worker
628*481dde66SAndroid Build Coastguard WorkerTo include disabled tests in test execution, just invoke the test program with
629*481dde66SAndroid Build Coastguard Workerthe `--gtest_also_run_disabled_tests` flag.
630*481dde66SAndroid Build Coastguard Worker
631*481dde66SAndroid Build Coastguard Worker## Is it OK if I have two separate `TEST(Foo, Bar)` test methods defined in different namespaces?
632*481dde66SAndroid Build Coastguard Worker
633*481dde66SAndroid Build Coastguard WorkerYes.
634*481dde66SAndroid Build Coastguard Worker
635*481dde66SAndroid Build Coastguard WorkerThe rule is **all test methods in the same test suite must use the same fixture
636*481dde66SAndroid Build Coastguard Workerclass**. This means that the following is **allowed** because both tests use the
637*481dde66SAndroid Build Coastguard Workersame fixture class (`::testing::Test`).
638*481dde66SAndroid Build Coastguard Worker
639*481dde66SAndroid Build Coastguard Worker```c++
640*481dde66SAndroid Build Coastguard Workernamespace foo {
641*481dde66SAndroid Build Coastguard WorkerTEST(CoolTest, DoSomething) {
642*481dde66SAndroid Build Coastguard Worker  SUCCEED();
643*481dde66SAndroid Build Coastguard Worker}
644*481dde66SAndroid Build Coastguard Worker}  // namespace foo
645*481dde66SAndroid Build Coastguard Worker
646*481dde66SAndroid Build Coastguard Workernamespace bar {
647*481dde66SAndroid Build Coastguard WorkerTEST(CoolTest, DoSomething) {
648*481dde66SAndroid Build Coastguard Worker  SUCCEED();
649*481dde66SAndroid Build Coastguard Worker}
650*481dde66SAndroid Build Coastguard Worker}  // namespace bar
651*481dde66SAndroid Build Coastguard Worker```
652*481dde66SAndroid Build Coastguard Worker
653*481dde66SAndroid Build Coastguard WorkerHowever, the following code is **not allowed** and will produce a runtime error
654*481dde66SAndroid Build Coastguard Workerfrom GoogleTest because the test methods are using different test fixture
655*481dde66SAndroid Build Coastguard Workerclasses with the same test suite name.
656*481dde66SAndroid Build Coastguard Worker
657*481dde66SAndroid Build Coastguard Worker```c++
658*481dde66SAndroid Build Coastguard Workernamespace foo {
659*481dde66SAndroid Build Coastguard Workerclass CoolTest : public ::testing::Test {};  // Fixture foo::CoolTest
660*481dde66SAndroid Build Coastguard WorkerTEST_F(CoolTest, DoSomething) {
661*481dde66SAndroid Build Coastguard Worker  SUCCEED();
662*481dde66SAndroid Build Coastguard Worker}
663*481dde66SAndroid Build Coastguard Worker}  // namespace foo
664*481dde66SAndroid Build Coastguard Worker
665*481dde66SAndroid Build Coastguard Workernamespace bar {
666*481dde66SAndroid Build Coastguard Workerclass CoolTest : public ::testing::Test {};  // Fixture: bar::CoolTest
667*481dde66SAndroid Build Coastguard WorkerTEST_F(CoolTest, DoSomething) {
668*481dde66SAndroid Build Coastguard Worker  SUCCEED();
669*481dde66SAndroid Build Coastguard Worker}
670*481dde66SAndroid Build Coastguard Worker}  // namespace bar
671*481dde66SAndroid Build Coastguard Worker```
672