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