primer.md 18.7 KB
Newer Older
1
# GoogleTest Primer
Gennadiy Civil's avatar
Gennadiy Civil committed
2

3
## Introduction: Why GoogleTest?
Gennadiy Civil's avatar
Gennadiy Civil committed
4

5
*GoogleTest* helps you write better C++ tests.
Gennadiy Civil's avatar
Gennadiy Civil committed
6

7
GoogleTest is a testing framework developed by the Testing Technology team with
Abseil Team's avatar
Abseil Team committed
8
Google's specific requirements and constraints in mind. Whether you work on
9
Linux, Windows, or a Mac, if you write C++ code, GoogleTest can help you. And it
Abseil Team's avatar
Abseil Team committed
10
supports *any* kind of tests, not just unit tests.
Gennadiy Civil's avatar
Gennadiy Civil committed
11

12
So what makes a good test, and how does GoogleTest fit in? We believe:
Gennadiy Civil's avatar
Gennadiy Civil committed
13
14

1.  Tests should be *independent* and *repeatable*. It's a pain to debug a test
15
    that succeeds or fails as a result of other tests. GoogleTest isolates the
Gennadiy Civil's avatar
Gennadiy Civil committed
16
    tests by running each of them on a different object. When a test fails,
17
    GoogleTest allows you to run it in isolation for quick debugging.
18
2.  Tests should be well *organized* and reflect the structure of the tested
19
    code. GoogleTest groups related tests into test suites that can share data
Gennadiy Civil's avatar
Gennadiy Civil committed
20
21
22
    and subroutines. This common pattern is easy to recognize and makes tests
    easy to maintain. Such consistency is especially helpful when people switch
    projects and start to work on a new code base.
23
3.  Tests should be *portable* and *reusable*. Google has a lot of code that is
24
    platform-neutral; its tests should also be platform-neutral. GoogleTest
Gennadiy Civil's avatar
 
Gennadiy Civil committed
25
    works on different OSes, with different compilers, with or without
26
    exceptions, so GoogleTest tests can work with a variety of configurations.
27
4.  When tests fail, they should provide as much *information* about the problem
28
    as possible. GoogleTest doesn't stop at the first test failure. Instead, it
Gennadiy Civil's avatar
Gennadiy Civil committed
29
30
31
32
    only stops the current test and continues with the next. You can also set up
    tests that report non-fatal failures after which the current test continues.
    Thus, you can detect and fix multiple bugs in a single run-edit-compile
    cycle.
33
5.  The testing framework should liberate test writers from housekeeping chores
34
    and let them focus on the test *content*. GoogleTest automatically keeps
Gennadiy Civil's avatar
Gennadiy Civil committed
35
36
    track of all tests defined, and doesn't require the user to enumerate them
    in order to run them.
37
6.  Tests should be *fast*. With GoogleTest, you can reuse shared resources
Gennadiy Civil's avatar
Gennadiy Civil committed
38
39
40
    across tests and pay for the set-up/tear-down only once, without making
    tests depend on each other.

41
Since GoogleTest is based on the popular xUnit architecture, you'll feel right
Gennadiy Civil's avatar
Gennadiy Civil committed
42
43
at home if you've used JUnit or PyUnit before. If not, it will take you about 10
minutes to learn the basics and get started. So let's go!
44

45
## Beware of the Nomenclature
46

Abseil Team's avatar
Abseil Team committed
47
{: .callout .note}
48
49
*Note:* There might be some confusion arising from different definitions of the
terms *Test*, *Test Case* and *Test Suite*, so beware of misunderstanding these.
50

51
Historically, GoogleTest started to use the term *Test Case* for grouping
Abseil Team's avatar
Abseil Team committed
52
53
54
related tests, whereas current publications, including International Software
Testing Qualifications Board ([ISTQB](http://www.istqb.org/)) materials and
various textbooks on software quality, use the term
55
*[Test Suite][istqb test suite]* for this.
56

57
The related term *Test*, as it is used in GoogleTest, corresponds to the term
58
*[Test Case][istqb test case]* of ISTQB and others.
59

60
61
The term *Test* is commonly of broad enough sense, including ISTQB's definition
of *Test Case*, so it's not much of a problem here. But the term *Test Case* as
62
was used in Google Test is of contradictory sense and thus confusing.
63

64
GoogleTest recently started replacing the term *Test Case* with *Test Suite*.
Abseil Team's avatar
Abseil Team committed
65
66
The preferred API is *TestSuite*. The older TestCase API is being slowly
deprecated and refactored away.
67

68
So please be aware of the different definitions of the terms:
69

70

71
Meaning                                                                              | GoogleTest Term         | [ISTQB](http://www.istqb.org/) Term
72
73
:----------------------------------------------------------------------------------- | :---------------------- | :----------------------------------
Exercise a particular program path with specific input values and verify the results | [TEST()](#simple-tests) | [Test Case][istqb test case]
Abseil Team's avatar
Abseil Team committed
74
75
76
77


[istqb test case]: http://glossary.istqb.org/en/search/test%20case
[istqb test suite]: http://glossary.istqb.org/en/search/test%20suite
Gennadiy Civil's avatar
Gennadiy Civil committed
78
79
80

## Basic Concepts

81
When using GoogleTest, you start by writing *assertions*, which are statements
Gennadiy Civil's avatar
Gennadiy Civil committed
82
83
84
85
86
87
88
that check whether a condition is true. An assertion's result can be *success*,
*nonfatal failure*, or *fatal failure*. If a fatal failure occurs, it aborts the
current function; otherwise the program continues normally.

*Tests* use assertions to verify the tested code's behavior. If a test crashes
or has a failed assertion, then it *fails*; otherwise it *succeeds*.

Gennadiy Civil's avatar
 
Gennadiy Civil committed
89
90
91
A *test suite* contains one or many tests. You should group your tests into test
suites that reflect the structure of the tested code. When multiple tests in a
test suite need to share common objects and subroutines, you can put them into a
Gennadiy Civil's avatar
Gennadiy Civil committed
92
*test fixture* class.
93

Gennadiy Civil's avatar
 
Gennadiy Civil committed
94
A *test program* can contain multiple test suites.
95
96

We'll now explain how to write a test program, starting at the individual
Gennadiy Civil's avatar
 
Gennadiy Civil committed
97
assertion level and building up to tests and test suites.
98

Gennadiy Civil's avatar
Gennadiy Civil committed
99
## Assertions
100

101
GoogleTest assertions are macros that resemble function calls. You test a class
Gennadiy Civil's avatar
Gennadiy Civil committed
102
or function by making assertions about its behavior. When an assertion fails,
103
GoogleTest prints the assertion's source file and line number location, along
Gennadiy Civil's avatar
Gennadiy Civil committed
104
with a failure message. You may also supply a custom failure message which will
105
be appended to GoogleTest's message.
106

Gennadiy Civil's avatar
Gennadiy Civil committed
107
108
109
110
111
112
113
The assertions come in pairs that test the same thing but have different effects
on the current function. `ASSERT_*` versions generate fatal failures when they
fail, and **abort the current function**. `EXPECT_*` versions generate nonfatal
failures, which don't abort the current function. Usually `EXPECT_*` are
preferred, as they allow more than one failure to be reported in a test.
However, you should use `ASSERT_*` if it doesn't make sense to continue when the
assertion in question fails.
114
115
116

Since a failed `ASSERT_*` returns from the current function immediately,
possibly skipping clean-up code that comes after it, it may cause a space leak.
Gennadiy Civil's avatar
Gennadiy Civil committed
117
118
Depending on the nature of the leak, it may or may not be worth fixing - so keep
this in mind if you get a heap checker error in addition to assertion errors.
119
120

To provide a custom failure message, simply stream it into the macro using the
Abseil Team's avatar
Abseil Team committed
121
`<<` operator or a sequence of such operators. See the following example, using
Abseil Team's avatar
Abseil Team committed
122
123
the [`ASSERT_EQ` and `EXPECT_EQ`](reference/assertions.md#EXPECT_EQ) macros to
verify value equality:
Gennadiy Civil's avatar
Gennadiy Civil committed
124
125

```c++
126
127
128
129
130
131
132
133
134
135
136
137
ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length";

for (int i = 0; i < x.size(); ++i) {
  EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;
}
```

Anything that can be streamed to an `ostream` can be streamed to an assertion
macro--in particular, C strings and `string` objects. If a wide string
(`wchar_t*`, `TCHAR*` in `UNICODE` mode on Windows, or `std::wstring`) is
streamed to an assertion, it will be translated to UTF-8 when printed.

Abseil Team's avatar
Abseil Team committed
138
139
140
141
142
143
GoogleTest provides a collection of assertions for verifying the behavior of
your code in various ways. You can check Boolean conditions, compare values
based on relational operators, verify string values, floating-point values, and
much more. There are even assertions that enable you to verify more complex
states by providing custom predicates. For the complete list of assertions
provided by GoogleTest, see the [Assertions Reference](reference/assertions.md).
144

Gennadiy Civil's avatar
Gennadiy Civil committed
145
## Simple Tests
146
147
148

To create a test:

Abseil Team's avatar
Abseil Team committed
149
1.  Use the `TEST()` macro to define and name a test function. These are
Gennadiy Civil's avatar
Gennadiy Civil committed
150
    ordinary C++ functions that don't return a value.
151
2.  In this function, along with any valid C++ statements you want to include,
152
    use the various GoogleTest assertions to check values.
153
3.  The test's result is determined by the assertions; if any assertion in the
Gennadiy Civil's avatar
Gennadiy Civil committed
154
155
156
157
    test fails (either fatally or non-fatally), or if the test crashes, the
    entire test fails. Otherwise, it succeeds.

```c++
158
TEST(TestSuiteName, TestName) {
Gennadiy Civil's avatar
Gennadiy Civil committed
159
  ... test body ...
160
161
162
}
```

Gennadiy Civil's avatar
Gennadiy Civil committed
163
`TEST()` arguments go from general to specific. The *first* argument is the name
164
of the test suite, and the *second* argument is the test's name within the test
Abseil Team's avatar
Abseil Team committed
165
166
167
suite. Both names must be valid C++ identifiers, and they should not contain any
underscores (`_`). A test's *full name* consists of its containing test suite
and its individual name. Tests from different test suites can have the same
Gennadiy Civil's avatar
Gennadiy Civil committed
168
individual name.
169
170

For example, let's take a simple integer function:
Gennadiy Civil's avatar
Gennadiy Civil committed
171
172
173

```c++
int Factorial(int n);  // Returns the factorial of n
174
175
```

176
A test suite for this function might look like:
Gennadiy Civil's avatar
Gennadiy Civil committed
177
178

```c++
179
180
// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
Gennadiy Civil's avatar
Gennadiy Civil committed
181
  EXPECT_EQ(Factorial(0), 1);
182
183
184
185
}

// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
Gennadiy Civil's avatar
Gennadiy Civil committed
186
187
188
189
  EXPECT_EQ(Factorial(1), 1);
  EXPECT_EQ(Factorial(2), 2);
  EXPECT_EQ(Factorial(3), 6);
  EXPECT_EQ(Factorial(8), 40320);
190
191
192
}
```

193
GoogleTest groups the test results by test suites, so logically related tests
194
should be in the same test suite; in other words, the first argument to their
195
`TEST()` should be the same. In the above example, we have two tests,
196
197
`HandlesZeroInput` and `HandlesPositiveInput`, that belong to the same test
suite `FactorialTest`.
198

199
When naming your test suites and tests, you should follow the same convention as
200
201
for
[naming functions and classes](https://google.github.io/styleguide/cppguide.html#Function_Names).
202

Gennadiy Civil's avatar
Gennadiy Civil committed
203
**Availability**: Linux, Windows, Mac.
204

Abseil Team's avatar
Abseil Team committed
205
## Test Fixtures: Using the Same Data Configuration for Multiple Tests {#same-data-multiple-tests}
Gennadiy Civil's avatar
Gennadiy Civil committed
206
207

If you find yourself writing two or more tests that operate on similar data, you
Abseil Team's avatar
Abseil Team committed
208
can use a *test fixture*. This allows you to reuse the same configuration of
209
210
objects for several different tests.

Gennadiy Civil's avatar
Gennadiy Civil committed
211
212
To create a fixture:

Abseil Team's avatar
Abseil Team committed
213
1.  Derive a class from `::testing::Test` . Start its body with `protected:`, as
Gennadiy Civil's avatar
Gennadiy Civil committed
214
    we'll want to access fixture members from sub-classes.
215
216
2.  Inside the class, declare any objects you plan to use.
3.  If necessary, write a default constructor or `SetUp()` function to prepare
Gennadiy Civil's avatar
Gennadiy Civil committed
217
218
    the objects for each test. A common mistake is to spell `SetUp()` as
    **`Setup()`** with a small `u` - Use `override` in C++11 to make sure you
Abseil Team's avatar
Abseil Team committed
219
    spelled it correctly.
220
4.  If necessary, write a destructor or `TearDown()` function to release any
Gennadiy Civil's avatar
Gennadiy Civil committed
221
222
    resources you allocated in `SetUp()` . To learn when you should use the
    constructor/destructor and when you should use `SetUp()/TearDown()`, read
Abseil Team's avatar
Abseil Team committed
223
    the [FAQ](faq.md#CtorVsSetUp).
224
5.  If needed, define subroutines for your tests to share.
225
226
227

When using a fixture, use `TEST_F()` instead of `TEST()` as it allows you to
access objects and subroutines in the test fixture:
Gennadiy Civil's avatar
Gennadiy Civil committed
228
229

```c++
230
TEST_F(TestFixtureClassName, TestName) {
Gennadiy Civil's avatar
Gennadiy Civil committed
231
  ... test body ...
232
233
234
}
```

235
236
237
Unlike `TEST()`, in `TEST_F()` the first argument must be the name of the test
fixture class. (`_F` stands for "Fixture"). No test suite name is specified for
this macro.
238
239
240
241
242
243
244
245
246

Unfortunately, the C++ macro system does not allow us to create a single macro
that can handle both types of tests. Using the wrong macro causes a compiler
error.

Also, you must first define a test fixture class before using it in a
`TEST_F()`, or you'll get the compiler error "`virtual outside class
declaration`".

247
For each test defined with `TEST_F()`, GoogleTest will create a *fresh* test
Abseil Team's avatar
Abseil Team committed
248
249
fixture at runtime, immediately initialize it via `SetUp()`, run the test, clean
up by calling `TearDown()`, and then delete the test fixture. Note that
250
different tests in the same test suite have different test fixture objects, and
251
252
GoogleTest always deletes a test fixture before it creates the next one.
GoogleTest does **not** reuse the same test fixture for multiple tests. Any
Gennadiy Civil's avatar
Gennadiy Civil committed
253
changes one test makes to the fixture do not affect other tests.
254

Gennadiy Civil's avatar
Gennadiy Civil committed
255
256
257
258
259
As an example, let's write tests for a FIFO queue class named `Queue`, which has
the following interface:

```c++
template <typename E>  // E is the element type.
260
261
262
263
class Queue {
 public:
  Queue();
  void Enqueue(const E& element);
Gennadiy Civil's avatar
Gennadiy Civil committed
264
  E* Dequeue();  // Returns NULL if the queue is empty.
265
266
267
268
269
270
271
  size_t size() const;
  ...
};
```

First, define a fixture class. By convention, you should give it the name
`FooTest` where `Foo` is the class being tested.
Gennadiy Civil's avatar
Gennadiy Civil committed
272
273

```c++
274
275
class QueueTest : public ::testing::Test {
 protected:
Gennadiy Civil's avatar
Gennadiy Civil committed
276
  void SetUp() override {
277
     // q0_ remains empty
278
279
     q1_.Enqueue(1);
     q2_.Enqueue(2);
Gennadiy Civil's avatar
Gennadiy Civil committed
280
     q2_.Enqueue(3);
281
282
  }

Gennadiy Civil's avatar
Gennadiy Civil committed
283
  // void TearDown() override {}
284
285
286
287
288
289
290
291
292
293
294

  Queue<int> q0_;
  Queue<int> q1_;
  Queue<int> q2_;
};
```

In this case, `TearDown()` is not needed since we don't have to clean up after
each test, other than what's already done by the destructor.

Now we'll write tests using `TEST_F()` and this fixture.
Gennadiy Civil's avatar
Gennadiy Civil committed
295
296

```c++
297
TEST_F(QueueTest, IsEmptyInitially) {
Gennadiy Civil's avatar
Gennadiy Civil committed
298
  EXPECT_EQ(q0_.size(), 0);
299
300
301
302
}

TEST_F(QueueTest, DequeueWorks) {
  int* n = q0_.Dequeue();
Gennadiy Civil's avatar
Gennadiy Civil committed
303
  EXPECT_EQ(n, nullptr);
304
305

  n = q1_.Dequeue();
Gennadiy Civil's avatar
Gennadiy Civil committed
306
307
308
  ASSERT_NE(n, nullptr);
  EXPECT_EQ(*n, 1);
  EXPECT_EQ(q1_.size(), 0);
309
310
311
  delete n;

  n = q2_.Dequeue();
Gennadiy Civil's avatar
Gennadiy Civil committed
312
313
314
  ASSERT_NE(n, nullptr);
  EXPECT_EQ(*n, 2);
  EXPECT_EQ(q2_.size(), 1);
315
316
317
318
319
  delete n;
}
```

The above uses both `ASSERT_*` and `EXPECT_*` assertions. The rule of thumb is
Gennadiy Civil's avatar
Gennadiy Civil committed
320
321
322
to use `EXPECT_*` when you want the test to continue to reveal more errors after
the assertion failure, and use `ASSERT_*` when continuing after failure doesn't
make sense. For example, the second assertion in the `Dequeue` test is
Abseil Team's avatar
Abseil Team committed
323
`ASSERT_NE(n, nullptr)`, as we need to dereference the pointer `n` later, which
Gennadiy Civil's avatar
Gennadiy Civil committed
324
would lead to a segfault when `n` is `NULL`.
325
326
327

When these tests run, the following happens:

328
1.  GoogleTest constructs a `QueueTest` object (let's call it `t1`).
Abseil Team's avatar
Abseil Team committed
329
330
2.  `t1.SetUp()` initializes `t1`.
3.  The first test (`IsEmptyInitially`) runs on `t1`.
331
332
333
4.  `t1.TearDown()` cleans up after the test finishes.
5.  `t1` is destructed.
6.  The above steps are repeated on another `QueueTest` object, this time
Gennadiy Civil's avatar
Gennadiy Civil committed
334
    running the `DequeueWorks` test.
335

Gennadiy Civil's avatar
Gennadiy Civil committed
336
**Availability**: Linux, Windows, Mac.
337

Gennadiy Civil's avatar
Gennadiy Civil committed
338
## Invoking the Tests
339

340
`TEST()` and `TEST_F()` implicitly register their tests with GoogleTest. So,
Gennadiy Civil's avatar
Gennadiy Civil committed
341
342
unlike with many other C++ testing frameworks, you don't have to re-list all
your defined tests in order to run them.
343

Abseil Team's avatar
Abseil Team committed
344
After defining your tests, you can run them with `RUN_ALL_TESTS()`, which
Gennadiy Civil's avatar
Gennadiy Civil committed
345
returns `0` if all the tests are successful, or `1` otherwise. Note that
Abseil Team's avatar
Abseil Team committed
346
347
`RUN_ALL_TESTS()` runs *all tests* in your link unit--they can be from different
test suites, or even different source files.
348
349
350

When invoked, the `RUN_ALL_TESTS()` macro:

351
*   Saves the state of all GoogleTest flags.
Gennadiy Civil's avatar
Gennadiy Civil committed
352
353
354
355
356
357

*   Creates a test fixture object for the first test.

*   Initializes it via `SetUp()`.

*   Runs the test on the fixture object.
358

Gennadiy Civil's avatar
Gennadiy Civil committed
359
*   Cleans up the fixture via `TearDown()`.
360

Gennadiy Civil's avatar
Gennadiy Civil committed
361
*   Deletes the fixture.
362

363
*   Restores the state of all GoogleTest flags.
364

Gennadiy Civil's avatar
Gennadiy Civil committed
365
*   Repeats the above steps for the next test, until all tests have run.
366

Gennadiy Civil's avatar
Gennadiy Civil committed
367
368
If a fatal failure happens the subsequent steps will be skipped.

Abseil Team's avatar
Abseil Team committed
369
{: .callout .important}
Gennadiy Civil's avatar
Gennadiy Civil committed
370
371
372
373
374
375
376
> IMPORTANT: You must **not** ignore the return value of `RUN_ALL_TESTS()`, or
> you will get a compiler error. The rationale for this design is that the
> automated testing service determines whether a test has passed based on its
> exit code, not on its stdout/stderr output; thus your `main()` function must
> return the value of `RUN_ALL_TESTS()`.
>
> Also, you should call `RUN_ALL_TESTS()` only **once**. Calling it more than
377
> once conflicts with some advanced GoogleTest features (e.g., thread-safe
378
> [death tests](advanced.md#death-tests)) and thus is not supported.
Gennadiy Civil's avatar
Gennadiy Civil committed
379
380
381
382
383

**Availability**: Linux, Windows, Mac.

## Writing the main() Function

384
Most users should *not* need to write their own `main` function and instead link
Abseil Team's avatar
Abseil Team committed
385
386
387
388
389
390
with `gtest_main` (as opposed to with `gtest`), which defines a suitable entry
point. See the end of this section for details. The remainder of this section
should only apply when you need to do something custom before the tests run that
cannot be expressed within the framework of fixtures and test suites.

If you write your own `main` function, it should return the value of
Abseil Team's avatar
Abseil Team committed
391
`RUN_ALL_TESTS()`.
392
393

You can start from this boilerplate:
Gennadiy Civil's avatar
Gennadiy Civil committed
394
395

```c++
396
#include "this/package/foo.h"
Abseil Team's avatar
Abseil Team committed
397

398
399
#include "gtest/gtest.h"

Abseil Team's avatar
Abseil Team committed
400
401
namespace my {
namespace project {
402
403
404
405
406
namespace {

// The fixture for testing class Foo.
class FooTest : public ::testing::Test {
 protected:
Abseil Team's avatar
Abseil Team committed
407
408
  // You can remove any or all of the following functions if their bodies would
  // be empty.
409
410

  FooTest() {
Gennadiy Civil's avatar
Gennadiy Civil committed
411
     // You can do set-up work for each test here.
412
413
  }

Gennadiy Civil's avatar
Gennadiy Civil committed
414
415
  ~FooTest() override {
     // You can do clean-up work that doesn't throw exceptions here.
416
417
418
419
420
  }

  // If the constructor and destructor are not enough for setting up
  // and cleaning up each test, you can define the following methods:

Gennadiy Civil's avatar
Gennadiy Civil committed
421
422
423
  void SetUp() override {
     // Code here will be called immediately after the constructor (right
     // before each test).
424
425
  }

Gennadiy Civil's avatar
Gennadiy Civil committed
426
427
428
  void TearDown() override {
     // Code here will be called immediately after each test (right
     // before the destructor).
429
430
  }

Abseil Team's avatar
Abseil Team committed
431
432
  // Class members declared here can be used by all tests in the test suite
  // for Foo.
433
434
435
436
};

// Tests that the Foo::Bar() method does Abc.
TEST_F(FooTest, MethodBarDoesAbc) {
Gennadiy Civil's avatar
Gennadiy Civil committed
437
438
  const std::string input_filepath = "this/package/testdata/myinputfile.dat";
  const std::string output_filepath = "this/package/testdata/myoutputfile.dat";
439
  Foo f;
Gennadiy Civil's avatar
Gennadiy Civil committed
440
  EXPECT_EQ(f.Bar(input_filepath, output_filepath), 0);
441
442
443
444
445
446
447
448
}

// Tests that Foo does Xyz.
TEST_F(FooTest, DoesXyz) {
  // Exercises the Xyz feature of Foo.
}

}  // namespace
Abseil Team's avatar
Abseil Team committed
449
450
}  // namespace project
}  // namespace my
451
452
453
454
455
456
457

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
```

Gennadiy Civil's avatar
Gennadiy Civil committed
458
The `::testing::InitGoogleTest()` function parses the command line for
459
GoogleTest flags, and removes all recognized flags. This allows the user to
Abseil Team's avatar
Abseil Team committed
460
461
control a test program's behavior via various flags, which we'll cover in the
[AdvancedGuide](advanced.md). You **must** call this function before calling
Gennadiy Civil's avatar
Gennadiy Civil committed
462
`RUN_ALL_TESTS()`, or the flags won't be properly initialized.
463
464
465
466

On Windows, `InitGoogleTest()` also works with wide strings, so it can be used
in programs compiled in `UNICODE` mode as well.

Abseil Team's avatar
Abseil Team committed
467
But maybe you think that writing all those `main` functions is too much work? We
Abseil Team's avatar
Abseil Team committed
468
agree with you completely, and that's why Google Test provides a basic
Gennadiy Civil's avatar
Gennadiy Civil committed
469
implementation of main(). If it fits your needs, then just link your test with
Abseil Team's avatar
Abseil Team committed
470
the `gtest_main` library and you are good to go.
471

Abseil Team's avatar
Abseil Team committed
472
{: .callout .note}
Gennadiy Civil's avatar
Gennadiy Civil committed
473
474
475
NOTE: `ParseGUnitFlags()` is deprecated in favor of `InitGoogleTest()`.

## Known Limitations
476

Gennadiy Civil's avatar
Gennadiy Civil committed
477
478
*   Google Test is designed to be thread-safe. The implementation is thread-safe
    on systems where the `pthreads` library is available. It is currently
479
    *unsafe* to use Google Test assertions from two threads concurrently on
Gennadiy Civil's avatar
Gennadiy Civil committed
480
481
482
483
    other systems (e.g. Windows). In most tests this is not an issue as usually
    the assertions are done in the main thread. If you want to help, you can
    volunteer to implement the necessary synchronization primitives in
    `gtest-port.h` for your platform.