xref: /freebsd/contrib/googletest/docs/reference/testing.md (revision 46333229c6a0187ebf231805682ee0bceed704d1)
1# Testing Reference
2
3<!--* toc_depth: 3 *-->
4
5This page lists the facilities provided by GoogleTest for writing test programs.
6To use them, add `#include <gtest/gtest.h>`.
7
8## Macros
9
10GoogleTest defines the following macros for writing tests.
11
12### TEST {#TEST}
13
14<pre>
15TEST(<em>TestSuiteName</em>, <em>TestName</em>) {
16  ... <em>statements</em> ...
17}
18</pre>
19
20Defines an individual test named *`TestName`* in the test suite
21*`TestSuiteName`*, consisting of the given statements.
22
23Both arguments *`TestSuiteName`* and *`TestName`* must be valid C++ identifiers
24and must not contain underscores (`_`). Tests in different test suites can have
25the same individual name.
26
27The statements within the test body can be any code under test.
28[Assertions](assertions.md) used within the test body determine the outcome of
29the test.
30
31### TEST_F {#TEST_F}
32
33<pre>
34TEST_F(<em>TestFixtureName</em>, <em>TestName</em>) {
35  ... <em>statements</em> ...
36}
37</pre>
38
39Defines an individual test named *`TestName`* that uses the test fixture class
40*`TestFixtureName`*. The test suite name is *`TestFixtureName`*.
41
42Both arguments *`TestFixtureName`* and *`TestName`* must be valid C++
43identifiers and must not contain underscores (`_`). *`TestFixtureName`* must be
44the name of a test fixture class—see
45[Test Fixtures](../primer.md#same-data-multiple-tests).
46
47The statements within the test body can be any code under test.
48[Assertions](assertions.md) used within the test body determine the outcome of
49the test.
50
51### TEST_P {#TEST_P}
52
53<pre>
54TEST_P(<em>TestFixtureName</em>, <em>TestName</em>) {
55  ... <em>statements</em> ...
56}
57</pre>
58
59Defines an individual value-parameterized test named *`TestName`* that uses the
60test fixture class *`TestFixtureName`*. The test suite name is
61*`TestFixtureName`*.
62
63Both arguments *`TestFixtureName`* and *`TestName`* must be valid C++
64identifiers and must not contain underscores (`_`). *`TestFixtureName`* must be
65the name of a value-parameterized test fixture class—see
66[Value-Parameterized Tests](../advanced.md#value-parameterized-tests).
67
68The statements within the test body can be any code under test. Within the test
69body, the test parameter can be accessed with the `GetParam()` function (see
70[`WithParamInterface`](#WithParamInterface)). For example:
71
72```cpp
73TEST_P(MyTestSuite, DoesSomething) {
74  ...
75  EXPECT_TRUE(DoSomething(GetParam()));
76  ...
77}
78```
79
80[Assertions](assertions.md) used within the test body determine the outcome of
81the test.
82
83See also [`INSTANTIATE_TEST_SUITE_P`](#INSTANTIATE_TEST_SUITE_P).
84
85### INSTANTIATE_TEST_SUITE_P {#INSTANTIATE_TEST_SUITE_P}
86
87`INSTANTIATE_TEST_SUITE_P(`*`InstantiationName`*`,`*`TestSuiteName`*`,`*`param_generator`*`)`
88\
89`INSTANTIATE_TEST_SUITE_P(`*`InstantiationName`*`,`*`TestSuiteName`*`,`*`param_generator`*`,`*`name_generator`*`)`
90
91Instantiates the value-parameterized test suite *`TestSuiteName`* (defined with
92[`TEST_P`](#TEST_P)).
93
94The argument *`InstantiationName`* is a unique name for the instantiation of the
95test suite, to distinguish between multiple instantiations. In test output, the
96instantiation name is added as a prefix to the test suite name
97*`TestSuiteName`*. If *`InstantiationName`* is empty
98(`INSTANTIATE_TEST_SUITE_P(, ...)`), no prefix is added.
99
100The argument *`param_generator`* is one of the following GoogleTest-provided
101functions that generate the test parameters, all defined in the `::testing`
102namespace:
103
104<span id="param-generators"></span>
105
106| Parameter Generator | Behavior                                             |
107| ------------------- | ---------------------------------------------------- |
108| `Range(begin, end [, step])` | Yields values `{begin, begin+step, begin+step+step, ...}`. The values do not include `end`. `step` defaults to 1. |
109| `Values(v1, v2, ..., vN)`    | Yields values `{v1, v2, ..., vN}`.          |
110| `ValuesIn(container)` or `ValuesIn(begin,end)` | Yields values from a C-style array, an STL-style container, or an iterator range `[begin, end)`. |
111| `Bool()`                     | Yields sequence `{false, true}`.            |
112| `Combine(g1, g2, ..., gN)`   | Yields as `std::tuple` *n*-tuples all combinations (Cartesian product) of the values generated by the given *n* generators `g1`, `g2`, ..., `gN`. |
113| `ConvertGenerator<T>(g)` or `ConvertGenerator(g, func)`    | Yields values generated by generator `g`, `static_cast` from `T`. (Note: `T` might not be what you expect. See [*Using ConvertGenerator*](#using-convertgenerator) below.) The second overload uses `func` to perform the conversion. |
114
115The optional last argument *`name_generator`* is a function or functor that
116generates custom test name suffixes based on the test parameters. The function
117must accept an argument of type
118[`TestParamInfo<class ParamType>`](#TestParamInfo) and return a `std::string`.
119The test name suffix can only contain alphanumeric characters and underscores.
120GoogleTest provides [`PrintToStringParamName`](#PrintToStringParamName), or a
121custom function can be used for more control:
122
123```cpp
124INSTANTIATE_TEST_SUITE_P(
125    MyInstantiation, MyTestSuite,
126    testing::Values(...),
127    [](const testing::TestParamInfo<MyTestSuite::ParamType>& info) {
128      // Can use info.param here to generate the test suffix
129      std::string name = ...
130      return name;
131    });
132```
133
134For more information, see
135[Value-Parameterized Tests](../advanced.md#value-parameterized-tests).
136
137See also
138[`GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST`](#GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST).
139
140###### Using `ConvertGenerator`
141
142The functions listed in the table above appear to return generators that create
143values of the desired types, but this is not generally the case. Rather, they
144typically return factory objects that convert to the the desired generators.
145This affords some flexibility in allowing you to specify values of types that
146are different from, yet implicitly convertible to, the actual parameter type
147required by your fixture class.
148
149For example, you can do the following with a fixture that requires an `int`
150parameter:
151
152```cpp
153INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
154    testing::Values(1, 1.2));  // Yes, Values() supports heterogeneous argument types.
155```
156
157It might seem obvious that `1.2` &mdash; a `double` &mdash; will be converted to
158an `int` but in actuality it requires some template gymnastics involving the
159indirection described in the previous paragraph.
160
161What if your parameter type is not implicitly convertible from the generated
162type but is *explicitly* convertible? There will be no automatic conversion, but
163you can force it by applying `ConvertGenerator<T>`. The compiler can
164automatically deduce the target type (your fixture's parameter type), but
165because of the aforementioned indirection it cannot decide what the generated
166type should be. You need to tell it, by providing the type `T` explicitly. Thus
167`T` should not be your fixture's parameter type, but rather an intermediate type
168that is supported by the factory object, and which can be `static_cast` to the
169fixture's parameter type:
170
171```cpp
172// The fixture's parameter type.
173class MyParam {
174 public:
175  // Explicit converting ctor.
176  explicit MyParam(const std::tuple<int, bool>& t);
177  ...
178};
179
180INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
181    ConvertGenerator<std::tuple<int, bool>>(Combine(Values(0.1, 1.2), Bool())));
182```
183
184In this example `Combine` supports the generation of `std::tuple<int, bool>>`
185objects (even though the provided values for the first tuple element are
186`double`s) and those `tuple`s get converted into `MyParam` objects by virtue of
187the call to `ConvertGenerator`.
188
189For parameter types that are not convertible from the generated types you can
190provide a callable that does the conversion. The callable accepts an object of
191the generated type and returns an object of the fixture's parameter type. The
192generated type can often be deduced by the compiler from the callable's call
193signature so you do not usually need specify it explicitly (but see a caveat
194below).
195
196```cpp
197// The fixture's parameter type.
198class MyParam {
199 public:
200  MyParam(int, bool);
201  ...
202};
203
204INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
205    ConvertGenerator(Combine(Values(1, 1.2), Bool()),
206        [](const std::tuple<int i, bool>& t){
207          const auto [i, b] = t;
208          return MyParam(i, b);
209        }));
210```
211
212The callable may be anything that can be used to initialize a `std::function`
213with the appropriate call signature. Note the callable's return object gets
214`static_cast` to the fixture's parameter type, so it does not have to be of that
215exact type, only convertible to it.
216
217**Caveat:** Consider the following example.
218
219```cpp
220INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
221    ConvertGenerator(Values(std::string("s")), [](std::string_view s) { ... }));
222```
223
224The `string` argument gets copied into the factory object returned by `Values`.
225Then, because the generated type deduced from the lambda is `string_view`, the
226factory object spawns a generator that holds a `string_view` referencing that
227`string`. Unfortunately, by the time this generator gets invoked, the factory
228object is gone and the `string_view` is dangling.
229
230To overcome this problem you can specify the generated type explicitly:
231`ConvertGenerator<std::string>(Values(std::string("s")), [](std::string_view s)
232{ ... })`. Alternatively, you can change the lambda's signature to take a
233`std::string` or a `const std::string&` (the latter will not leave you with a
234dangling reference because the type deduction strips off the reference and the
235`const`).
236
237### TYPED_TEST_SUITE {#TYPED_TEST_SUITE}
238
239`TYPED_TEST_SUITE(`*`TestFixtureName`*`,`*`Types`*`)`
240`TYPED_TEST_SUITE(`*`TestFixtureName`*`,`*`Types`*`,`*`NameGenerator`*`)`
241
242Defines a typed test suite based on the test fixture *`TestFixtureName`*. The
243test suite name is *`TestFixtureName`*.
244
245The argument *`TestFixtureName`* is a fixture class template, parameterized by a
246type, for example:
247
248```cpp
249template <typename T>
250class MyFixture : public testing::Test {
251 public:
252  ...
253  using List = std::list<T>;
254  static T shared_;
255  T value_;
256};
257```
258
259The argument *`Types`* is a [`Types`](#Types) object representing the list of
260types to run the tests on, for example:
261
262```cpp
263using MyTypes = ::testing::Types<char, int, unsigned int>;
264TYPED_TEST_SUITE(MyFixture, MyTypes);
265```
266
267The type alias (`using` or `typedef`) is necessary for the `TYPED_TEST_SUITE`
268macro to parse correctly.
269
270The optional third argument *`NameGenerator`* allows specifying a class that
271exposes a templated static function `GetName(int)`. For example:
272
273```cpp
274class NameGenerator {
275 public:
276  template <typename T>
277  static std::string GetName(int) {
278    if constexpr (std::is_same_v<T, char>) return "char";
279    if constexpr (std::is_same_v<T, int>) return "int";
280    if constexpr (std::is_same_v<T, unsigned int>) return "unsignedInt";
281  }
282};
283TYPED_TEST_SUITE(MyFixture, MyTypes, NameGenerator);
284```
285
286See also [`TYPED_TEST`](#TYPED_TEST) and
287[Typed Tests](../advanced.md#typed-tests) for more information.
288
289### TYPED_TEST {#TYPED_TEST}
290
291<pre>
292TYPED_TEST(<em>TestSuiteName</em>, <em>TestName</em>) {
293  ... <em>statements</em> ...
294}
295</pre>
296
297Defines an individual typed test named *`TestName`* in the typed test suite
298*`TestSuiteName`*. The test suite must be defined with
299[`TYPED_TEST_SUITE`](#TYPED_TEST_SUITE).
300
301Within the test body, the special name `TypeParam` refers to the type parameter,
302and `TestFixture` refers to the fixture class. See the following example:
303
304```cpp
305TYPED_TEST(MyFixture, Example) {
306  // Inside a test, refer to the special name TypeParam to get the type
307  // parameter.  Since we are inside a derived class template, C++ requires
308  // us to visit the members of MyFixture via 'this'.
309  TypeParam n = this->value_;
310
311  // To visit static members of the fixture, add the 'TestFixture::'
312  // prefix.
313  n += TestFixture::shared_;
314
315  // To refer to typedefs in the fixture, add the 'typename TestFixture::'
316  // prefix. The 'typename' is required to satisfy the compiler.
317  typename TestFixture::List values;
318
319  values.push_back(n);
320  ...
321}
322```
323
324For more information, see [Typed Tests](../advanced.md#typed-tests).
325
326### TYPED_TEST_SUITE_P {#TYPED_TEST_SUITE_P}
327
328`TYPED_TEST_SUITE_P(`*`TestFixtureName`*`)`
329
330Defines a type-parameterized test suite based on the test fixture
331*`TestFixtureName`*. The test suite name is *`TestFixtureName`*.
332
333The argument *`TestFixtureName`* is a fixture class template, parameterized by a
334type. See [`TYPED_TEST_SUITE`](#TYPED_TEST_SUITE) for an example.
335
336See also [`TYPED_TEST_P`](#TYPED_TEST_P) and
337[Type-Parameterized Tests](../advanced.md#type-parameterized-tests) for more
338information.
339
340### TYPED_TEST_P {#TYPED_TEST_P}
341
342<pre>
343TYPED_TEST_P(<em>TestSuiteName</em>, <em>TestName</em>) {
344  ... <em>statements</em> ...
345}
346</pre>
347
348Defines an individual type-parameterized test named *`TestName`* in the
349type-parameterized test suite *`TestSuiteName`*. The test suite must be defined
350with [`TYPED_TEST_SUITE_P`](#TYPED_TEST_SUITE_P).
351
352Within the test body, the special name `TypeParam` refers to the type parameter,
353and `TestFixture` refers to the fixture class. See [`TYPED_TEST`](#TYPED_TEST)
354for an example.
355
356See also [`REGISTER_TYPED_TEST_SUITE_P`](#REGISTER_TYPED_TEST_SUITE_P) and
357[Type-Parameterized Tests](../advanced.md#type-parameterized-tests) for more
358information.
359
360### REGISTER_TYPED_TEST_SUITE_P {#REGISTER_TYPED_TEST_SUITE_P}
361
362`REGISTER_TYPED_TEST_SUITE_P(`*`TestSuiteName`*`,`*`TestNames...`*`)`
363
364Registers the type-parameterized tests *`TestNames...`* of the test suite
365*`TestSuiteName`*. The test suite and tests must be defined with
366[`TYPED_TEST_SUITE_P`](#TYPED_TEST_SUITE_P) and [`TYPED_TEST_P`](#TYPED_TEST_P).
367
368For example:
369
370```cpp
371// Define the test suite and tests.
372TYPED_TEST_SUITE_P(MyFixture);
373TYPED_TEST_P(MyFixture, HasPropertyA) { ... }
374TYPED_TEST_P(MyFixture, HasPropertyB) { ... }
375
376// Register the tests in the test suite.
377REGISTER_TYPED_TEST_SUITE_P(MyFixture, HasPropertyA, HasPropertyB);
378```
379
380See also [`INSTANTIATE_TYPED_TEST_SUITE_P`](#INSTANTIATE_TYPED_TEST_SUITE_P) and
381[Type-Parameterized Tests](../advanced.md#type-parameterized-tests) for more
382information.
383
384### INSTANTIATE_TYPED_TEST_SUITE_P {#INSTANTIATE_TYPED_TEST_SUITE_P}
385
386`INSTANTIATE_TYPED_TEST_SUITE_P(`*`InstantiationName`*`,`*`TestSuiteName`*`,`*`Types`*`)`
387
388Instantiates the type-parameterized test suite *`TestSuiteName`*. The test suite
389must be registered with
390[`REGISTER_TYPED_TEST_SUITE_P`](#REGISTER_TYPED_TEST_SUITE_P).
391
392The argument *`InstantiationName`* is a unique name for the instantiation of the
393test suite, to distinguish between multiple instantiations. In test output, the
394instantiation name is added as a prefix to the test suite name
395*`TestSuiteName`*. If *`InstantiationName`* is empty
396(`INSTANTIATE_TYPED_TEST_SUITE_P(, ...)`), no prefix is added.
397
398The argument *`Types`* is a [`Types`](#Types) object representing the list of
399types to run the tests on, for example:
400
401```cpp
402using MyTypes = ::testing::Types<char, int, unsigned int>;
403INSTANTIATE_TYPED_TEST_SUITE_P(MyInstantiation, MyFixture, MyTypes);
404```
405
406The type alias (`using` or `typedef`) is necessary for the
407`INSTANTIATE_TYPED_TEST_SUITE_P` macro to parse correctly.
408
409For more information, see
410[Type-Parameterized Tests](../advanced.md#type-parameterized-tests).
411
412### FRIEND_TEST {#FRIEND_TEST}
413
414`FRIEND_TEST(`*`TestSuiteName`*`,`*`TestName`*`)`
415
416Within a class body, declares an individual test as a friend of the class,
417enabling the test to access private class members.
418
419If the class is defined in a namespace, then in order to be friends of the
420class, test fixtures and tests must be defined in the exact same namespace,
421without inline or anonymous namespaces.
422
423For example, if the class definition looks like the following:
424
425```cpp
426namespace my_namespace {
427
428class MyClass {
429  friend class MyClassTest;
430  FRIEND_TEST(MyClassTest, HasPropertyA);
431  FRIEND_TEST(MyClassTest, HasPropertyB);
432  ... definition of class MyClass ...
433};
434
435}  // namespace my_namespace
436```
437
438Then the test code should look like:
439
440```cpp
441namespace my_namespace {
442
443class MyClassTest : public testing::Test {
444  ...
445};
446
447TEST_F(MyClassTest, HasPropertyA) { ... }
448TEST_F(MyClassTest, HasPropertyB) { ... }
449
450}  // namespace my_namespace
451```
452
453See [Testing Private Code](../advanced.md#testing-private-code) for more
454information.
455
456### SCOPED_TRACE {#SCOPED_TRACE}
457
458`SCOPED_TRACE(`*`message`*`)`
459
460Causes the current file name, line number, and the given message *`message`* to
461be added to the failure message for each assertion failure that occurs in the
462scope.
463
464For more information, see
465[Adding Traces to Assertions](../advanced.md#adding-traces-to-assertions).
466
467See also the [`ScopedTrace` class](#ScopedTrace).
468
469### GTEST_SKIP {#GTEST_SKIP}
470
471`GTEST_SKIP()`
472
473Prevents further test execution at runtime.
474
475Can be used in individual test cases or in the `SetUp()` methods of test
476environments or test fixtures (classes derived from the
477[`Environment`](#Environment) or [`Test`](#Test) classes). If used in a global
478test environment `SetUp()` method, it skips all tests in the test program. If
479used in a test fixture `SetUp()` method, it skips all tests in the corresponding
480test suite.
481
482Similar to assertions, `GTEST_SKIP` allows streaming a custom message into it.
483
484See [Skipping Test Execution](../advanced.md#skipping-test-execution) for more
485information.
486
487### GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST {#GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST}
488
489`GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(`*`TestSuiteName`*`)`
490
491Allows the value-parameterized test suite *`TestSuiteName`* to be
492uninstantiated.
493
494By default, every [`TEST_P`](#TEST_P) call without a corresponding
495[`INSTANTIATE_TEST_SUITE_P`](#INSTANTIATE_TEST_SUITE_P) call causes a failing
496test in the test suite `GoogleTestVerification`.
497`GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST` suppresses this failure for the
498given test suite.
499
500## Classes and types
501
502GoogleTest defines the following classes and types to help with writing tests.
503
504### AssertionResult {#AssertionResult}
505
506`testing::AssertionResult`
507
508A class for indicating whether an assertion was successful.
509
510When the assertion wasn't successful, the `AssertionResult` object stores a
511non-empty failure message that can be retrieved with the object's `message()`
512method.
513
514To create an instance of this class, use one of the factory functions
515[`AssertionSuccess()`](#AssertionSuccess) or
516[`AssertionFailure()`](#AssertionFailure).
517
518### AssertionException {#AssertionException}
519
520`testing::AssertionException`
521
522Exception which can be thrown from
523[`TestEventListener::OnTestPartResult`](#TestEventListener::OnTestPartResult).
524
525### EmptyTestEventListener {#EmptyTestEventListener}
526
527`testing::EmptyTestEventListener`
528
529Provides an empty implementation of all methods in the
530[`TestEventListener`](#TestEventListener) interface, such that a subclass only
531needs to override the methods it cares about.
532
533### Environment {#Environment}
534
535`testing::Environment`
536
537Represents a global test environment. See
538[Global Set-Up and Tear-Down](../advanced.md#global-set-up-and-tear-down).
539
540#### Protected Methods {#Environment-protected}
541
542##### SetUp {#Environment::SetUp}
543
544`virtual void Environment::SetUp()`
545
546Override this to define how to set up the environment.
547
548##### TearDown {#Environment::TearDown}
549
550`virtual void Environment::TearDown()`
551
552Override this to define how to tear down the environment.
553
554### ScopedTrace {#ScopedTrace}
555
556`testing::ScopedTrace`
557
558An instance of this class causes a trace to be included in every test failure
559message generated by code in the scope of the lifetime of the `ScopedTrace`
560instance. The effect is undone with the destruction of the instance.
561
562The `ScopedTrace` constructor has the following form:
563
564```cpp
565template <typename T>
566ScopedTrace(const char* file, int line, const T& message)
567```
568
569Example usage:
570
571```cpp
572testing::ScopedTrace trace("file.cc", 123, "message");
573```
574
575The resulting trace includes the given source file path and line number, and the
576given message. The `message` argument can be anything streamable to
577`std::ostream`.
578
579See also [`SCOPED_TRACE`](#SCOPED_TRACE).
580
581### Test {#Test}
582
583`testing::Test`
584
585The abstract class that all tests inherit from. `Test` is not copyable.
586
587#### Public Methods {#Test-public}
588
589##### SetUpTestSuite {#Test::SetUpTestSuite}
590
591`static void Test::SetUpTestSuite()`
592
593Performs shared setup for all tests in the test suite. GoogleTest calls
594`SetUpTestSuite()` before running the first test in the test suite.
595
596##### TearDownTestSuite {#Test::TearDownTestSuite}
597
598`static void Test::TearDownTestSuite()`
599
600Performs shared teardown for all tests in the test suite. GoogleTest calls
601`TearDownTestSuite()` after running the last test in the test suite.
602
603##### HasFatalFailure {#Test::HasFatalFailure}
604
605`static bool Test::HasFatalFailure()`
606
607Returns true if and only if the current test has a fatal failure.
608
609##### HasNonfatalFailure {#Test::HasNonfatalFailure}
610
611`static bool Test::HasNonfatalFailure()`
612
613Returns true if and only if the current test has a nonfatal failure.
614
615##### HasFailure {#Test::HasFailure}
616
617`static bool Test::HasFailure()`
618
619Returns true if and only if the current test has any failure, either fatal or
620nonfatal.
621
622##### IsSkipped {#Test::IsSkipped}
623
624`static bool Test::IsSkipped()`
625
626Returns true if and only if the current test was skipped.
627
628##### RecordProperty {#Test::RecordProperty}
629
630`static void Test::RecordProperty(const std::string& key, const std::string&
631value)` \
632`static void Test::RecordProperty(const std::string& key, int value)`
633
634Logs a property for the current test, test suite, or entire invocation of the
635test program. Only the last value for a given key is logged.
636
637The key must be a valid XML attribute name, and cannot conflict with the ones
638already used by GoogleTest (`name`, `file`, `line`, `status`, `time`,
639`classname`, `type_param`, and `value_param`).
640
641`RecordProperty` is `public static` so it can be called from utility functions
642that are not members of the test fixture.
643
644Calls to `RecordProperty` made during the lifespan of the test (from the moment
645its constructor starts to the moment its destructor finishes) are output in XML
646as attributes of the `<testcase>` element. Properties recorded from a fixture's
647`SetUpTestSuite` or `TearDownTestSuite` methods are logged as attributes of the
648corresponding `<testsuite>` element. Calls to `RecordProperty` made in the
649global context (before or after invocation of `RUN_ALL_TESTS` or from the
650`SetUp`/`TearDown` methods of registered `Environment` objects) are output as
651attributes of the `<testsuites>` element.
652
653#### Protected Methods {#Test-protected}
654
655##### SetUp {#Test::SetUp}
656
657`virtual void Test::SetUp()`
658
659Override this to perform test fixture setup. GoogleTest calls `SetUp()` before
660running each individual test.
661
662##### TearDown {#Test::TearDown}
663
664`virtual void Test::TearDown()`
665
666Override this to perform test fixture teardown. GoogleTest calls `TearDown()`
667after running each individual test.
668
669### TestWithParam {#TestWithParam}
670
671`testing::TestWithParam<T>`
672
673A convenience class which inherits from both [`Test`](#Test) and
674[`WithParamInterface<T>`](#WithParamInterface).
675
676### TestSuite {#TestSuite}
677
678Represents a test suite. `TestSuite` is not copyable.
679
680#### Public Methods {#TestSuite-public}
681
682##### name {#TestSuite::name}
683
684`const char* TestSuite::name() const`
685
686Gets the name of the test suite.
687
688##### type_param {#TestSuite::type_param}
689
690`const char* TestSuite::type_param() const`
691
692Returns the name of the parameter type, or `NULL` if this is not a typed or
693type-parameterized test suite. See [Typed Tests](../advanced.md#typed-tests) and
694[Type-Parameterized Tests](../advanced.md#type-parameterized-tests).
695
696##### should_run {#TestSuite::should_run}
697
698`bool TestSuite::should_run() const`
699
700Returns true if any test in this test suite should run.
701
702##### successful_test_count {#TestSuite::successful_test_count}
703
704`int TestSuite::successful_test_count() const`
705
706Gets the number of successful tests in this test suite.
707
708##### skipped_test_count {#TestSuite::skipped_test_count}
709
710`int TestSuite::skipped_test_count() const`
711
712Gets the number of skipped tests in this test suite.
713
714##### failed_test_count {#TestSuite::failed_test_count}
715
716`int TestSuite::failed_test_count() const`
717
718Gets the number of failed tests in this test suite.
719
720##### reportable_disabled_test_count {#TestSuite::reportable_disabled_test_count}
721
722`int TestSuite::reportable_disabled_test_count() const`
723
724Gets the number of disabled tests that will be reported in the XML report.
725
726##### disabled_test_count {#TestSuite::disabled_test_count}
727
728`int TestSuite::disabled_test_count() const`
729
730Gets the number of disabled tests in this test suite.
731
732##### reportable_test_count {#TestSuite::reportable_test_count}
733
734`int TestSuite::reportable_test_count() const`
735
736Gets the number of tests to be printed in the XML report.
737
738##### test_to_run_count {#TestSuite::test_to_run_count}
739
740`int TestSuite::test_to_run_count() const`
741
742Get the number of tests in this test suite that should run.
743
744##### total_test_count {#TestSuite::total_test_count}
745
746`int TestSuite::total_test_count() const`
747
748Gets the number of all tests in this test suite.
749
750##### Passed {#TestSuite::Passed}
751
752`bool TestSuite::Passed() const`
753
754Returns true if and only if the test suite passed.
755
756##### Failed {#TestSuite::Failed}
757
758`bool TestSuite::Failed() const`
759
760Returns true if and only if the test suite failed.
761
762##### elapsed_time {#TestSuite::elapsed_time}
763
764`TimeInMillis TestSuite::elapsed_time() const`
765
766Returns the elapsed time, in milliseconds.
767
768##### start_timestamp {#TestSuite::start_timestamp}
769
770`TimeInMillis TestSuite::start_timestamp() const`
771
772Gets the time of the test suite start, in ms from the start of the UNIX epoch.
773
774##### GetTestInfo {#TestSuite::GetTestInfo}
775
776`const TestInfo* TestSuite::GetTestInfo(int i) const`
777
778Returns the [`TestInfo`](#TestInfo) for the `i`-th test among all the tests. `i`
779can range from 0 to `total_test_count() - 1`. If `i` is not in that range,
780returns `NULL`.
781
782##### ad_hoc_test_result {#TestSuite::ad_hoc_test_result}
783
784`const TestResult& TestSuite::ad_hoc_test_result() const`
785
786Returns the [`TestResult`](#TestResult) that holds test properties recorded
787during execution of `SetUpTestSuite` and `TearDownTestSuite`.
788
789### TestInfo {#TestInfo}
790
791`testing::TestInfo`
792
793Stores information about a test.
794
795#### Public Methods {#TestInfo-public}
796
797##### test_suite_name {#TestInfo::test_suite_name}
798
799`const char* TestInfo::test_suite_name() const`
800
801Returns the test suite name.
802
803##### name {#TestInfo::name}
804
805`const char* TestInfo::name() const`
806
807Returns the test name.
808
809##### type_param {#TestInfo::type_param}
810
811`const char* TestInfo::type_param() const`
812
813Returns the name of the parameter type, or `NULL` if this is not a typed or
814type-parameterized test. See [Typed Tests](../advanced.md#typed-tests) and
815[Type-Parameterized Tests](../advanced.md#type-parameterized-tests).
816
817##### value_param {#TestInfo::value_param}
818
819`const char* TestInfo::value_param() const`
820
821Returns the text representation of the value parameter, or `NULL` if this is not
822a value-parameterized test. See
823[Value-Parameterized Tests](../advanced.md#value-parameterized-tests).
824
825##### file {#TestInfo::file}
826
827`const char* TestInfo::file() const`
828
829Returns the file name where this test is defined.
830
831##### line {#TestInfo::line}
832
833`int TestInfo::line() const`
834
835Returns the line where this test is defined.
836
837##### is_in_another_shard {#TestInfo::is_in_another_shard}
838
839`bool TestInfo::is_in_another_shard() const`
840
841Returns true if this test should not be run because it's in another shard.
842
843##### should_run {#TestInfo::should_run}
844
845`bool TestInfo::should_run() const`
846
847Returns true if this test should run, that is if the test is not disabled (or it
848is disabled but the `also_run_disabled_tests` flag has been specified) and its
849full name matches the user-specified filter.
850
851GoogleTest allows the user to filter the tests by their full names. Only the
852tests that match the filter will run. See
853[Running a Subset of the Tests](../advanced.md#running-a-subset-of-the-tests)
854for more information.
855
856##### is_reportable {#TestInfo::is_reportable}
857
858`bool TestInfo::is_reportable() const`
859
860Returns true if and only if this test will appear in the XML report.
861
862##### result {#TestInfo::result}
863
864`const TestResult* TestInfo::result() const`
865
866Returns the result of the test. See [`TestResult`](#TestResult).
867
868### TestParamInfo {#TestParamInfo}
869
870`testing::TestParamInfo<T>`
871
872Describes a parameter to a value-parameterized test. The type `T` is the type of
873the parameter.
874
875Contains the fields `param` and `index` which hold the value of the parameter
876and its integer index respectively.
877
878### UnitTest {#UnitTest}
879
880`testing::UnitTest`
881
882This class contains information about the test program.
883
884`UnitTest` is a singleton class. The only instance is created when
885`UnitTest::GetInstance()` is first called. This instance is never deleted.
886
887`UnitTest` is not copyable.
888
889#### Public Methods {#UnitTest-public}
890
891##### GetInstance {#UnitTest::GetInstance}
892
893`static UnitTest* UnitTest::GetInstance()`
894
895Gets the singleton `UnitTest` object. The first time this method is called, a
896`UnitTest` object is constructed and returned. Consecutive calls will return the
897same object.
898
899##### original_working_dir {#UnitTest::original_working_dir}
900
901`const char* UnitTest::original_working_dir() const`
902
903Returns the working directory when the first [`TEST()`](#TEST) or
904[`TEST_F()`](#TEST_F) was executed. The `UnitTest` object owns the string.
905
906##### current_test_suite {#UnitTest::current_test_suite}
907
908`const TestSuite* UnitTest::current_test_suite() const`
909
910Returns the [`TestSuite`](#TestSuite) object for the test that's currently
911running, or `NULL` if no test is running.
912
913##### current_test_info {#UnitTest::current_test_info}
914
915`const TestInfo* UnitTest::current_test_info() const`
916
917Returns the [`TestInfo`](#TestInfo) object for the test that's currently
918running, or `NULL` if no test is running.
919
920##### random_seed {#UnitTest::random_seed}
921
922`int UnitTest::random_seed() const`
923
924Returns the random seed used at the start of the current test run.
925
926##### successful_test_suite_count {#UnitTest::successful_test_suite_count}
927
928`int UnitTest::successful_test_suite_count() const`
929
930Gets the number of successful test suites.
931
932##### failed_test_suite_count {#UnitTest::failed_test_suite_count}
933
934`int UnitTest::failed_test_suite_count() const`
935
936Gets the number of failed test suites.
937
938##### total_test_suite_count {#UnitTest::total_test_suite_count}
939
940`int UnitTest::total_test_suite_count() const`
941
942Gets the number of all test suites.
943
944##### test_suite_to_run_count {#UnitTest::test_suite_to_run_count}
945
946`int UnitTest::test_suite_to_run_count() const`
947
948Gets the number of all test suites that contain at least one test that should
949run.
950
951##### successful_test_count {#UnitTest::successful_test_count}
952
953`int UnitTest::successful_test_count() const`
954
955Gets the number of successful tests.
956
957##### skipped_test_count {#UnitTest::skipped_test_count}
958
959`int UnitTest::skipped_test_count() const`
960
961Gets the number of skipped tests.
962
963##### failed_test_count {#UnitTest::failed_test_count}
964
965`int UnitTest::failed_test_count() const`
966
967Gets the number of failed tests.
968
969##### reportable_disabled_test_count {#UnitTest::reportable_disabled_test_count}
970
971`int UnitTest::reportable_disabled_test_count() const`
972
973Gets the number of disabled tests that will be reported in the XML report.
974
975##### disabled_test_count {#UnitTest::disabled_test_count}
976
977`int UnitTest::disabled_test_count() const`
978
979Gets the number of disabled tests.
980
981##### reportable_test_count {#UnitTest::reportable_test_count}
982
983`int UnitTest::reportable_test_count() const`
984
985Gets the number of tests to be printed in the XML report.
986
987##### total_test_count {#UnitTest::total_test_count}
988
989`int UnitTest::total_test_count() const`
990
991Gets the number of all tests.
992
993##### test_to_run_count {#UnitTest::test_to_run_count}
994
995`int UnitTest::test_to_run_count() const`
996
997Gets the number of tests that should run.
998
999##### start_timestamp {#UnitTest::start_timestamp}
1000
1001`TimeInMillis UnitTest::start_timestamp() const`
1002
1003Gets the time of the test program start, in ms from the start of the UNIX epoch.
1004
1005##### elapsed_time {#UnitTest::elapsed_time}
1006
1007`TimeInMillis UnitTest::elapsed_time() const`
1008
1009Gets the elapsed time, in milliseconds.
1010
1011##### Passed {#UnitTest::Passed}
1012
1013`bool UnitTest::Passed() const`
1014
1015Returns true if and only if the unit test passed (i.e. all test suites passed).
1016
1017##### Failed {#UnitTest::Failed}
1018
1019`bool UnitTest::Failed() const`
1020
1021Returns true if and only if the unit test failed (i.e. some test suite failed or
1022something outside of all tests failed).
1023
1024##### GetTestSuite {#UnitTest::GetTestSuite}
1025
1026`const TestSuite* UnitTest::GetTestSuite(int i) const`
1027
1028Gets the [`TestSuite`](#TestSuite) object for the `i`-th test suite among all
1029the test suites. `i` can range from 0 to `total_test_suite_count() - 1`. If `i`
1030is not in that range, returns `NULL`.
1031
1032##### ad_hoc_test_result {#UnitTest::ad_hoc_test_result}
1033
1034`const TestResult& UnitTest::ad_hoc_test_result() const`
1035
1036Returns the [`TestResult`](#TestResult) containing information on test failures
1037and properties logged outside of individual test suites.
1038
1039##### listeners {#UnitTest::listeners}
1040
1041`TestEventListeners& UnitTest::listeners()`
1042
1043Returns the list of event listeners that can be used to track events inside
1044GoogleTest. See [`TestEventListeners`](#TestEventListeners).
1045
1046### TestEventListener {#TestEventListener}
1047
1048`testing::TestEventListener`
1049
1050The interface for tracing execution of tests. The methods below are listed in
1051the order the corresponding events are fired.
1052
1053#### Public Methods {#TestEventListener-public}
1054
1055##### OnTestProgramStart {#TestEventListener::OnTestProgramStart}
1056
1057`virtual void TestEventListener::OnTestProgramStart(const UnitTest& unit_test)`
1058
1059Fired before any test activity starts.
1060
1061##### OnTestIterationStart {#TestEventListener::OnTestIterationStart}
1062
1063`virtual void TestEventListener::OnTestIterationStart(const UnitTest& unit_test,
1064int iteration)`
1065
1066Fired before each iteration of tests starts. There may be more than one
1067iteration if `GTEST_FLAG(repeat)` is set. `iteration` is the iteration index,
1068starting from 0.
1069
1070##### OnEnvironmentsSetUpStart {#TestEventListener::OnEnvironmentsSetUpStart}
1071
1072`virtual void TestEventListener::OnEnvironmentsSetUpStart(const UnitTest&
1073unit_test)`
1074
1075Fired before environment set-up for each iteration of tests starts.
1076
1077##### OnEnvironmentsSetUpEnd {#TestEventListener::OnEnvironmentsSetUpEnd}
1078
1079`virtual void TestEventListener::OnEnvironmentsSetUpEnd(const UnitTest&
1080unit_test)`
1081
1082Fired after environment set-up for each iteration of tests ends.
1083
1084##### OnTestSuiteStart {#TestEventListener::OnTestSuiteStart}
1085
1086`virtual void TestEventListener::OnTestSuiteStart(const TestSuite& test_suite)`
1087
1088Fired before the test suite starts.
1089
1090##### OnTestStart {#TestEventListener::OnTestStart}
1091
1092`virtual void TestEventListener::OnTestStart(const TestInfo& test_info)`
1093
1094Fired before the test starts.
1095
1096##### OnTestPartResult {#TestEventListener::OnTestPartResult}
1097
1098`virtual void TestEventListener::OnTestPartResult(const TestPartResult&
1099test_part_result)`
1100
1101Fired after a failed assertion or a `SUCCEED()` invocation. If you want to throw
1102an exception from this function to skip to the next test, it must be an
1103[`AssertionException`](#AssertionException) or inherited from it.
1104
1105##### OnTestEnd {#TestEventListener::OnTestEnd}
1106
1107`virtual void TestEventListener::OnTestEnd(const TestInfo& test_info)`
1108
1109Fired after the test ends.
1110
1111##### OnTestSuiteEnd {#TestEventListener::OnTestSuiteEnd}
1112
1113`virtual void TestEventListener::OnTestSuiteEnd(const TestSuite& test_suite)`
1114
1115Fired after the test suite ends.
1116
1117##### OnEnvironmentsTearDownStart {#TestEventListener::OnEnvironmentsTearDownStart}
1118
1119`virtual void TestEventListener::OnEnvironmentsTearDownStart(const UnitTest&
1120unit_test)`
1121
1122Fired before environment tear-down for each iteration of tests starts.
1123
1124##### OnEnvironmentsTearDownEnd {#TestEventListener::OnEnvironmentsTearDownEnd}
1125
1126`virtual void TestEventListener::OnEnvironmentsTearDownEnd(const UnitTest&
1127unit_test)`
1128
1129Fired after environment tear-down for each iteration of tests ends.
1130
1131##### OnTestIterationEnd {#TestEventListener::OnTestIterationEnd}
1132
1133`virtual void TestEventListener::OnTestIterationEnd(const UnitTest& unit_test,
1134int iteration)`
1135
1136Fired after each iteration of tests finishes.
1137
1138##### OnTestProgramEnd {#TestEventListener::OnTestProgramEnd}
1139
1140`virtual void TestEventListener::OnTestProgramEnd(const UnitTest& unit_test)`
1141
1142Fired after all test activities have ended.
1143
1144### TestEventListeners {#TestEventListeners}
1145
1146`testing::TestEventListeners`
1147
1148Lets users add listeners to track events in GoogleTest.
1149
1150#### Public Methods {#TestEventListeners-public}
1151
1152##### Append {#TestEventListeners::Append}
1153
1154`void TestEventListeners::Append(TestEventListener* listener)`
1155
1156Appends an event listener to the end of the list. GoogleTest assumes ownership
1157of the listener (i.e. it will delete the listener when the test program
1158finishes).
1159
1160##### Release {#TestEventListeners::Release}
1161
1162`TestEventListener* TestEventListeners::Release(TestEventListener* listener)`
1163
1164Removes the given event listener from the list and returns it. It then becomes
1165the caller's responsibility to delete the listener. Returns `NULL` if the
1166listener is not found in the list.
1167
1168##### default_result_printer {#TestEventListeners::default_result_printer}
1169
1170`TestEventListener* TestEventListeners::default_result_printer() const`
1171
1172Returns the standard listener responsible for the default console output. Can be
1173removed from the listeners list to shut down default console output. Note that
1174removing this object from the listener list with
1175[`Release()`](#TestEventListeners::Release) transfers its ownership to the
1176caller and makes this function return `NULL` the next time.
1177
1178##### default_xml_generator {#TestEventListeners::default_xml_generator}
1179
1180`TestEventListener* TestEventListeners::default_xml_generator() const`
1181
1182Returns the standard listener responsible for the default XML output controlled
1183by the `--gtest_output=xml` flag. Can be removed from the listeners list by
1184users who want to shut down the default XML output controlled by this flag and
1185substitute it with custom one. Note that removing this object from the listener
1186list with [`Release()`](#TestEventListeners::Release) transfers its ownership to
1187the caller and makes this function return `NULL` the next time.
1188
1189### TestPartResult {#TestPartResult}
1190
1191`testing::TestPartResult`
1192
1193A copyable object representing the result of a test part (i.e. an assertion or
1194an explicit `FAIL()`, `ADD_FAILURE()`, or `SUCCESS()`).
1195
1196#### Public Methods {#TestPartResult-public}
1197
1198##### type {#TestPartResult::type}
1199
1200`Type TestPartResult::type() const`
1201
1202Gets the outcome of the test part.
1203
1204The return type `Type` is an enum defined as follows:
1205
1206```cpp
1207enum Type {
1208  kSuccess,          // Succeeded.
1209  kNonFatalFailure,  // Failed but the test can continue.
1210  kFatalFailure,     // Failed and the test should be terminated.
1211  kSkip              // Skipped.
1212};
1213```
1214
1215##### file_name {#TestPartResult::file_name}
1216
1217`const char* TestPartResult::file_name() const`
1218
1219Gets the name of the source file where the test part took place, or `NULL` if
1220it's unknown.
1221
1222##### line_number {#TestPartResult::line_number}
1223
1224`int TestPartResult::line_number() const`
1225
1226Gets the line in the source file where the test part took place, or `-1` if it's
1227unknown.
1228
1229##### summary {#TestPartResult::summary}
1230
1231`const char* TestPartResult::summary() const`
1232
1233Gets the summary of the failure message.
1234
1235##### message {#TestPartResult::message}
1236
1237`const char* TestPartResult::message() const`
1238
1239Gets the message associated with the test part.
1240
1241##### skipped {#TestPartResult::skipped}
1242
1243`bool TestPartResult::skipped() const`
1244
1245Returns true if and only if the test part was skipped.
1246
1247##### passed {#TestPartResult::passed}
1248
1249`bool TestPartResult::passed() const`
1250
1251Returns true if and only if the test part passed.
1252
1253##### nonfatally_failed {#TestPartResult::nonfatally_failed}
1254
1255`bool TestPartResult::nonfatally_failed() const`
1256
1257Returns true if and only if the test part non-fatally failed.
1258
1259##### fatally_failed {#TestPartResult::fatally_failed}
1260
1261`bool TestPartResult::fatally_failed() const`
1262
1263Returns true if and only if the test part fatally failed.
1264
1265##### failed {#TestPartResult::failed}
1266
1267`bool TestPartResult::failed() const`
1268
1269Returns true if and only if the test part failed.
1270
1271### TestProperty {#TestProperty}
1272
1273`testing::TestProperty`
1274
1275A copyable object representing a user-specified test property which can be
1276output as a key/value string pair.
1277
1278#### Public Methods {#TestProperty-public}
1279
1280##### key {#key}
1281
1282`const char* key() const`
1283
1284Gets the user-supplied key.
1285
1286##### value {#value}
1287
1288`const char* value() const`
1289
1290Gets the user-supplied value.
1291
1292##### SetValue {#SetValue}
1293
1294`void SetValue(const std::string& new_value)`
1295
1296Sets a new value, overriding the previous one.
1297
1298### TestResult {#TestResult}
1299
1300`testing::TestResult`
1301
1302Contains information about the result of a single test.
1303
1304`TestResult` is not copyable.
1305
1306#### Public Methods {#TestResult-public}
1307
1308##### total_part_count {#TestResult::total_part_count}
1309
1310`int TestResult::total_part_count() const`
1311
1312Gets the number of all test parts. This is the sum of the number of successful
1313test parts and the number of failed test parts.
1314
1315##### test_property_count {#TestResult::test_property_count}
1316
1317`int TestResult::test_property_count() const`
1318
1319Returns the number of test properties.
1320
1321##### Passed {#TestResult::Passed}
1322
1323`bool TestResult::Passed() const`
1324
1325Returns true if and only if the test passed (i.e. no test part failed).
1326
1327##### Skipped {#TestResult::Skipped}
1328
1329`bool TestResult::Skipped() const`
1330
1331Returns true if and only if the test was skipped.
1332
1333##### Failed {#TestResult::Failed}
1334
1335`bool TestResult::Failed() const`
1336
1337Returns true if and only if the test failed.
1338
1339##### HasFatalFailure {#TestResult::HasFatalFailure}
1340
1341`bool TestResult::HasFatalFailure() const`
1342
1343Returns true if and only if the test fatally failed.
1344
1345##### HasNonfatalFailure {#TestResult::HasNonfatalFailure}
1346
1347`bool TestResult::HasNonfatalFailure() const`
1348
1349Returns true if and only if the test has a non-fatal failure.
1350
1351##### elapsed_time {#TestResult::elapsed_time}
1352
1353`TimeInMillis TestResult::elapsed_time() const`
1354
1355Returns the elapsed time, in milliseconds.
1356
1357##### start_timestamp {#TestResult::start_timestamp}
1358
1359`TimeInMillis TestResult::start_timestamp() const`
1360
1361Gets the time of the test case start, in ms from the start of the UNIX epoch.
1362
1363##### GetTestPartResult {#TestResult::GetTestPartResult}
1364
1365`const TestPartResult& TestResult::GetTestPartResult(int i) const`
1366
1367Returns the [`TestPartResult`](#TestPartResult) for the `i`-th test part result
1368among all the results. `i` can range from 0 to `total_part_count() - 1`. If `i`
1369is not in that range, aborts the program.
1370
1371##### GetTestProperty {#TestResult::GetTestProperty}
1372
1373`const TestProperty& TestResult::GetTestProperty(int i) const`
1374
1375Returns the [`TestProperty`](#TestProperty) object for the `i`-th test property.
1376`i` can range from 0 to `test_property_count() - 1`. If `i` is not in that
1377range, aborts the program.
1378
1379### TimeInMillis {#TimeInMillis}
1380
1381`testing::TimeInMillis`
1382
1383An integer type representing time in milliseconds.
1384
1385### Types {#Types}
1386
1387`testing::Types<T...>`
1388
1389Represents a list of types for use in typed tests and type-parameterized tests.
1390
1391The template argument `T...` can be any number of types, for example:
1392
1393```
1394testing::Types<char, int, unsigned int>
1395```
1396
1397See [Typed Tests](../advanced.md#typed-tests) and
1398[Type-Parameterized Tests](../advanced.md#type-parameterized-tests) for more
1399information.
1400
1401### WithParamInterface {#WithParamInterface}
1402
1403`testing::WithParamInterface<T>`
1404
1405The pure interface class that all value-parameterized tests inherit from.
1406
1407A value-parameterized test fixture class must inherit from both [`Test`](#Test)
1408and `WithParamInterface`. In most cases that just means inheriting from
1409[`TestWithParam`](#TestWithParam), but more complicated test hierarchies may
1410need to inherit from `Test` and `WithParamInterface` at different levels.
1411
1412This interface defines the type alias `ParamType` for the parameter type `T` and
1413has support for accessing the test parameter value via the `GetParam()` method:
1414
1415```
1416static const ParamType& GetParam()
1417```
1418
1419For more information, see
1420[Value-Parameterized Tests](../advanced.md#value-parameterized-tests).
1421
1422## Functions
1423
1424GoogleTest defines the following functions to help with writing and running
1425tests.
1426
1427### InitGoogleTest {#InitGoogleTest}
1428
1429`void testing::InitGoogleTest(int* argc, char** argv)` \
1430`void testing::InitGoogleTest(int* argc, wchar_t** argv)` \
1431`void testing::InitGoogleTest()`
1432
1433Initializes GoogleTest. This must be called before calling
1434[`RUN_ALL_TESTS()`](#RUN_ALL_TESTS). In particular, it parses the command line
1435for the flags that GoogleTest recognizes. Whenever a GoogleTest flag is seen, it
1436is removed from `argv`, and `*argc` is decremented. Keep in mind that `argv`
1437must terminate with a `NULL` pointer (i.e. `argv[argc]` is `NULL`), which is
1438already the case with the default `argv` passed to `main`.
1439
1440No value is returned. Instead, the GoogleTest flag variables are updated.
1441
1442The `InitGoogleTest(int* argc, wchar_t** argv)` overload can be used in Windows
1443programs compiled in `UNICODE` mode.
1444
1445The argument-less `InitGoogleTest()` overload can be used on Arduino/embedded
1446platforms where there is no `argc`/`argv`.
1447
1448### AddGlobalTestEnvironment {#AddGlobalTestEnvironment}
1449
1450`Environment* testing::AddGlobalTestEnvironment(Environment* env)`
1451
1452Adds a test environment to the test program. Must be called before
1453[`RUN_ALL_TESTS()`](#RUN_ALL_TESTS) is called. See
1454[Global Set-Up and Tear-Down](../advanced.md#global-set-up-and-tear-down) for
1455more information.
1456
1457See also [`Environment`](#Environment).
1458
1459### RegisterTest {#RegisterTest}
1460
1461```cpp
1462template <typename Factory>
1463TestInfo* testing::RegisterTest(const char* test_suite_name, const char* test_name,
1464                                  const char* type_param, const char* value_param,
1465                                  const char* file, int line, Factory factory)
1466```
1467
1468Dynamically registers a test with the framework.
1469
1470The `factory` argument is a factory callable (move-constructible) object or
1471function pointer that creates a new instance of the `Test` object. It handles
1472ownership to the caller. The signature of the callable is `Fixture*()`, where
1473`Fixture` is the test fixture class for the test. All tests registered with the
1474same `test_suite_name` must return the same fixture type. This is checked at
1475runtime.
1476
1477The framework will infer the fixture class from the factory and will call the
1478`SetUpTestSuite` and `TearDownTestSuite` methods for it.
1479
1480Must be called before [`RUN_ALL_TESTS()`](#RUN_ALL_TESTS) is invoked, otherwise
1481behavior is undefined.
1482
1483See
1484[Registering tests programmatically](../advanced.md#registering-tests-programmatically)
1485for more information.
1486
1487### RUN_ALL_TESTS {#RUN_ALL_TESTS}
1488
1489`int RUN_ALL_TESTS()`
1490
1491Use this function in `main()` to run all tests. It returns `0` if all tests are
1492successful, or `1` otherwise.
1493
1494`RUN_ALL_TESTS()` should be invoked after the command line has been parsed by
1495[`InitGoogleTest()`](#InitGoogleTest).
1496
1497This function was formerly a macro; thus, it is in the global namespace and has
1498an all-caps name.
1499
1500### AssertionSuccess {#AssertionSuccess}
1501
1502`AssertionResult testing::AssertionSuccess()`
1503
1504Creates a successful assertion result. See
1505[`AssertionResult`](#AssertionResult).
1506
1507### AssertionFailure {#AssertionFailure}
1508
1509`AssertionResult testing::AssertionFailure()`
1510
1511Creates a failed assertion result. Use the `<<` operator to store a failure
1512message:
1513
1514```cpp
1515testing::AssertionFailure() << "My failure message";
1516```
1517
1518See [`AssertionResult`](#AssertionResult).
1519
1520### StaticAssertTypeEq {#StaticAssertTypeEq}
1521
1522`testing::StaticAssertTypeEq<T1, T2>()`
1523
1524Compile-time assertion for type equality. Compiles if and only if `T1` and `T2`
1525are the same type. The value it returns is irrelevant.
1526
1527See [Type Assertions](../advanced.md#type-assertions) for more information.
1528
1529### PrintToString {#PrintToString}
1530
1531`std::string testing::PrintToString(x)`
1532
1533Prints any value `x` using GoogleTest's value printer.
1534
1535See
1536[Teaching GoogleTest How to Print Your Values](../advanced.md#teaching-googletest-how-to-print-your-values)
1537for more information.
1538
1539### PrintToStringParamName {#PrintToStringParamName}
1540
1541`std::string testing::PrintToStringParamName(TestParamInfo<T>& info)`
1542
1543A built-in parameterized test name generator which returns the result of
1544[`PrintToString`](#PrintToString) called on `info.param`. Does not work when the
1545test parameter is a `std::string` or C string. See
1546[Specifying Names for Value-Parameterized Test Parameters](../advanced.md#specifying-names-for-value-parameterized-test-parameters)
1547for more information.
1548
1549See also [`TestParamInfo`](#TestParamInfo) and
1550[`INSTANTIATE_TEST_SUITE_P`](#INSTANTIATE_TEST_SUITE_P).
1551