faq.md 31.3 KB
Newer Older
Gennadiy Civil's avatar
Gennadiy Civil committed
1
# Googletest FAQ
2

3
<!-- GOOGLETEST_CM0014 DO NOT DELETE -->
4

Abseil Team's avatar
Abseil Team committed
5
6
<!-- GOOGLETEST_CM0035 DO NOT DELETE -->

7
## Why should test suite names and test names not contain underscore?
8

Abseil Team's avatar
Abseil Team committed
9
10
11
12
Note: Googletest reserves underscore (`_`) for special purpose keywords, such as
[the `DISABLED_` prefix](advanced.md#temporarily-disabling-tests), in addition
to the following rationale.

Gennadiy Civil's avatar
Gennadiy Civil committed
13
14
Underscore (`_`) is special, as C++ reserves the following to be used by the
compiler and the standard library:
15

Gennadiy Civil's avatar
Gennadiy Civil committed
16
1.  any identifier that starts with an `_` followed by an upper-case letter, and
17
2.  any identifier that contains two consecutive underscores (i.e. `__`)
Gennadiy Civil's avatar
Gennadiy Civil committed
18
    *anywhere* in its name.
19

Gennadiy Civil's avatar
Gennadiy Civil committed
20
User code is *prohibited* from using such identifiers.
21
22
23

Now let's look at what this means for `TEST` and `TEST_F`.

24
25
Currently `TEST(TestSuiteName, TestName)` generates a class named
`TestSuiteName_TestName_Test`. What happens if `TestSuiteName` or `TestName`
26
27
contains `_`?

28
1.  If `TestSuiteName` starts with an `_` followed by an upper-case letter (say,
Gennadiy Civil's avatar
Gennadiy Civil committed
29
30
    `_Foo`), we end up with `_Foo_TestName_Test`, which is reserved and thus
    invalid.
31
2.  If `TestSuiteName` ends with an `_` (say, `Foo_`), we get
Gennadiy Civil's avatar
Gennadiy Civil committed
32
    `Foo__TestName_Test`, which is invalid.
33
3.  If `TestName` starts with an `_` (say, `_Bar`), we get
34
    `TestSuiteName__Bar_Test`, which is invalid.
35
4.  If `TestName` ends with an `_` (say, `Bar_`), we get
36
    `TestSuiteName_Bar__Test`, which is invalid.
Gennadiy Civil's avatar
Gennadiy Civil committed
37

38
39
40
41
So clearly `TestSuiteName` and `TestName` cannot start or end with `_`
(Actually, `TestSuiteName` can start with `_` -- as long as the `_` isn't
followed by an upper-case letter. But that's getting complicated. So for
simplicity we just say that it cannot start with `_`.).
42

43
44
It may seem fine for `TestSuiteName` and `TestName` to contain `_` in the
middle. However, consider this:
45

Gennadiy Civil's avatar
Gennadiy Civil committed
46
```c++
47
48
49
50
51
TEST(Time, Flies_Like_An_Arrow) { ... }
TEST(Time_Flies, Like_An_Arrow) { ... }
```

Now, the two `TEST`s will both generate the same class
Gennadiy Civil's avatar
Gennadiy Civil committed
52
53
(`Time_Flies_Like_An_Arrow_Test`). That's not good.

54
So for simplicity, we just ask the users to avoid `_` in `TestSuiteName` and
Gennadiy Civil's avatar
Gennadiy Civil committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
`TestName`. The rule is more constraining than necessary, but it's simple and
easy to remember. It also gives googletest some wiggle room in case its
implementation needs to change in the future.

If you violate the rule, there may not be immediate consequences, but your test
may (just may) break with a new compiler (or a new version of the compiler you
are using) or with a new version of googletest. Therefore it's best to follow
the rule.

## Why does googletest support `EXPECT_EQ(NULL, ptr)` and `ASSERT_EQ(NULL, ptr)` but not `EXPECT_NE(NULL, ptr)` and `ASSERT_NE(NULL, ptr)`?

First of all you can use `EXPECT_NE(nullptr, ptr)` and `ASSERT_NE(nullptr,
ptr)`. This is the preferred syntax in the style guide because nullptr does not
have the type problems that NULL does. Which is why NULL does not work.

Due to some peculiarity of C++, it requires some non-trivial template meta
programming tricks to support using `NULL` as an argument of the `EXPECT_XX()`
and `ASSERT_XX()` macros. Therefore we only do it where it's most needed
(otherwise we make the implementation of googletest harder to maintain and more
error-prone than necessary).

The `EXPECT_EQ()` macro takes the *expected* value as its first argument and the
*actual* value as the second. It's reasonable that someone wants to write
`EXPECT_EQ(NULL, some_expression)`, and this indeed was requested several times.
Therefore we implemented it.

The need for `EXPECT_NE(NULL, ptr)` isn't nearly as strong. When the assertion
fails, you already know that `ptr` must be `NULL`, so it doesn't add any
information to print `ptr` in this case. That means `EXPECT_TRUE(ptr != NULL)`
works just as well.

If we were to support `EXPECT_NE(NULL, ptr)`, for consistency we'll have to
support `EXPECT_NE(ptr, NULL)` as well, as unlike `EXPECT_EQ`, we don't have a
convention on the order of the two arguments for `EXPECT_NE`. This means using
the template meta programming tricks twice in the implementation, making it even
harder to understand and maintain. We believe the benefit doesn't justify the
cost.

Finally, with the growth of the gMock matcher library, we are encouraging people
to use the unified `EXPECT_THAT(value, matcher)` syntax more often in tests. One
significant advantage of the matcher approach is that matchers can be easily
combined to form new matchers, while the `EXPECT_NE`, etc, macros cannot be
easily combined. Therefore we want to invest more in the matchers than in the
98
99
`EXPECT_XX()` macros.

Gennadiy Civil's avatar
Gennadiy Civil committed
100
## I need to test that different implementations of an interface satisfy some common requirements. Should I use typed tests or value-parameterized tests?
101

Gennadiy Civil's avatar
Gennadiy Civil committed
102
103
104
105
For testing various implementations of the same interface, either typed tests or
value-parameterized tests can get it done. It's really up to you the user to
decide which is more convenient for you, depending on your particular case. Some
rough guidelines:
106

Gennadiy Civil's avatar
Gennadiy Civil committed
107
108
109
110
111
112
113
114
115
116
*   Typed tests can be easier to write if instances of the different
    implementations can be created the same way, modulo the type. For example,
    if all these implementations have a public default constructor (such that
    you can write `new TypeParam`), or if their factory functions have the same
    form (e.g. `CreateInstance<TypeParam>()`).
*   Value-parameterized tests can be easier to write if you need different code
    patterns to create different implementations' instances, e.g. `new Foo` vs
    `new Bar(5)`. To accommodate for the differences, you can write factory
    function wrappers and pass these function pointers to the tests as their
    parameters.
117
118
119
120
121
122
*   When a typed test fails, the default output includes the name of the type,
    which can help you quickly identify which implementation is wrong.
    Value-parameterized tests only show the number of the failed iteration by
    default. You will need to define a function that returns the iteration name
    and pass it as the third parameter to INSTANTIATE_TEST_SUITE_P to have more
    useful output.
Gennadiy Civil's avatar
Gennadiy Civil committed
123
124
125
126
127
*   When using typed tests, you need to make sure you are testing against the
    interface type, not the concrete types (in other words, you want to make
    sure `implicit_cast<MyInterface*>(my_concrete_impl)` works, not just that
    `my_concrete_impl` works). It's less likely to make mistakes in this area
    when using value-parameterized tests.
128

Gennadiy Civil's avatar
Gennadiy Civil committed
129
130
131
132
I hope I didn't confuse you more. :-) If you don't mind, I'd suggest you to give
both approaches a try. Practice is a much better way to grasp the subtle
differences between the two tools. Once you have some concrete experience, you
can much more easily decide which one to use the next time.
133

Gennadiy Civil's avatar
Gennadiy Civil committed
134
## I got some run-time errors about invalid proto descriptors when using `ProtocolMessageEquals`. Help!
135

Gennadiy Civil's avatar
Gennadiy Civil committed
136
137
**Note:** `ProtocolMessageEquals` and `ProtocolMessageEquiv` are *deprecated*
now. Please use `EqualsProto`, etc instead.
138

Gennadiy Civil's avatar
Gennadiy Civil committed
139
`ProtocolMessageEquals` and `ProtocolMessageEquiv` were redefined recently and
140
are now less tolerant of invalid protocol buffer definitions. In particular, if
Gennadiy Civil's avatar
Gennadiy Civil committed
141
142
143
you have a `foo.proto` that doesn't fully qualify the type of a protocol message
it references (e.g. `message<Bar>` where it should be `message<blah.Bar>`), you
will now get run-time errors like:
144
145

```
Gennadiy Civil's avatar
Gennadiy Civil committed
146
147
... descriptor.cc:...] Invalid proto descriptor for file "path/to/foo.proto":
... descriptor.cc:...]  blah.MyMessage.my_field: ".Bar" is not defined.
148
149
```

Gennadiy Civil's avatar
Gennadiy Civil committed
150
151
152
If you see this, your `.proto` file is broken and needs to be fixed by making
the types fully qualified. The new definition of `ProtocolMessageEquals` and
`ProtocolMessageEquiv` just happen to reveal your bug.
153

Gennadiy Civil's avatar
Gennadiy Civil committed
154
## My death test modifies some state, but the change seems lost after the death test finishes. Why?
155

Gennadiy Civil's avatar
Gennadiy Civil committed
156
157
158
159
160
Death tests (`EXPECT_DEATH`, etc) are executed in a sub-process s.t. the
expected crash won't kill the test program (i.e. the parent process). As a
result, any in-memory side effects they incur are observable in their respective
sub-processes, but not in the parent process. You can think of them as running
in a parallel universe, more or less.
161

162
163
164
165
In particular, if you use mocking and the death test statement invokes some mock
methods, the parent process will think the calls have never occurred. Therefore,
you may want to move your `EXPECT_CALL` statements inside the `EXPECT_DEATH`
macro.
166

Gennadiy Civil's avatar
Gennadiy Civil committed
167
## EXPECT_EQ(htonl(blah), blah_blah) generates weird compiler errors in opt mode. Is this a googletest bug?
168

Gennadiy Civil's avatar
Gennadiy Civil committed
169
Actually, the bug is in `htonl()`.
170

Gennadiy Civil's avatar
Gennadiy Civil committed
171
172
173
According to `'man htonl'`, `htonl()` is a *function*, which means it's valid to
use `htonl` as a function pointer. However, in opt mode `htonl()` is defined as
a *macro*, which breaks this usage.
174

Gennadiy Civil's avatar
Gennadiy Civil committed
175
176
177
178
Worse, the macro definition of `htonl()` uses a `gcc` extension and is *not*
standard C++. That hacky implementation has some ad hoc limitations. In
particular, it prevents you from writing `Foo<sizeof(htonl(x))>()`, where `Foo`
is a template that has an integral argument.
179

Gennadiy Civil's avatar
Gennadiy Civil committed
180
181
182
183
The implementation of `EXPECT_EQ(a, b)` uses `sizeof(... a ...)` inside a
template argument, and thus doesn't compile in opt mode when `a` contains a call
to `htonl()`. It is difficult to make `EXPECT_EQ` bypass the `htonl()` bug, as
the solution must work with different compilers on various platforms.
184

Gennadiy Civil's avatar
Gennadiy Civil committed
185
186
187
188
189
190
191
`htonl()` has some other problems as described in `//util/endian/endian.h`,
which defines `ghtonl()` to replace it. `ghtonl()` does the same thing `htonl()`
does, only without its problems. We suggest you to use `ghtonl()` instead of
`htonl()`, both in your tests and production code.

`//util/endian/endian.h` also defines `ghtons()`, which solves similar problems
in `htons()`.
192

Gennadiy Civil's avatar
Gennadiy Civil committed
193
194
195
196
197
Don't forget to add `//util/endian` to the list of dependencies in the `BUILD`
file wherever `ghtonl()` and `ghtons()` are used. The library consists of a
single header file and will not bloat your binary.

## The compiler complains about "undefined references" to some static const member variables, but I did define them in the class body. What's wrong?
198
199
200

If your class has a static data member:

Gennadiy Civil's avatar
Gennadiy Civil committed
201
```c++
202
203
204
205
206
207
208
// foo.h
class Foo {
  ...
  static const int kBar = 100;
};
```

Gennadiy Civil's avatar
Gennadiy Civil committed
209
You also need to define it *outside* of the class body in `foo.cc`:
210

Gennadiy Civil's avatar
Gennadiy Civil committed
211
```c++
212
213
214
215
const int Foo::kBar;  // No initializer here.
```

Otherwise your code is **invalid C++**, and may break in unexpected ways. In
Gennadiy Civil's avatar
Gennadiy Civil committed
216
217
218
particular, using it in googletest comparison assertions (`EXPECT_EQ`, etc) will
generate an "undefined reference" linker error. The fact that "it used to work"
doesn't mean it's valid. It just means that you were lucky. :-)
219

Abseil Team's avatar
Abseil Team committed
220
221
222
223
224
225
226
227
228
229
230
231
If the declaration of the static data member is `constexpr` then it is
implicitly an `inline` definition, and a separate definition in `foo.cc` is not
needed:

```c++
// foo.h
class Foo {
  ...
  static constexpr int kBar = 100;  // Defines kBar, no need to do it in foo.cc.
};
```

Gennadiy Civil's avatar
Gennadiy Civil committed
232
## Can I derive a test fixture from another?
233
234
235

Yes.

236
237
Each test fixture has a corresponding and same named test suite. This means only
one test suite can use a particular fixture. Sometimes, however, multiple test
238
cases may want to use the same or slightly different fixtures. For example, you
239
may want to make sure that all of a GUI library's test suites don't leak
240
241
important system resources like fonts and brushes.

242
In googletest, you share a fixture among test suites by putting the shared logic
Gennadiy Civil's avatar
Gennadiy Civil committed
243
in a base test fixture, then deriving from that base a separate fixture for each
244
test suite that wants to use this common logic. You then use `TEST_F()` to write
Gennadiy Civil's avatar
Gennadiy Civil committed
245
tests using each derived fixture.
246
247
248

Typically, your code looks like this:

Gennadiy Civil's avatar
Gennadiy Civil committed
249
```c++
250
251
// Defines a base test fixture.
class BaseTest : public ::testing::Test {
Gennadiy Civil's avatar
Gennadiy Civil committed
252
253
 protected:
  ...
254
255
256
257
};

// Derives a fixture FooTest from BaseTest.
class FooTest : public BaseTest {
Gennadiy Civil's avatar
Gennadiy Civil committed
258
259
260
261
262
263
264
265
266
267
268
269
270
 protected:
  void SetUp() override {
    BaseTest::SetUp();  // Sets up the base fixture first.
    ... additional set-up work ...
  }

  void TearDown() override {
    ... clean-up work for FooTest ...
    BaseTest::TearDown();  // Remember to tear down the base fixture
                           // after cleaning up FooTest!
  }

  ... functions and variables for FooTest ...
271
272
273
274
275
276
277
278
279
280
};

// Tests that use the fixture FooTest.
TEST_F(FooTest, Bar) { ... }
TEST_F(FooTest, Baz) { ... }

... additional fixtures derived from BaseTest ...
```

If necessary, you can continue to derive test fixtures from a derived fixture.
Gennadiy Civil's avatar
Gennadiy Civil committed
281
googletest has no limit on how deep the hierarchy can be.
282

283
For a complete example using derived test fixtures, see
284
[sample5_unittest.cc](../googletest/samples/sample5_unittest.cc).
285

Gennadiy Civil's avatar
Gennadiy Civil committed
286
## My compiler complains "void value not ignored as it ought to be." What does this mean?
287
288

You're probably using an `ASSERT_*()` in a function that doesn't return `void`.
Gennadiy Civil's avatar
Gennadiy Civil committed
289
290
291
`ASSERT_*()` can only be used in `void` functions, due to exceptions being
disabled by our build system. Please see more details
[here](advanced.md#assertion-placement).
292

Gennadiy Civil's avatar
Gennadiy Civil committed
293
## My death test hangs (or seg-faults). How do I fix it?
294

Gennadiy Civil's avatar
Gennadiy Civil committed
295
In googletest, death tests are run in a child process and the way they work is
296
delicate. To write death tests you really need to understand how they work.
Gennadiy Civil's avatar
Gennadiy Civil committed
297
Please make sure you have read [this](advanced.md#how-it-works).
298
299

In particular, death tests don't like having multiple threads in the parent
Gennadiy Civil's avatar
Gennadiy Civil committed
300
process. So the first thing you can try is to eliminate creating threads outside
301
302
of `EXPECT_DEATH()`. For example, you may want to use mocks or fake objects
instead of real ones in your tests.
303
304
305
306
307

Sometimes this is impossible as some library you must use may be creating
threads before `main()` is even reached. In this case, you can try to minimize
the chance of conflicts by either moving as many activities as possible inside
`EXPECT_DEATH()` (in the extreme case, you want to move everything inside), or
Gennadiy Civil's avatar
Gennadiy Civil committed
308
309
leaving as few things as possible in it. Also, you can try to set the death test
style to `"threadsafe"`, which is safer but slower, and see if it helps.
310
311
312
313
314
315

If you go with thread-safe death tests, remember that they rerun the test
program from the beginning in the child process. Therefore make sure your
program can run side-by-side with itself and is deterministic.

In the end, this boils down to good concurrent programming. You have to make
Ashik Paul's avatar
Ashik Paul committed
316
sure that there are no race conditions or deadlocks in your program. No silver
317
318
bullet - sorry!

Abseil Team's avatar
Abseil Team committed
319
## Should I use the constructor/destructor of the test fixture or SetUp()/TearDown()? {#CtorVsSetUp}
320

Gennadiy Civil's avatar
Gennadiy Civil committed
321
322
323
324
The first thing to remember is that googletest does **not** reuse the same test
fixture object across multiple tests. For each `TEST_F`, googletest will create
a **fresh** test fixture object, immediately call `SetUp()`, run the test body,
call `TearDown()`, and then delete the test fixture object.
325

Gennadiy Civil's avatar
Gennadiy Civil committed
326
327
328
When you need to write per-test set-up and tear-down logic, you have the choice
between using the test fixture constructor/destructor or `SetUp()/TearDown()`.
The former is usually preferred, as it has the following benefits:
329

Gennadiy Civil's avatar
Gennadiy Civil committed
330
331
332
333
334
335
336
337
338
*   By initializing a member variable in the constructor, we have the option to
    make it `const`, which helps prevent accidental changes to its value and
    makes the tests more obviously correct.
*   In case we need to subclass the test fixture class, the subclass'
    constructor is guaranteed to call the base class' constructor *first*, and
    the subclass' destructor is guaranteed to call the base class' destructor
    *afterward*. With `SetUp()/TearDown()`, a subclass may make the mistake of
    forgetting to call the base class' `SetUp()/TearDown()` or call them at the
    wrong time.
339

340
You may still want to use `SetUp()/TearDown()` in the following cases:
341

342
343
344
345
346
347
348
349
*   C++ does not allow virtual function calls in constructors and destructors.
    You can call a method declared as virtual, but it will not use dynamic
    dispatch, it will use the definition from the class the constructor of which
    is currently executing. This is because calling a virtual method before the
    derived class constructor has a chance to run is very dangerous - the
    virtual method might operate on uninitialized data. Therefore, if you need
    to call a method that will be overridden in a derived class, you have to use
    `SetUp()/TearDown()`.
Gennadiy Civil's avatar
Gennadiy Civil committed
350
351
352
*   In the body of a constructor (or destructor), it's not possible to use the
    `ASSERT_xx` macros. Therefore, if the set-up operation could cause a fatal
    test failure that should prevent the test from running, it's necessary to
353
    use `abort` <!-- GOOGLETEST_CM0015 DO NOT DELETE --> and abort the whole test executable,
354
    or to use `SetUp()` instead of a constructor.
Gennadiy Civil's avatar
Gennadiy Civil committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
*   If the tear-down operation could throw an exception, you must use
    `TearDown()` as opposed to the destructor, as throwing in a destructor leads
    to undefined behavior and usually will kill your program right away. Note
    that many standard libraries (like STL) may throw when exceptions are
    enabled in the compiler. Therefore you should prefer `TearDown()` if you
    want to write portable tests that work with or without exceptions.
*   The googletest team is considering making the assertion macros throw on
    platforms where exceptions are enabled (e.g. Windows, Mac OS, and Linux
    client-side), which will eliminate the need for the user to propagate
    failures from a subroutine to its caller. Therefore, you shouldn't use
    googletest assertions in a destructor if your code could run on such a
    platform.

## The compiler complains "no matching function to call" when I use ASSERT_PRED*. How do I fix it?
369
370
371
372
373
374
375
376
377
378
379
380
381

If the predicate function you use in `ASSERT_PRED*` or `EXPECT_PRED*` is
overloaded or a template, the compiler will have trouble figuring out which
overloaded version it should use. `ASSERT_PRED_FORMAT*` and
`EXPECT_PRED_FORMAT*` don't have this problem.

If you see this error, you might want to switch to
`(ASSERT|EXPECT)_PRED_FORMAT*`, which will also give you a better failure
message. If, however, that is not an option, you can resolve the problem by
explicitly telling the compiler which version to pick.

For example, suppose you have

Gennadiy Civil's avatar
Gennadiy Civil committed
382
```c++
383
384
385
bool IsPositive(int n) {
  return n > 0;
}
Gennadiy Civil's avatar
Gennadiy Civil committed
386

387
388
389
390
391
392
393
bool IsPositive(double x) {
  return x > 0;
}
```

you will get a compiler error if you write

Gennadiy Civil's avatar
Gennadiy Civil committed
394
```c++
395
396
397
398
399
EXPECT_PRED1(IsPositive, 5);
```

However, this will work:

Gennadiy Civil's avatar
Gennadiy Civil committed
400
```c++
401
EXPECT_PRED1(static_cast<bool (*)(int)>(IsPositive), 5);
402
403
```

Gennadiy Civil's avatar
Gennadiy Civil committed
404
405
(The stuff inside the angled brackets for the `static_cast` operator is the type
of the function pointer for the `int`-version of `IsPositive()`.)
406
407
408

As another example, when you have a template function

Gennadiy Civil's avatar
Gennadiy Civil committed
409
```c++
410
411
412
413
414
415
416
417
template <typename T>
bool IsNegative(T x) {
  return x < 0;
}
```

you can use it in a predicate assertion like this:

Gennadiy Civil's avatar
Gennadiy Civil committed
418
```c++
419
ASSERT_PRED1(IsNegative<int>, -5);
420
421
```

Ashik Paul's avatar
Ashik Paul committed
422
Things are more interesting if your template has more than one parameter. The
423
424
following won't compile:

Gennadiy Civil's avatar
Gennadiy Civil committed
425
```c++
426
ASSERT_PRED2(GreaterThan<int, int>, 5, 0);
427
428
```

Gennadiy Civil's avatar
Gennadiy Civil committed
429
430
431
as the C++ pre-processor thinks you are giving `ASSERT_PRED2` 4 arguments, which
is one more than expected. The workaround is to wrap the predicate function in
parentheses:
432

Gennadiy Civil's avatar
Gennadiy Civil committed
433
```c++
434
ASSERT_PRED2((GreaterThan<int, int>), 5, 0);
435
436
```

Gennadiy Civil's avatar
Gennadiy Civil committed
437
## My compiler complains about "ignoring return value" when I call RUN_ALL_TESTS(). Why?
438
439
440
441

Some people had been ignoring the return value of `RUN_ALL_TESTS()`. That is,
instead of

Gennadiy Civil's avatar
Gennadiy Civil committed
442
```c++
Gennadiy Civil's avatar
Gennadiy Civil committed
443
  return RUN_ALL_TESTS();
444
445
446
447
```

they write

Gennadiy Civil's avatar
Gennadiy Civil committed
448
```c++
Gennadiy Civil's avatar
Gennadiy Civil committed
449
  RUN_ALL_TESTS();
450
451
```

Gennadiy Civil's avatar
Gennadiy Civil committed
452
453
454
455
This is **wrong and dangerous**. The testing services needs to see the return
value of `RUN_ALL_TESTS()` in order to determine if a test has passed. If your
`main()` function ignores it, your test will be considered successful even if it
has a googletest assertion failure. Very bad.
456

Gennadiy Civil's avatar
Gennadiy Civil committed
457
458
459
We have decided to fix this (thanks to Michael Chastain for the idea). Now, your
code will no longer be able to ignore `RUN_ALL_TESTS()` when compiled with
`gcc`. If you do so, you'll get a compiler error.
460

Gennadiy Civil's avatar
Gennadiy Civil committed
461
462
463
464
465
466
467
468
If you see the compiler complaining about you ignoring the return value of
`RUN_ALL_TESTS()`, the fix is simple: just make sure its value is used as the
return value of `main()`.

But how could we introduce a change that breaks existing tests? Well, in this
case, the code was already broken in the first place, so we didn't break it. :-)

## My compiler complains that a constructor (or destructor) cannot return a value. What's going on?
469
470
471
472

Due to a peculiarity of C++, in order to support the syntax for streaming
messages to an `ASSERT_*`, e.g.

Gennadiy Civil's avatar
Gennadiy Civil committed
473
```c++
Gennadiy Civil's avatar
Gennadiy Civil committed
474
  ASSERT_EQ(1, Foo()) << "blah blah" << foo;
475
476
477
478
479
```

we had to give up using `ASSERT*` and `FAIL*` (but not `EXPECT*` and
`ADD_FAILURE*`) in constructors and destructors. The workaround is to move the
content of your constructor/destructor to a private void member function, or
Gennadiy Civil's avatar
Gennadiy Civil committed
480
481
switch to `EXPECT_*()` if that works. This
[section](advanced.md#assertion-placement) in the user's guide explains it.
482

Gennadiy Civil's avatar
Gennadiy Civil committed
483
## My SetUp() function is not called. Why?
484

Gennadiy Civil's avatar
Gennadiy Civil committed
485
C++ is case-sensitive. Did you spell it as `Setup()`?
486

487
Similarly, sometimes people spell `SetUpTestSuite()` as `SetupTestSuite()` and
488
489
490
wonder why it's never called.


491
## 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.
492
493
494

You don't have to. Instead of

Gennadiy Civil's avatar
Gennadiy Civil committed
495
```c++
496
497
498
499
500
501
502
503
504
505
506
507
class FooTest : public BaseTest {};

TEST_F(FooTest, Abc) { ... }
TEST_F(FooTest, Def) { ... }

class BarTest : public BaseTest {};

TEST_F(BarTest, Abc) { ... }
TEST_F(BarTest, Def) { ... }
```

you can simply `typedef` the test fixtures:
Gennadiy Civil's avatar
Gennadiy Civil committed
508

Gennadiy Civil's avatar
Gennadiy Civil committed
509
```c++
510
511
512
513
514
515
516
517
518
519
520
typedef BaseTest FooTest;

TEST_F(FooTest, Abc) { ... }
TEST_F(FooTest, Def) { ... }

typedef BaseTest BarTest;

TEST_F(BarTest, Abc) { ... }
TEST_F(BarTest, Def) { ... }
```

Gennadiy Civil's avatar
Gennadiy Civil committed
521
## googletest output is buried in a whole bunch of LOG messages. What do I do?
522

Gennadiy Civil's avatar
Gennadiy Civil committed
523
524
The googletest output is meant to be a concise and human-friendly report. If
your test generates textual output itself, it will mix with the googletest
525
526
527
output, making it hard to read. However, there is an easy solution to this
problem.

Gennadiy Civil's avatar
Gennadiy Civil committed
528
529
Since `LOG` messages go to stderr, we decided to let googletest output go to
stdout. This way, you can easily separate the two using redirection. For
530
531
example:

Gennadiy Civil's avatar
Gennadiy Civil committed
532
533
```shell
$ ./my_test > gtest_output.txt
534
535
```

Gennadiy Civil's avatar
Gennadiy Civil committed
536
## Why should I prefer test fixtures over global variables?
537

Gennadiy Civil's avatar
Gennadiy Civil committed
538
There are several good reasons:
539

Gennadiy Civil's avatar
Gennadiy Civil committed
540
541
542
543
544
1.  It's likely your test needs to change the states of its global variables.
    This makes it difficult to keep side effects from escaping one test and
    contaminating others, making debugging difficult. By using fixtures, each
    test has a fresh set of variables that's different (but with the same
    names). Thus, tests are kept independent of each other.
545
546
2.  Global variables pollute the global namespace.
3.  Test fixtures can be reused via subclassing, which cannot be done easily
547
    with global variables. This is useful if many test suites have something in
Gennadiy Civil's avatar
Gennadiy Civil committed
548
    common.
549

550
## What can the statement argument in ASSERT_DEATH() be?
551

hyuk.myeong's avatar
hyuk.myeong committed
552
553
`ASSERT_DEATH(statement, matcher)` (or any death assertion macro) can be used
wherever *`statement`* is valid. So basically *`statement`* can be any C++
554
555
statement that makes sense in the current context. In particular, it can
reference global and/or local variables, and can be:
Gennadiy Civil's avatar
Gennadiy Civil committed
556
557
558
559

*   a simple function call (often the case),
*   a complex expression, or
*   a compound statement.
560

Arkady Shapkin's avatar
Arkady Shapkin committed
561
562
Some examples are shown here:

Gennadiy Civil's avatar
Gennadiy Civil committed
563
```c++
564
565
566
567
568
569
570
571
572
573
574
575
// A death test can be a simple function call.
TEST(MyDeathTest, FunctionCall) {
  ASSERT_DEATH(Xyz(5), "Xyz failed");
}

// Or a complex expression that references variables and functions.
TEST(MyDeathTest, ComplexExpression) {
  const bool c = Condition();
  ASSERT_DEATH((c ? Func1(0) : object2.Method("test")),
               "(Func1|Method) failed");
}

Ashik Paul's avatar
Ashik Paul committed
576
// Death assertions can be used anywhere in a function.  In
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// particular, they can be inside a loop.
TEST(MyDeathTest, InsideLoop) {
  // Verifies that Foo(0), Foo(1), ..., and Foo(4) all die.
  for (int i = 0; i < 5; i++) {
    EXPECT_DEATH_M(Foo(i), "Foo has \\d+ errors",
                   ::testing::Message() << "where i is " << i);
  }
}

// A death assertion can contain a compound statement.
TEST(MyDeathTest, CompoundStatement) {
  // Verifies that at lease one of Bar(0), Bar(1), ..., and
  // Bar(4) dies.
  ASSERT_DEATH({
    for (int i = 0; i < 5; i++) {
      Bar(i);
    }
  },
Gennadiy Civil's avatar
Gennadiy Civil committed
595
596
  "Bar has \\d+ errors");
}
597
598
```

Gennadiy Civil's avatar
Gennadiy Civil committed
599
gtest-death-test_test.cc contains more examples if you are interested.
600

Gennadiy Civil's avatar
Gennadiy Civil committed
601
## I have a fixture class `FooTest`, but `TEST_F(FooTest, Bar)` gives me error ``"no matching function for call to `FooTest::FooTest()'"``. Why?
602

Gennadiy Civil's avatar
Gennadiy Civil committed
603
604
605
Googletest needs to be able to create objects of your test fixture class, so it
must have a default constructor. Normally the compiler will define one for you.
However, there are cases where you have to define your own:
606

Gennadiy Civil's avatar
Gennadiy Civil committed
607
608
609
610
611
612
613
*   If you explicitly declare a non-default constructor for class `FooTest`
    (`DISALLOW_EVIL_CONSTRUCTORS()` does this), then you need to define a
    default constructor, even if it would be empty.
*   If `FooTest` has a const non-static data member, then you have to define the
    default constructor *and* initialize the const member in the initializer
    list of the constructor. (Early versions of `gcc` doesn't force you to
    initialize the const member. It's a bug that has been fixed in `gcc 4`.)
614

Gennadiy Civil's avatar
Gennadiy Civil committed
615
## Why does ASSERT_DEATH complain about previous threads that were already joined?
616

Gennadiy Civil's avatar
Gennadiy Civil committed
617
With the Linux pthread library, there is no turning back once you cross the line
Ashik Paul's avatar
Ashik Paul committed
618
from a single thread to multiple threads. The first time you create a thread, a
Gennadiy Civil's avatar
Gennadiy Civil committed
619
620
621
622
manager thread is created in addition, so you get 3, not 2, threads. Later when
the thread you create joins the main thread, the thread count decrements by 1,
but the manager thread will never be killed, so you still have 2 threads, which
means you cannot safely run a death test.
623
624
625
626
627

The new NPTL thread library doesn't suffer from this problem, as it doesn't
create a manager thread. However, if you don't control which machine your test
runs on, you shouldn't depend on this.

628
## Why does googletest require the entire test suite, instead of individual tests, to be named *DeathTest when it uses ASSERT_DEATH?
629

630
631
632
googletest does not interleave tests from different test suites. That is, it
runs all tests in one test suite first, and then runs all tests in the next test
suite, and so on. googletest does this because it needs to set up a test suite
Ashik Paul's avatar
Ashik Paul committed
633
before the first test in it is run, and tear it down afterwards. Splitting up
634
635
the test case would require multiple set-up and tear-down processes, which is
inefficient and makes the semantics unclean.
636
637
638
639

If we were to determine the order of tests based on test name instead of test
case name, then we would have a problem with the following situation:

Gennadiy Civil's avatar
Gennadiy Civil committed
640
```c++
641
642
643
644
645
646
647
648
TEST_F(FooTest, AbcDeathTest) { ... }
TEST_F(FooTest, Uvw) { ... }

TEST_F(BarTest, DefDeathTest) { ... }
TEST_F(BarTest, Xyz) { ... }
```

Since `FooTest.AbcDeathTest` needs to run before `BarTest.Xyz`, and we don't
649
interleave tests from different test suites, we need to run all tests in the
650
651
652
`FooTest` case before running any test in the `BarTest` case. This contradicts
with the requirement to run `BarTest.DefDeathTest` before `FooTest.Uvw`.

653
## 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?
654

655
You don't have to, but if you like, you may split up the test suite into
656
657
658
`FooTest` and `FooDeathTest`, where the names make it clear that they are
related:

Gennadiy Civil's avatar
Gennadiy Civil committed
659
```c++
660
661
662
663
664
class FooTest : public ::testing::Test { ... };

TEST_F(FooTest, Abc) { ... }
TEST_F(FooTest, Def) { ... }

Gennadiy Civil's avatar
Gennadiy Civil committed
665
using FooDeathTest = FooTest;
666
667
668
669
670

TEST_F(FooDeathTest, Uvw) { ... EXPECT_DEATH(...) ... }
TEST_F(FooDeathTest, Xyz) { ... ASSERT_DEATH(...) ... }
```

Gennadiy Civil's avatar
Gennadiy Civil committed
671
672
673
674
675
676
677
678
679
680
681
682
## 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?

Printing the LOG messages generated by the statement inside `EXPECT_DEATH()`
makes it harder to search for real problems in the parent's log. Therefore,
googletest only prints them when the death test has failed.

If you really need to see such LOG messages, a workaround is to temporarily
break the death test (e.g. by changing the regex pattern it is expected to
match). Admittedly, this is a hack. We'll consider a more permanent solution
after the fork-and-exec-style death tests are implemented.

## The compiler complains about "no match for 'operator<<'" when I use an assertion. What gives?
683
684
685
686
687
688

If you use a user-defined type `FooType` in an assertion, you must make sure
there is an `std::ostream& operator<<(std::ostream&, const FooType&)` function
defined such that we can print a value of `FooType`.

In addition, if `FooType` is declared in a name space, the `<<` operator also
689
needs to be defined in the *same* name space. See https://abseil.io/tips/49 for details.
690

Gennadiy Civil's avatar
Gennadiy Civil committed
691
## How do I suppress the memory leak messages on Windows?
692

Gennadiy Civil's avatar
Gennadiy Civil committed
693
Since the statically initialized googletest singleton requires allocations on
694
695
696
697
698
699
the heap, the Visual C++ memory leak detector will report memory leaks at the
end of the program run. The easiest way to avoid this is to use the
`_CrtMemCheckpoint` and `_CrtMemDumpAllObjectsSince` calls to not report any
statically initialized heap objects. See MSDN for more details and additional
heap check/debug routines.

Gennadiy Civil's avatar
Gennadiy Civil committed
700
701
702
703
704
705
706
707
708
709
## How can my code detect if it is running in a test?

If you write code that sniffs whether it's running in a test and does different
things accordingly, you are leaking test-only logic into production code and
there is no easy way to ensure that the test-only code paths aren't run by
mistake in production. Such cleverness also leads to
[Heisenbugs](https://en.wikipedia.org/wiki/Heisenbug). Therefore we strongly
advise against the practice, and googletest doesn't provide a way to do it.

In general, the recommended way to cause the code to behave differently under
710
test is [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection). You can inject
Gennadiy Civil's avatar
Gennadiy Civil committed
711
712
different functionality from the test and from the production code. Since your
production code doesn't link in the for-test logic at all (the
713
714
[`testonly`](https://docs.bazel.build/versions/master/be/common-definitions.html#common.testonly) attribute for BUILD targets helps to ensure
that), there is no danger in accidentally running it.
715

Gennadiy Civil's avatar
Gennadiy Civil committed
716
717
718
719
However, if you *really*, *really*, *really* have no choice, and if you follow
the rule of ending your test program names with `_test`, you can use the
*horrible* hack of sniffing your executable name (`argv[0]` in `main()`) to know
whether the code is under test.
720

Gennadiy Civil's avatar
Gennadiy Civil committed
721
722
723
724
725
726
727
728
729
730
731
## How do I temporarily disable a test?

If you have a broken test that you cannot fix right away, you can add the
DISABLED_ prefix to its name. This will exclude it from execution. This is
better than commenting out the code or using #if 0, as disabled tests are still
compiled (and thus won't rot).

To include disabled tests in test execution, just invoke the test program with
the --gtest_also_run_disabled_tests flag.

## Is it OK if I have two separate `TEST(Foo, Bar)` test methods defined in different namespaces?
732
733
734

Yes.

735
The rule is **all test methods in the same test suite must use the same fixture
Gennadiy Civil's avatar
Gennadiy Civil committed
736
737
class.** This means that the following is **allowed** because both tests use the
same fixture class (`::testing::Test`).
738

Gennadiy Civil's avatar
 
Gennadiy Civil committed
739
```c++
740
741
742
743
744
745
746
747
748
749
namespace foo {
TEST(CoolTest, DoSomething) {
  SUCCEED();
}
}  // namespace foo

namespace bar {
TEST(CoolTest, DoSomething) {
  SUCCEED();
}
Herbert Thielen's avatar
Herbert Thielen committed
750
}  // namespace bar
751
752
```

Gennadiy Civil's avatar
Gennadiy Civil committed
753
754
However, the following code is **not allowed** and will produce a runtime error
from googletest because the test methods are using different test fixture
755
classes with the same test suite name.
756

Gennadiy Civil's avatar
 
Gennadiy Civil committed
757
```c++
758
759
760
761
762
763
764
765
766
767
768
769
namespace foo {
class CoolTest : public ::testing::Test {};  // Fixture foo::CoolTest
TEST_F(CoolTest, DoSomething) {
  SUCCEED();
}
}  // namespace foo

namespace bar {
class CoolTest : public ::testing::Test {};  // Fixture: bar::CoolTest
TEST_F(CoolTest, DoSomething) {
  SUCCEED();
}
Herbert Thielen's avatar
Herbert Thielen committed
770
}  // namespace bar
771
```