1f92157deSopenharmony_ci# Googletest Primer
2f92157deSopenharmony_ci
3f92157deSopenharmony_ci## Introduction: Why googletest?
4f92157deSopenharmony_ci
5f92157deSopenharmony_ci*googletest* helps you write better C++ tests.
6f92157deSopenharmony_ci
7f92157deSopenharmony_cigoogletest is a testing framework developed by the Testing Technology team with
8f92157deSopenharmony_ciGoogle's specific requirements and constraints in mind. Whether you work on
9f92157deSopenharmony_ciLinux, Windows, or a Mac, if you write C++ code, googletest can help you. And it
10f92157deSopenharmony_cisupports *any* kind of tests, not just unit tests.
11f92157deSopenharmony_ci
12f92157deSopenharmony_ciSo what makes a good test, and how does googletest fit in? We believe:
13f92157deSopenharmony_ci
14f92157deSopenharmony_ci1.  Tests should be *independent* and *repeatable*. It's a pain to debug a test
15f92157deSopenharmony_ci    that succeeds or fails as a result of other tests. googletest isolates the
16f92157deSopenharmony_ci    tests by running each of them on a different object. When a test fails,
17f92157deSopenharmony_ci    googletest allows you to run it in isolation for quick debugging.
18f92157deSopenharmony_ci2.  Tests should be well *organized* and reflect the structure of the tested
19f92157deSopenharmony_ci    code. googletest groups related tests into test suites that can share data
20f92157deSopenharmony_ci    and subroutines. This common pattern is easy to recognize and makes tests
21f92157deSopenharmony_ci    easy to maintain. Such consistency is especially helpful when people switch
22f92157deSopenharmony_ci    projects and start to work on a new code base.
23f92157deSopenharmony_ci3.  Tests should be *portable* and *reusable*. Google has a lot of code that is
24f92157deSopenharmony_ci    platform-neutral; its tests should also be platform-neutral. googletest
25f92157deSopenharmony_ci    works on different OSes, with different compilers, with or without
26f92157deSopenharmony_ci    exceptions, so googletest tests can work with a variety of configurations.
27f92157deSopenharmony_ci4.  When tests fail, they should provide as much *information* about the problem
28f92157deSopenharmony_ci    as possible. googletest doesn't stop at the first test failure. Instead, it
29f92157deSopenharmony_ci    only stops the current test and continues with the next. You can also set up
30f92157deSopenharmony_ci    tests that report non-fatal failures after which the current test continues.
31f92157deSopenharmony_ci    Thus, you can detect and fix multiple bugs in a single run-edit-compile
32f92157deSopenharmony_ci    cycle.
33f92157deSopenharmony_ci5.  The testing framework should liberate test writers from housekeeping chores
34f92157deSopenharmony_ci    and let them focus on the test *content*. googletest automatically keeps
35f92157deSopenharmony_ci    track of all tests defined, and doesn't require the user to enumerate them
36f92157deSopenharmony_ci    in order to run them.
37f92157deSopenharmony_ci6.  Tests should be *fast*. With googletest, you can reuse shared resources
38f92157deSopenharmony_ci    across tests and pay for the set-up/tear-down only once, without making
39f92157deSopenharmony_ci    tests depend on each other.
40f92157deSopenharmony_ci
41f92157deSopenharmony_ciSince googletest is based on the popular xUnit architecture, you'll feel right
42f92157deSopenharmony_ciat home if you've used JUnit or PyUnit before. If not, it will take you about 10
43f92157deSopenharmony_ciminutes to learn the basics and get started. So let's go!
44f92157deSopenharmony_ci
45f92157deSopenharmony_ci## Beware of the nomenclature
46f92157deSopenharmony_ci
47f92157deSopenharmony_ci{: .callout .note}
48f92157deSopenharmony_ci_Note:_ There might be some confusion arising from different definitions of the
49f92157deSopenharmony_citerms _Test_, _Test Case_ and _Test Suite_, so beware of misunderstanding these.
50f92157deSopenharmony_ci
51f92157deSopenharmony_ciHistorically, googletest started to use the term _Test Case_ for grouping
52f92157deSopenharmony_cirelated tests, whereas current publications, including International Software
53f92157deSopenharmony_ciTesting Qualifications Board ([ISTQB](http://www.istqb.org/)) materials and
54f92157deSopenharmony_civarious textbooks on software quality, use the term
55f92157deSopenharmony_ci_[Test Suite][istqb test suite]_ for this.
56f92157deSopenharmony_ci
57f92157deSopenharmony_ciThe related term _Test_, as it is used in googletest, corresponds to the term
58f92157deSopenharmony_ci_[Test Case][istqb test case]_ of ISTQB and others.
59f92157deSopenharmony_ci
60f92157deSopenharmony_ciThe term _Test_ is commonly of broad enough sense, including ISTQB's definition
61f92157deSopenharmony_ciof _Test Case_, so it's not much of a problem here. But the term _Test Case_ as
62f92157deSopenharmony_ciwas used in Google Test is of contradictory sense and thus confusing.
63f92157deSopenharmony_ci
64f92157deSopenharmony_cigoogletest recently started replacing the term _Test Case_ with _Test Suite_.
65f92157deSopenharmony_ciThe preferred API is *TestSuite*. The older TestCase API is being slowly
66f92157deSopenharmony_cideprecated and refactored away.
67f92157deSopenharmony_ci
68f92157deSopenharmony_ciSo please be aware of the different definitions of the terms:
69f92157deSopenharmony_ci
70f92157deSopenharmony_ci
71f92157deSopenharmony_ciMeaning                                                                              | googletest Term         | [ISTQB](http://www.istqb.org/) Term
72f92157deSopenharmony_ci:----------------------------------------------------------------------------------- | :---------------------- | :----------------------------------
73f92157deSopenharmony_ciExercise a particular program path with specific input values and verify the results | [TEST()](#simple-tests) | [Test Case][istqb test case]
74f92157deSopenharmony_ci
75f92157deSopenharmony_ci
76f92157deSopenharmony_ci[istqb test case]: http://glossary.istqb.org/en/search/test%20case
77f92157deSopenharmony_ci[istqb test suite]: http://glossary.istqb.org/en/search/test%20suite
78f92157deSopenharmony_ci
79f92157deSopenharmony_ci## Basic Concepts
80f92157deSopenharmony_ci
81f92157deSopenharmony_ciWhen using googletest, you start by writing *assertions*, which are statements
82f92157deSopenharmony_cithat check whether a condition is true. An assertion's result can be *success*,
83f92157deSopenharmony_ci*nonfatal failure*, or *fatal failure*. If a fatal failure occurs, it aborts the
84f92157deSopenharmony_cicurrent function; otherwise the program continues normally.
85f92157deSopenharmony_ci
86f92157deSopenharmony_ci*Tests* use assertions to verify the tested code's behavior. If a test crashes
87f92157deSopenharmony_cior has a failed assertion, then it *fails*; otherwise it *succeeds*.
88f92157deSopenharmony_ci
89f92157deSopenharmony_ciA *test suite* contains one or many tests. You should group your tests into test
90f92157deSopenharmony_cisuites that reflect the structure of the tested code. When multiple tests in a
91f92157deSopenharmony_citest suite need to share common objects and subroutines, you can put them into a
92f92157deSopenharmony_ci*test fixture* class.
93f92157deSopenharmony_ci
94f92157deSopenharmony_ciA *test program* can contain multiple test suites.
95f92157deSopenharmony_ci
96f92157deSopenharmony_ciWe'll now explain how to write a test program, starting at the individual
97f92157deSopenharmony_ciassertion level and building up to tests and test suites.
98f92157deSopenharmony_ci
99f92157deSopenharmony_ci## Assertions
100f92157deSopenharmony_ci
101f92157deSopenharmony_cigoogletest assertions are macros that resemble function calls. You test a class
102f92157deSopenharmony_cior function by making assertions about its behavior. When an assertion fails,
103f92157deSopenharmony_cigoogletest prints the assertion's source file and line number location, along
104f92157deSopenharmony_ciwith a failure message. You may also supply a custom failure message which will
105f92157deSopenharmony_cibe appended to googletest's message.
106f92157deSopenharmony_ci
107f92157deSopenharmony_ciThe assertions come in pairs that test the same thing but have different effects
108f92157deSopenharmony_cion the current function. `ASSERT_*` versions generate fatal failures when they
109f92157deSopenharmony_cifail, and **abort the current function**. `EXPECT_*` versions generate nonfatal
110f92157deSopenharmony_cifailures, which don't abort the current function. Usually `EXPECT_*` are
111f92157deSopenharmony_cipreferred, as they allow more than one failure to be reported in a test.
112f92157deSopenharmony_ciHowever, you should use `ASSERT_*` if it doesn't make sense to continue when the
113f92157deSopenharmony_ciassertion in question fails.
114f92157deSopenharmony_ci
115f92157deSopenharmony_ciSince a failed `ASSERT_*` returns from the current function immediately,
116f92157deSopenharmony_cipossibly skipping clean-up code that comes after it, it may cause a space leak.
117f92157deSopenharmony_ciDepending on the nature of the leak, it may or may not be worth fixing - so keep
118f92157deSopenharmony_cithis in mind if you get a heap checker error in addition to assertion errors.
119f92157deSopenharmony_ci
120f92157deSopenharmony_ciTo provide a custom failure message, simply stream it into the macro using the
121f92157deSopenharmony_ci`<<` operator or a sequence of such operators. See the following example, using
122f92157deSopenharmony_cithe [`ASSERT_EQ` and `EXPECT_EQ`](reference/assertions.md#EXPECT_EQ) macros to
123f92157deSopenharmony_civerify value equality:
124f92157deSopenharmony_ci
125f92157deSopenharmony_ci```c++
126f92157deSopenharmony_ciASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length";
127f92157deSopenharmony_ci
128f92157deSopenharmony_cifor (int i = 0; i < x.size(); ++i) {
129f92157deSopenharmony_ci  EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;
130f92157deSopenharmony_ci}
131f92157deSopenharmony_ci```
132f92157deSopenharmony_ci
133f92157deSopenharmony_ciAnything that can be streamed to an `ostream` can be streamed to an assertion
134f92157deSopenharmony_cimacro--in particular, C strings and `string` objects. If a wide string
135f92157deSopenharmony_ci(`wchar_t*`, `TCHAR*` in `UNICODE` mode on Windows, or `std::wstring`) is
136f92157deSopenharmony_cistreamed to an assertion, it will be translated to UTF-8 when printed.
137f92157deSopenharmony_ci
138f92157deSopenharmony_ciGoogleTest provides a collection of assertions for verifying the behavior of
139f92157deSopenharmony_ciyour code in various ways. You can check Boolean conditions, compare values
140f92157deSopenharmony_cibased on relational operators, verify string values, floating-point values, and
141f92157deSopenharmony_cimuch more. There are even assertions that enable you to verify more complex
142f92157deSopenharmony_cistates by providing custom predicates. For the complete list of assertions
143f92157deSopenharmony_ciprovided by GoogleTest, see the [Assertions Reference](reference/assertions.md).
144f92157deSopenharmony_ci
145f92157deSopenharmony_ci## Simple Tests
146f92157deSopenharmony_ci
147f92157deSopenharmony_ciTo create a test:
148f92157deSopenharmony_ci
149f92157deSopenharmony_ci1.  Use the `TEST()` macro to define and name a test function. These are
150f92157deSopenharmony_ci    ordinary C++ functions that don't return a value.
151f92157deSopenharmony_ci2.  In this function, along with any valid C++ statements you want to include,
152f92157deSopenharmony_ci    use the various googletest assertions to check values.
153f92157deSopenharmony_ci3.  The test's result is determined by the assertions; if any assertion in the
154f92157deSopenharmony_ci    test fails (either fatally or non-fatally), or if the test crashes, the
155f92157deSopenharmony_ci    entire test fails. Otherwise, it succeeds.
156f92157deSopenharmony_ci
157f92157deSopenharmony_ci```c++
158f92157deSopenharmony_ciTEST(TestSuiteName, TestName) {
159f92157deSopenharmony_ci  ... test body ...
160f92157deSopenharmony_ci}
161f92157deSopenharmony_ci```
162f92157deSopenharmony_ci
163f92157deSopenharmony_ci`TEST()` arguments go from general to specific. The *first* argument is the name
164f92157deSopenharmony_ciof the test suite, and the *second* argument is the test's name within the test
165f92157deSopenharmony_cisuite. Both names must be valid C++ identifiers, and they should not contain any
166f92157deSopenharmony_ciunderscores (`_`). A test's *full name* consists of its containing test suite
167f92157deSopenharmony_ciand its individual name. Tests from different test suites can have the same
168f92157deSopenharmony_ciindividual name.
169f92157deSopenharmony_ci
170f92157deSopenharmony_ciFor example, let's take a simple integer function:
171f92157deSopenharmony_ci
172f92157deSopenharmony_ci```c++
173f92157deSopenharmony_ciint Factorial(int n);  // Returns the factorial of n
174f92157deSopenharmony_ci```
175f92157deSopenharmony_ci
176f92157deSopenharmony_ciA test suite for this function might look like:
177f92157deSopenharmony_ci
178f92157deSopenharmony_ci```c++
179f92157deSopenharmony_ci// Tests factorial of 0.
180f92157deSopenharmony_ciTEST(FactorialTest, HandlesZeroInput) {
181f92157deSopenharmony_ci  EXPECT_EQ(Factorial(0), 1);
182f92157deSopenharmony_ci}
183f92157deSopenharmony_ci
184f92157deSopenharmony_ci// Tests factorial of positive numbers.
185f92157deSopenharmony_ciTEST(FactorialTest, HandlesPositiveInput) {
186f92157deSopenharmony_ci  EXPECT_EQ(Factorial(1), 1);
187f92157deSopenharmony_ci  EXPECT_EQ(Factorial(2), 2);
188f92157deSopenharmony_ci  EXPECT_EQ(Factorial(3), 6);
189f92157deSopenharmony_ci  EXPECT_EQ(Factorial(8), 40320);
190f92157deSopenharmony_ci}
191f92157deSopenharmony_ci```
192f92157deSopenharmony_ci
193f92157deSopenharmony_cigoogletest groups the test results by test suites, so logically related tests
194f92157deSopenharmony_cishould be in the same test suite; in other words, the first argument to their
195f92157deSopenharmony_ci`TEST()` should be the same. In the above example, we have two tests,
196f92157deSopenharmony_ci`HandlesZeroInput` and `HandlesPositiveInput`, that belong to the same test
197f92157deSopenharmony_cisuite `FactorialTest`.
198f92157deSopenharmony_ci
199f92157deSopenharmony_ciWhen naming your test suites and tests, you should follow the same convention as
200f92157deSopenharmony_cifor
201f92157deSopenharmony_ci[naming functions and classes](https://google.github.io/styleguide/cppguide.html#Function_Names).
202f92157deSopenharmony_ci
203f92157deSopenharmony_ci**Availability**: Linux, Windows, Mac.
204f92157deSopenharmony_ci
205f92157deSopenharmony_ci## Test Fixtures: Using the Same Data Configuration for Multiple Tests {#same-data-multiple-tests}
206f92157deSopenharmony_ci
207f92157deSopenharmony_ciIf you find yourself writing two or more tests that operate on similar data, you
208f92157deSopenharmony_cican use a *test fixture*. This allows you to reuse the same configuration of
209f92157deSopenharmony_ciobjects for several different tests.
210f92157deSopenharmony_ci
211f92157deSopenharmony_ciTo create a fixture:
212f92157deSopenharmony_ci
213f92157deSopenharmony_ci1.  Derive a class from `::testing::Test` . Start its body with `protected:`, as
214f92157deSopenharmony_ci    we'll want to access fixture members from sub-classes.
215f92157deSopenharmony_ci2.  Inside the class, declare any objects you plan to use.
216f92157deSopenharmony_ci3.  If necessary, write a default constructor or `SetUp()` function to prepare
217f92157deSopenharmony_ci    the objects for each test. A common mistake is to spell `SetUp()` as
218f92157deSopenharmony_ci    **`Setup()`** with a small `u` - Use `override` in C++11 to make sure you
219f92157deSopenharmony_ci    spelled it correctly.
220f92157deSopenharmony_ci4.  If necessary, write a destructor or `TearDown()` function to release any
221f92157deSopenharmony_ci    resources you allocated in `SetUp()` . To learn when you should use the
222f92157deSopenharmony_ci    constructor/destructor and when you should use `SetUp()/TearDown()`, read
223f92157deSopenharmony_ci    the [FAQ](faq.md#CtorVsSetUp).
224f92157deSopenharmony_ci5.  If needed, define subroutines for your tests to share.
225f92157deSopenharmony_ci
226f92157deSopenharmony_ciWhen using a fixture, use `TEST_F()` instead of `TEST()` as it allows you to
227f92157deSopenharmony_ciaccess objects and subroutines in the test fixture:
228f92157deSopenharmony_ci
229f92157deSopenharmony_ci```c++
230f92157deSopenharmony_ciTEST_F(TestFixtureName, TestName) {
231f92157deSopenharmony_ci  ... test body ...
232f92157deSopenharmony_ci}
233f92157deSopenharmony_ci```
234f92157deSopenharmony_ci
235f92157deSopenharmony_ciLike `TEST()`, the first argument is the test suite name, but for `TEST_F()`
236f92157deSopenharmony_cithis must be the name of the test fixture class. You've probably guessed: `_F`
237f92157deSopenharmony_ciis for fixture.
238f92157deSopenharmony_ci
239f92157deSopenharmony_ciUnfortunately, the C++ macro system does not allow us to create a single macro
240f92157deSopenharmony_cithat can handle both types of tests. Using the wrong macro causes a compiler
241f92157deSopenharmony_cierror.
242f92157deSopenharmony_ci
243f92157deSopenharmony_ciAlso, you must first define a test fixture class before using it in a
244f92157deSopenharmony_ci`TEST_F()`, or you'll get the compiler error "`virtual outside class
245f92157deSopenharmony_cideclaration`".
246f92157deSopenharmony_ci
247f92157deSopenharmony_ciFor each test defined with `TEST_F()`, googletest will create a *fresh* test
248f92157deSopenharmony_cifixture at runtime, immediately initialize it via `SetUp()`, run the test, clean
249f92157deSopenharmony_ciup by calling `TearDown()`, and then delete the test fixture. Note that
250f92157deSopenharmony_cidifferent tests in the same test suite have different test fixture objects, and
251f92157deSopenharmony_cigoogletest always deletes a test fixture before it creates the next one.
252f92157deSopenharmony_cigoogletest does **not** reuse the same test fixture for multiple tests. Any
253f92157deSopenharmony_cichanges one test makes to the fixture do not affect other tests.
254f92157deSopenharmony_ci
255f92157deSopenharmony_ciAs an example, let's write tests for a FIFO queue class named `Queue`, which has
256f92157deSopenharmony_cithe following interface:
257f92157deSopenharmony_ci
258f92157deSopenharmony_ci```c++
259f92157deSopenharmony_citemplate <typename E>  // E is the element type.
260f92157deSopenharmony_ciclass Queue {
261f92157deSopenharmony_ci public:
262f92157deSopenharmony_ci  Queue();
263f92157deSopenharmony_ci  void Enqueue(const E& element);
264f92157deSopenharmony_ci  E* Dequeue();  // Returns NULL if the queue is empty.
265f92157deSopenharmony_ci  size_t size() const;
266f92157deSopenharmony_ci  ...
267f92157deSopenharmony_ci};
268f92157deSopenharmony_ci```
269f92157deSopenharmony_ci
270f92157deSopenharmony_ciFirst, define a fixture class. By convention, you should give it the name
271f92157deSopenharmony_ci`FooTest` where `Foo` is the class being tested.
272f92157deSopenharmony_ci
273f92157deSopenharmony_ci```c++
274f92157deSopenharmony_ciclass QueueTest : public ::testing::Test {
275f92157deSopenharmony_ci protected:
276f92157deSopenharmony_ci  void SetUp() override {
277f92157deSopenharmony_ci     // q0_ remains empty
278f92157deSopenharmony_ci     q1_.Enqueue(1);
279f92157deSopenharmony_ci     q2_.Enqueue(2);
280f92157deSopenharmony_ci     q2_.Enqueue(3);
281f92157deSopenharmony_ci  }
282f92157deSopenharmony_ci
283f92157deSopenharmony_ci  // void TearDown() override {}
284f92157deSopenharmony_ci
285f92157deSopenharmony_ci  Queue<int> q0_;
286f92157deSopenharmony_ci  Queue<int> q1_;
287f92157deSopenharmony_ci  Queue<int> q2_;
288f92157deSopenharmony_ci};
289f92157deSopenharmony_ci```
290f92157deSopenharmony_ci
291f92157deSopenharmony_ciIn this case, `TearDown()` is not needed since we don't have to clean up after
292f92157deSopenharmony_cieach test, other than what's already done by the destructor.
293f92157deSopenharmony_ci
294f92157deSopenharmony_ciNow we'll write tests using `TEST_F()` and this fixture.
295f92157deSopenharmony_ci
296f92157deSopenharmony_ci```c++
297f92157deSopenharmony_ciTEST_F(QueueTest, IsEmptyInitially) {
298f92157deSopenharmony_ci  EXPECT_EQ(q0_.size(), 0);
299f92157deSopenharmony_ci}
300f92157deSopenharmony_ci
301f92157deSopenharmony_ciTEST_F(QueueTest, DequeueWorks) {
302f92157deSopenharmony_ci  int* n = q0_.Dequeue();
303f92157deSopenharmony_ci  EXPECT_EQ(n, nullptr);
304f92157deSopenharmony_ci
305f92157deSopenharmony_ci  n = q1_.Dequeue();
306f92157deSopenharmony_ci  ASSERT_NE(n, nullptr);
307f92157deSopenharmony_ci  EXPECT_EQ(*n, 1);
308f92157deSopenharmony_ci  EXPECT_EQ(q1_.size(), 0);
309f92157deSopenharmony_ci  delete n;
310f92157deSopenharmony_ci
311f92157deSopenharmony_ci  n = q2_.Dequeue();
312f92157deSopenharmony_ci  ASSERT_NE(n, nullptr);
313f92157deSopenharmony_ci  EXPECT_EQ(*n, 2);
314f92157deSopenharmony_ci  EXPECT_EQ(q2_.size(), 1);
315f92157deSopenharmony_ci  delete n;
316f92157deSopenharmony_ci}
317f92157deSopenharmony_ci```
318f92157deSopenharmony_ci
319f92157deSopenharmony_ciThe above uses both `ASSERT_*` and `EXPECT_*` assertions. The rule of thumb is
320f92157deSopenharmony_cito use `EXPECT_*` when you want the test to continue to reveal more errors after
321f92157deSopenharmony_cithe assertion failure, and use `ASSERT_*` when continuing after failure doesn't
322f92157deSopenharmony_cimake sense. For example, the second assertion in the `Dequeue` test is
323f92157deSopenharmony_ci`ASSERT_NE(n, nullptr)`, as we need to dereference the pointer `n` later, which
324f92157deSopenharmony_ciwould lead to a segfault when `n` is `NULL`.
325f92157deSopenharmony_ci
326f92157deSopenharmony_ciWhen these tests run, the following happens:
327f92157deSopenharmony_ci
328f92157deSopenharmony_ci1.  googletest constructs a `QueueTest` object (let's call it `t1`).
329f92157deSopenharmony_ci2.  `t1.SetUp()` initializes `t1`.
330f92157deSopenharmony_ci3.  The first test (`IsEmptyInitially`) runs on `t1`.
331f92157deSopenharmony_ci4.  `t1.TearDown()` cleans up after the test finishes.
332f92157deSopenharmony_ci5.  `t1` is destructed.
333f92157deSopenharmony_ci6.  The above steps are repeated on another `QueueTest` object, this time
334f92157deSopenharmony_ci    running the `DequeueWorks` test.
335f92157deSopenharmony_ci
336f92157deSopenharmony_ci**Availability**: Linux, Windows, Mac.
337f92157deSopenharmony_ci
338f92157deSopenharmony_ci## Invoking the Tests
339f92157deSopenharmony_ci
340f92157deSopenharmony_ci`TEST()` and `TEST_F()` implicitly register their tests with googletest. So,
341f92157deSopenharmony_ciunlike with many other C++ testing frameworks, you don't have to re-list all
342f92157deSopenharmony_ciyour defined tests in order to run them.
343f92157deSopenharmony_ci
344f92157deSopenharmony_ciAfter defining your tests, you can run them with `RUN_ALL_TESTS()`, which
345f92157deSopenharmony_cireturns `0` if all the tests are successful, or `1` otherwise. Note that
346f92157deSopenharmony_ci`RUN_ALL_TESTS()` runs *all tests* in your link unit--they can be from different
347f92157deSopenharmony_citest suites, or even different source files.
348f92157deSopenharmony_ci
349f92157deSopenharmony_ciWhen invoked, the `RUN_ALL_TESTS()` macro:
350f92157deSopenharmony_ci
351f92157deSopenharmony_ci*   Saves the state of all googletest flags.
352f92157deSopenharmony_ci
353f92157deSopenharmony_ci*   Creates a test fixture object for the first test.
354f92157deSopenharmony_ci
355f92157deSopenharmony_ci*   Initializes it via `SetUp()`.
356f92157deSopenharmony_ci
357f92157deSopenharmony_ci*   Runs the test on the fixture object.
358f92157deSopenharmony_ci
359f92157deSopenharmony_ci*   Cleans up the fixture via `TearDown()`.
360f92157deSopenharmony_ci
361f92157deSopenharmony_ci*   Deletes the fixture.
362f92157deSopenharmony_ci
363f92157deSopenharmony_ci*   Restores the state of all googletest flags.
364f92157deSopenharmony_ci
365f92157deSopenharmony_ci*   Repeats the above steps for the next test, until all tests have run.
366f92157deSopenharmony_ci
367f92157deSopenharmony_ciIf a fatal failure happens the subsequent steps will be skipped.
368f92157deSopenharmony_ci
369f92157deSopenharmony_ci{: .callout .important}
370f92157deSopenharmony_ci> IMPORTANT: You must **not** ignore the return value of `RUN_ALL_TESTS()`, or
371f92157deSopenharmony_ci> you will get a compiler error. The rationale for this design is that the
372f92157deSopenharmony_ci> automated testing service determines whether a test has passed based on its
373f92157deSopenharmony_ci> exit code, not on its stdout/stderr output; thus your `main()` function must
374f92157deSopenharmony_ci> return the value of `RUN_ALL_TESTS()`.
375f92157deSopenharmony_ci>
376f92157deSopenharmony_ci> Also, you should call `RUN_ALL_TESTS()` only **once**. Calling it more than
377f92157deSopenharmony_ci> once conflicts with some advanced googletest features (e.g., thread-safe
378f92157deSopenharmony_ci> [death tests](advanced.md#death-tests)) and thus is not supported.
379f92157deSopenharmony_ci
380f92157deSopenharmony_ci**Availability**: Linux, Windows, Mac.
381f92157deSopenharmony_ci
382f92157deSopenharmony_ci## Writing the main() Function
383f92157deSopenharmony_ci
384f92157deSopenharmony_ciMost users should _not_ need to write their own `main` function and instead link
385f92157deSopenharmony_ciwith `gtest_main` (as opposed to with `gtest`), which defines a suitable entry
386f92157deSopenharmony_cipoint. See the end of this section for details. The remainder of this section
387f92157deSopenharmony_cishould only apply when you need to do something custom before the tests run that
388f92157deSopenharmony_cicannot be expressed within the framework of fixtures and test suites.
389f92157deSopenharmony_ci
390f92157deSopenharmony_ciIf you write your own `main` function, it should return the value of
391f92157deSopenharmony_ci`RUN_ALL_TESTS()`.
392f92157deSopenharmony_ci
393f92157deSopenharmony_ciYou can start from this boilerplate:
394f92157deSopenharmony_ci
395f92157deSopenharmony_ci```c++
396f92157deSopenharmony_ci#include "this/package/foo.h"
397f92157deSopenharmony_ci
398f92157deSopenharmony_ci#include "gtest/gtest.h"
399f92157deSopenharmony_ci
400f92157deSopenharmony_cinamespace my {
401f92157deSopenharmony_cinamespace project {
402f92157deSopenharmony_cinamespace {
403f92157deSopenharmony_ci
404f92157deSopenharmony_ci// The fixture for testing class Foo.
405f92157deSopenharmony_ciclass FooTest : public ::testing::Test {
406f92157deSopenharmony_ci protected:
407f92157deSopenharmony_ci  // You can remove any or all of the following functions if their bodies would
408f92157deSopenharmony_ci  // be empty.
409f92157deSopenharmony_ci
410f92157deSopenharmony_ci  FooTest() {
411f92157deSopenharmony_ci     // You can do set-up work for each test here.
412f92157deSopenharmony_ci  }
413f92157deSopenharmony_ci
414f92157deSopenharmony_ci  ~FooTest() override {
415f92157deSopenharmony_ci     // You can do clean-up work that doesn't throw exceptions here.
416f92157deSopenharmony_ci  }
417f92157deSopenharmony_ci
418f92157deSopenharmony_ci  // If the constructor and destructor are not enough for setting up
419f92157deSopenharmony_ci  // and cleaning up each test, you can define the following methods:
420f92157deSopenharmony_ci
421f92157deSopenharmony_ci  void SetUp() override {
422f92157deSopenharmony_ci     // Code here will be called immediately after the constructor (right
423f92157deSopenharmony_ci     // before each test).
424f92157deSopenharmony_ci  }
425f92157deSopenharmony_ci
426f92157deSopenharmony_ci  void TearDown() override {
427f92157deSopenharmony_ci     // Code here will be called immediately after each test (right
428f92157deSopenharmony_ci     // before the destructor).
429f92157deSopenharmony_ci  }
430f92157deSopenharmony_ci
431f92157deSopenharmony_ci  // Class members declared here can be used by all tests in the test suite
432f92157deSopenharmony_ci  // for Foo.
433f92157deSopenharmony_ci};
434f92157deSopenharmony_ci
435f92157deSopenharmony_ci// Tests that the Foo::Bar() method does Abc.
436f92157deSopenharmony_ciTEST_F(FooTest, MethodBarDoesAbc) {
437f92157deSopenharmony_ci  const std::string input_filepath = "this/package/testdata/myinputfile.dat";
438f92157deSopenharmony_ci  const std::string output_filepath = "this/package/testdata/myoutputfile.dat";
439f92157deSopenharmony_ci  Foo f;
440f92157deSopenharmony_ci  EXPECT_EQ(f.Bar(input_filepath, output_filepath), 0);
441f92157deSopenharmony_ci}
442f92157deSopenharmony_ci
443f92157deSopenharmony_ci// Tests that Foo does Xyz.
444f92157deSopenharmony_ciTEST_F(FooTest, DoesXyz) {
445f92157deSopenharmony_ci  // Exercises the Xyz feature of Foo.
446f92157deSopenharmony_ci}
447f92157deSopenharmony_ci
448f92157deSopenharmony_ci}  // namespace
449f92157deSopenharmony_ci}  // namespace project
450f92157deSopenharmony_ci}  // namespace my
451f92157deSopenharmony_ci
452f92157deSopenharmony_ciint main(int argc, char **argv) {
453f92157deSopenharmony_ci  ::testing::InitGoogleTest(&argc, argv);
454f92157deSopenharmony_ci  return RUN_ALL_TESTS();
455f92157deSopenharmony_ci}
456f92157deSopenharmony_ci```
457f92157deSopenharmony_ci
458f92157deSopenharmony_ciThe `::testing::InitGoogleTest()` function parses the command line for
459f92157deSopenharmony_cigoogletest flags, and removes all recognized flags. This allows the user to
460f92157deSopenharmony_cicontrol a test program's behavior via various flags, which we'll cover in the
461f92157deSopenharmony_ci[AdvancedGuide](advanced.md). You **must** call this function before calling
462f92157deSopenharmony_ci`RUN_ALL_TESTS()`, or the flags won't be properly initialized.
463f92157deSopenharmony_ci
464f92157deSopenharmony_ciOn Windows, `InitGoogleTest()` also works with wide strings, so it can be used
465f92157deSopenharmony_ciin programs compiled in `UNICODE` mode as well.
466f92157deSopenharmony_ci
467f92157deSopenharmony_ciBut maybe you think that writing all those `main` functions is too much work? We
468f92157deSopenharmony_ciagree with you completely, and that's why Google Test provides a basic
469f92157deSopenharmony_ciimplementation of main(). If it fits your needs, then just link your test with
470f92157deSopenharmony_cithe `gtest_main` library and you are good to go.
471f92157deSopenharmony_ci
472f92157deSopenharmony_ci{: .callout .note}
473f92157deSopenharmony_ciNOTE: `ParseGUnitFlags()` is deprecated in favor of `InitGoogleTest()`.
474f92157deSopenharmony_ci
475f92157deSopenharmony_ci## Known Limitations
476f92157deSopenharmony_ci
477f92157deSopenharmony_ci*   Google Test is designed to be thread-safe. The implementation is thread-safe
478f92157deSopenharmony_ci    on systems where the `pthreads` library is available. It is currently
479f92157deSopenharmony_ci    _unsafe_ to use Google Test assertions from two threads concurrently on
480f92157deSopenharmony_ci    other systems (e.g. Windows). In most tests this is not an issue as usually
481f92157deSopenharmony_ci    the assertions are done in the main thread. If you want to help, you can
482f92157deSopenharmony_ci    volunteer to implement the necessary synchronization primitives in
483f92157deSopenharmony_ci    `gtest-port.h` for your platform.
484