128f6c2f2SEnji Cooper# GoogleTest FAQ 228f6c2f2SEnji Cooper 328f6c2f2SEnji Cooper## Why should test suite names and test names not contain underscore? 428f6c2f2SEnji Cooper 528f6c2f2SEnji Cooper{: .callout .note} 6*5ca8c28cSEnji CooperNote: GoogleTest reserves underscore (`_`) for special-purpose keywords, such as 728f6c2f2SEnji Cooper[the `DISABLED_` prefix](advanced.md#temporarily-disabling-tests), in addition 828f6c2f2SEnji Cooperto the following rationale. 928f6c2f2SEnji Cooper 1028f6c2f2SEnji CooperUnderscore (`_`) is special, as C++ reserves the following to be used by the 1128f6c2f2SEnji Coopercompiler and the standard library: 1228f6c2f2SEnji Cooper 1328f6c2f2SEnji Cooper1. any identifier that starts with an `_` followed by an upper-case letter, and 1428f6c2f2SEnji Cooper2. any identifier that contains two consecutive underscores (i.e. `__`) 1528f6c2f2SEnji Cooper *anywhere* in its name. 1628f6c2f2SEnji Cooper 1728f6c2f2SEnji CooperUser code is *prohibited* from using such identifiers. 1828f6c2f2SEnji Cooper 1928f6c2f2SEnji CooperNow let's look at what this means for `TEST` and `TEST_F`. 2028f6c2f2SEnji Cooper 2128f6c2f2SEnji CooperCurrently `TEST(TestSuiteName, TestName)` generates a class named 2228f6c2f2SEnji Cooper`TestSuiteName_TestName_Test`. What happens if `TestSuiteName` or `TestName` 2328f6c2f2SEnji Coopercontains `_`? 2428f6c2f2SEnji Cooper 2528f6c2f2SEnji Cooper1. If `TestSuiteName` starts with an `_` followed by an upper-case letter (say, 2628f6c2f2SEnji Cooper `_Foo`), we end up with `_Foo_TestName_Test`, which is reserved and thus 2728f6c2f2SEnji Cooper invalid. 2828f6c2f2SEnji Cooper2. If `TestSuiteName` ends with an `_` (say, `Foo_`), we get 2928f6c2f2SEnji Cooper `Foo__TestName_Test`, which is invalid. 3028f6c2f2SEnji Cooper3. If `TestName` starts with an `_` (say, `_Bar`), we get 3128f6c2f2SEnji Cooper `TestSuiteName__Bar_Test`, which is invalid. 3228f6c2f2SEnji Cooper4. If `TestName` ends with an `_` (say, `Bar_`), we get 3328f6c2f2SEnji Cooper `TestSuiteName_Bar__Test`, which is invalid. 3428f6c2f2SEnji Cooper 3528f6c2f2SEnji CooperSo clearly `TestSuiteName` and `TestName` cannot start or end with `_` 36*5ca8c28cSEnji Cooper(Actually, `TestSuiteName` can start with `_`—as long as the `_` isn't followed 37*5ca8c28cSEnji Cooperby an upper-case letter. But that's getting complicated. So for simplicity we 38*5ca8c28cSEnji Cooperjust say that it cannot start with `_`.). 3928f6c2f2SEnji Cooper 4028f6c2f2SEnji CooperIt may seem fine for `TestSuiteName` and `TestName` to contain `_` in the 4128f6c2f2SEnji Coopermiddle. However, consider this: 4228f6c2f2SEnji Cooper 4328f6c2f2SEnji Cooper```c++ 4428f6c2f2SEnji CooperTEST(Time, Flies_Like_An_Arrow) { ... } 4528f6c2f2SEnji CooperTEST(Time_Flies, Like_An_Arrow) { ... } 4628f6c2f2SEnji Cooper``` 4728f6c2f2SEnji Cooper 4828f6c2f2SEnji CooperNow, the two `TEST`s will both generate the same class 4928f6c2f2SEnji Cooper(`Time_Flies_Like_An_Arrow_Test`). That's not good. 5028f6c2f2SEnji Cooper 5128f6c2f2SEnji CooperSo for simplicity, we just ask the users to avoid `_` in `TestSuiteName` and 5228f6c2f2SEnji Cooper`TestName`. The rule is more constraining than necessary, but it's simple and 5328f6c2f2SEnji Coopereasy to remember. It also gives GoogleTest some wiggle room in case its 5428f6c2f2SEnji Cooperimplementation needs to change in the future. 5528f6c2f2SEnji Cooper 5628f6c2f2SEnji CooperIf you violate the rule, there may not be immediate consequences, but your test 5728f6c2f2SEnji Coopermay (just may) break with a new compiler (or a new version of the compiler you 5828f6c2f2SEnji Cooperare using) or with a new version of GoogleTest. Therefore it's best to follow 5928f6c2f2SEnji Cooperthe rule. 6028f6c2f2SEnji Cooper 6128f6c2f2SEnji Cooper## Why does GoogleTest support `EXPECT_EQ(NULL, ptr)` and `ASSERT_EQ(NULL, ptr)` but not `EXPECT_NE(NULL, ptr)` and `ASSERT_NE(NULL, ptr)`? 6228f6c2f2SEnji Cooper 6328f6c2f2SEnji CooperFirst of all, you can use `nullptr` with each of these macros, e.g. 6428f6c2f2SEnji Cooper`EXPECT_EQ(ptr, nullptr)`, `EXPECT_NE(ptr, nullptr)`, `ASSERT_EQ(ptr, nullptr)`, 6528f6c2f2SEnji Cooper`ASSERT_NE(ptr, nullptr)`. This is the preferred syntax in the style guide 6628f6c2f2SEnji Cooperbecause `nullptr` does not have the type problems that `NULL` does. 6728f6c2f2SEnji Cooper 6828f6c2f2SEnji CooperDue to some peculiarity of C++, it requires some non-trivial template meta 6928f6c2f2SEnji Cooperprogramming tricks to support using `NULL` as an argument of the `EXPECT_XX()` 7028f6c2f2SEnji Cooperand `ASSERT_XX()` macros. Therefore we only do it where it's most needed 7128f6c2f2SEnji Cooper(otherwise we make the implementation of GoogleTest harder to maintain and more 7228f6c2f2SEnji Coopererror-prone than necessary). 7328f6c2f2SEnji Cooper 7428f6c2f2SEnji CooperHistorically, the `EXPECT_EQ()` macro took the *expected* value as its first 7528f6c2f2SEnji Cooperargument and the *actual* value as the second, though this argument order is now 7628f6c2f2SEnji Cooperdiscouraged. It was reasonable that someone wanted 7728f6c2f2SEnji Cooperto write `EXPECT_EQ(NULL, some_expression)`, and this indeed was requested 7828f6c2f2SEnji Cooperseveral times. Therefore we implemented it. 7928f6c2f2SEnji Cooper 8028f6c2f2SEnji CooperThe need for `EXPECT_NE(NULL, ptr)` wasn't nearly as strong. When the assertion 8128f6c2f2SEnji Cooperfails, you already know that `ptr` must be `NULL`, so it doesn't add any 8228f6c2f2SEnji Cooperinformation to print `ptr` in this case. That means `EXPECT_TRUE(ptr != NULL)` 8328f6c2f2SEnji Cooperworks just as well. 8428f6c2f2SEnji Cooper 8528f6c2f2SEnji CooperIf we were to support `EXPECT_NE(NULL, ptr)`, for consistency we'd have to 8628f6c2f2SEnji Coopersupport `EXPECT_NE(ptr, NULL)` as well. This means using the template meta 8728f6c2f2SEnji Cooperprogramming tricks twice in the implementation, making it even harder to 8828f6c2f2SEnji Cooperunderstand and maintain. We believe the benefit doesn't justify the cost. 8928f6c2f2SEnji Cooper 9028f6c2f2SEnji CooperFinally, with the growth of the gMock matcher library, we are encouraging people 9128f6c2f2SEnji Cooperto use the unified `EXPECT_THAT(value, matcher)` syntax more often in tests. One 9228f6c2f2SEnji Coopersignificant advantage of the matcher approach is that matchers can be easily 9328f6c2f2SEnji Coopercombined to form new matchers, while the `EXPECT_NE`, etc, macros cannot be 9428f6c2f2SEnji Coopereasily combined. Therefore we want to invest more in the matchers than in the 9528f6c2f2SEnji Cooper`EXPECT_XX()` macros. 9628f6c2f2SEnji Cooper 9728f6c2f2SEnji Cooper## I need to test that different implementations of an interface satisfy some common requirements. Should I use typed tests or value-parameterized tests? 9828f6c2f2SEnji Cooper 9928f6c2f2SEnji CooperFor testing various implementations of the same interface, either typed tests or 10028f6c2f2SEnji Coopervalue-parameterized tests can get it done. It's really up to you the user to 10128f6c2f2SEnji Cooperdecide which is more convenient for you, depending on your particular case. Some 10228f6c2f2SEnji Cooperrough guidelines: 10328f6c2f2SEnji Cooper 10428f6c2f2SEnji Cooper* Typed tests can be easier to write if instances of the different 10528f6c2f2SEnji Cooper implementations can be created the same way, modulo the type. For example, 10628f6c2f2SEnji Cooper if all these implementations have a public default constructor (such that 10728f6c2f2SEnji Cooper you can write `new TypeParam`), or if their factory functions have the same 10828f6c2f2SEnji Cooper form (e.g. `CreateInstance<TypeParam>()`). 10928f6c2f2SEnji Cooper* Value-parameterized tests can be easier to write if you need different code 11028f6c2f2SEnji Cooper patterns to create different implementations' instances, e.g. `new Foo` vs 11128f6c2f2SEnji Cooper `new Bar(5)`. To accommodate for the differences, you can write factory 11228f6c2f2SEnji Cooper function wrappers and pass these function pointers to the tests as their 11328f6c2f2SEnji Cooper parameters. 11428f6c2f2SEnji Cooper* When a typed test fails, the default output includes the name of the type, 11528f6c2f2SEnji Cooper which can help you quickly identify which implementation is wrong. 11628f6c2f2SEnji Cooper Value-parameterized tests only show the number of the failed iteration by 11728f6c2f2SEnji Cooper default. You will need to define a function that returns the iteration name 11828f6c2f2SEnji Cooper and pass it as the third parameter to INSTANTIATE_TEST_SUITE_P to have more 11928f6c2f2SEnji Cooper useful output. 12028f6c2f2SEnji Cooper* When using typed tests, you need to make sure you are testing against the 12128f6c2f2SEnji Cooper interface type, not the concrete types (in other words, you want to make 12228f6c2f2SEnji Cooper sure `implicit_cast<MyInterface*>(my_concrete_impl)` works, not just that 12328f6c2f2SEnji Cooper `my_concrete_impl` works). It's less likely to make mistakes in this area 12428f6c2f2SEnji Cooper when using value-parameterized tests. 12528f6c2f2SEnji Cooper 12628f6c2f2SEnji CooperI hope I didn't confuse you more. :-) If you don't mind, I'd suggest you to give 12728f6c2f2SEnji Cooperboth approaches a try. Practice is a much better way to grasp the subtle 12828f6c2f2SEnji Cooperdifferences between the two tools. Once you have some concrete experience, you 12928f6c2f2SEnji Coopercan much more easily decide which one to use the next time. 13028f6c2f2SEnji Cooper 13128f6c2f2SEnji Cooper## My death test modifies some state, but the change seems lost after the death test finishes. Why? 13228f6c2f2SEnji Cooper 133*5ca8c28cSEnji CooperDeath tests (`EXPECT_DEATH`, etc.) are executed in a sub-process s.t. the 13428f6c2f2SEnji Cooperexpected crash won't kill the test program (i.e. the parent process). As a 13528f6c2f2SEnji Cooperresult, any in-memory side effects they incur are observable in their respective 13628f6c2f2SEnji Coopersub-processes, but not in the parent process. You can think of them as running 13728f6c2f2SEnji Cooperin a parallel universe, more or less. 13828f6c2f2SEnji Cooper 13928f6c2f2SEnji CooperIn particular, if you use mocking and the death test statement invokes some mock 14028f6c2f2SEnji Coopermethods, the parent process will think the calls have never occurred. Therefore, 14128f6c2f2SEnji Cooperyou may want to move your `EXPECT_CALL` statements inside the `EXPECT_DEATH` 14228f6c2f2SEnji Coopermacro. 14328f6c2f2SEnji Cooper 14428f6c2f2SEnji Cooper## EXPECT_EQ(htonl(blah), blah_blah) generates weird compiler errors in opt mode. Is this a GoogleTest bug? 14528f6c2f2SEnji Cooper 14628f6c2f2SEnji CooperActually, the bug is in `htonl()`. 14728f6c2f2SEnji Cooper 14828f6c2f2SEnji CooperAccording to `'man htonl'`, `htonl()` is a *function*, which means it's valid to 14928f6c2f2SEnji Cooperuse `htonl` as a function pointer. However, in opt mode `htonl()` is defined as 15028f6c2f2SEnji Coopera *macro*, which breaks this usage. 15128f6c2f2SEnji Cooper 15228f6c2f2SEnji CooperWorse, the macro definition of `htonl()` uses a `gcc` extension and is *not* 15328f6c2f2SEnji Cooperstandard C++. That hacky implementation has some ad hoc limitations. In 15428f6c2f2SEnji Cooperparticular, it prevents you from writing `Foo<sizeof(htonl(x))>()`, where `Foo` 15528f6c2f2SEnji Cooperis a template that has an integral argument. 15628f6c2f2SEnji Cooper 15728f6c2f2SEnji CooperThe implementation of `EXPECT_EQ(a, b)` uses `sizeof(... a ...)` inside a 15828f6c2f2SEnji Coopertemplate argument, and thus doesn't compile in opt mode when `a` contains a call 15928f6c2f2SEnji Cooperto `htonl()`. It is difficult to make `EXPECT_EQ` bypass the `htonl()` bug, as 16028f6c2f2SEnji Cooperthe solution must work with different compilers on various platforms. 16128f6c2f2SEnji Cooper 16228f6c2f2SEnji Cooper## The compiler complains about "undefined references" to some static const member variables, but I did define them in the class body. What's wrong? 16328f6c2f2SEnji Cooper 16428f6c2f2SEnji CooperIf your class has a static data member: 16528f6c2f2SEnji Cooper 16628f6c2f2SEnji Cooper```c++ 16728f6c2f2SEnji Cooper// foo.h 16828f6c2f2SEnji Cooperclass Foo { 16928f6c2f2SEnji Cooper ... 17028f6c2f2SEnji Cooper static const int kBar = 100; 17128f6c2f2SEnji Cooper}; 17228f6c2f2SEnji Cooper``` 17328f6c2f2SEnji Cooper 174*5ca8c28cSEnji Cooperyou also need to define it *outside* of the class body in `foo.cc`: 17528f6c2f2SEnji Cooper 17628f6c2f2SEnji Cooper```c++ 17728f6c2f2SEnji Cooperconst int Foo::kBar; // No initializer here. 17828f6c2f2SEnji Cooper``` 17928f6c2f2SEnji Cooper 18028f6c2f2SEnji CooperOtherwise your code is **invalid C++**, and may break in unexpected ways. In 181*5ca8c28cSEnji Cooperparticular, using it in GoogleTest comparison assertions (`EXPECT_EQ`, etc.) 182*5ca8c28cSEnji Cooperwill generate an "undefined reference" linker error. The fact that "it used to 183*5ca8c28cSEnji Cooperwork" doesn't mean it's valid. It just means that you were lucky. :-) 18428f6c2f2SEnji Cooper 18528f6c2f2SEnji CooperIf the declaration of the static data member is `constexpr` then it is 18628f6c2f2SEnji Cooperimplicitly an `inline` definition, and a separate definition in `foo.cc` is not 18728f6c2f2SEnji Cooperneeded: 18828f6c2f2SEnji Cooper 18928f6c2f2SEnji Cooper```c++ 19028f6c2f2SEnji Cooper// foo.h 19128f6c2f2SEnji Cooperclass Foo { 19228f6c2f2SEnji Cooper ... 19328f6c2f2SEnji Cooper static constexpr int kBar = 100; // Defines kBar, no need to do it in foo.cc. 19428f6c2f2SEnji Cooper}; 19528f6c2f2SEnji Cooper``` 19628f6c2f2SEnji Cooper 19728f6c2f2SEnji Cooper## Can I derive a test fixture from another? 19828f6c2f2SEnji Cooper 19928f6c2f2SEnji CooperYes. 20028f6c2f2SEnji Cooper 20128f6c2f2SEnji CooperEach test fixture has a corresponding and same named test suite. This means only 20228f6c2f2SEnji Cooperone test suite can use a particular fixture. Sometimes, however, multiple test 20328f6c2f2SEnji Coopercases may want to use the same or slightly different fixtures. For example, you 20428f6c2f2SEnji Coopermay want to make sure that all of a GUI library's test suites don't leak 20528f6c2f2SEnji Cooperimportant system resources like fonts and brushes. 20628f6c2f2SEnji Cooper 20728f6c2f2SEnji CooperIn GoogleTest, you share a fixture among test suites by putting the shared logic 20828f6c2f2SEnji Cooperin a base test fixture, then deriving from that base a separate fixture for each 20928f6c2f2SEnji Coopertest suite that wants to use this common logic. You then use `TEST_F()` to write 21028f6c2f2SEnji Coopertests using each derived fixture. 21128f6c2f2SEnji Cooper 21228f6c2f2SEnji CooperTypically, your code looks like this: 21328f6c2f2SEnji Cooper 21428f6c2f2SEnji Cooper```c++ 21528f6c2f2SEnji Cooper// Defines a base test fixture. 21628f6c2f2SEnji Cooperclass BaseTest : public ::testing::Test { 21728f6c2f2SEnji Cooper protected: 21828f6c2f2SEnji Cooper ... 21928f6c2f2SEnji Cooper}; 22028f6c2f2SEnji Cooper 22128f6c2f2SEnji Cooper// Derives a fixture FooTest from BaseTest. 22228f6c2f2SEnji Cooperclass FooTest : public BaseTest { 22328f6c2f2SEnji Cooper protected: 22428f6c2f2SEnji Cooper void SetUp() override { 22528f6c2f2SEnji Cooper BaseTest::SetUp(); // Sets up the base fixture first. 22628f6c2f2SEnji Cooper ... additional set-up work ... 22728f6c2f2SEnji Cooper } 22828f6c2f2SEnji Cooper 22928f6c2f2SEnji Cooper void TearDown() override { 23028f6c2f2SEnji Cooper ... clean-up work for FooTest ... 23128f6c2f2SEnji Cooper BaseTest::TearDown(); // Remember to tear down the base fixture 23228f6c2f2SEnji Cooper // after cleaning up FooTest! 23328f6c2f2SEnji Cooper } 23428f6c2f2SEnji Cooper 23528f6c2f2SEnji Cooper ... functions and variables for FooTest ... 23628f6c2f2SEnji Cooper}; 23728f6c2f2SEnji Cooper 23828f6c2f2SEnji Cooper// Tests that use the fixture FooTest. 23928f6c2f2SEnji CooperTEST_F(FooTest, Bar) { ... } 24028f6c2f2SEnji CooperTEST_F(FooTest, Baz) { ... } 24128f6c2f2SEnji Cooper 24228f6c2f2SEnji Cooper... additional fixtures derived from BaseTest ... 24328f6c2f2SEnji Cooper``` 24428f6c2f2SEnji Cooper 24528f6c2f2SEnji CooperIf necessary, you can continue to derive test fixtures from a derived fixture. 24628f6c2f2SEnji CooperGoogleTest has no limit on how deep the hierarchy can be. 24728f6c2f2SEnji Cooper 24828f6c2f2SEnji CooperFor a complete example using derived test fixtures, see 24928f6c2f2SEnji Cooper[sample5_unittest.cc](https://github.com/google/googletest/blob/main/googletest/samples/sample5_unittest.cc). 25028f6c2f2SEnji Cooper 25128f6c2f2SEnji Cooper## My compiler complains "void value not ignored as it ought to be." What does this mean? 25228f6c2f2SEnji Cooper 25328f6c2f2SEnji CooperYou're probably using an `ASSERT_*()` in a function that doesn't return `void`. 25428f6c2f2SEnji Cooper`ASSERT_*()` can only be used in `void` functions, due to exceptions being 25528f6c2f2SEnji Cooperdisabled by our build system. Please see more details 25628f6c2f2SEnji Cooper[here](advanced.md#assertion-placement). 25728f6c2f2SEnji Cooper 25828f6c2f2SEnji Cooper## My death test hangs (or seg-faults). How do I fix it? 25928f6c2f2SEnji Cooper 26028f6c2f2SEnji CooperIn GoogleTest, death tests are run in a child process and the way they work is 26128f6c2f2SEnji Cooperdelicate. To write death tests you really need to understand how they work—see 26228f6c2f2SEnji Cooperthe details at [Death Assertions](reference/assertions.md#death) in the 26328f6c2f2SEnji CooperAssertions Reference. 26428f6c2f2SEnji Cooper 26528f6c2f2SEnji CooperIn particular, death tests don't like having multiple threads in the parent 26628f6c2f2SEnji Cooperprocess. So the first thing you can try is to eliminate creating threads outside 26728f6c2f2SEnji Cooperof `EXPECT_DEATH()`. For example, you may want to use mocks or fake objects 26828f6c2f2SEnji Cooperinstead of real ones in your tests. 26928f6c2f2SEnji Cooper 27028f6c2f2SEnji CooperSometimes this is impossible as some library you must use may be creating 27128f6c2f2SEnji Cooperthreads before `main()` is even reached. In this case, you can try to minimize 27228f6c2f2SEnji Cooperthe chance of conflicts by either moving as many activities as possible inside 27328f6c2f2SEnji Cooper`EXPECT_DEATH()` (in the extreme case, you want to move everything inside), or 27428f6c2f2SEnji Cooperleaving as few things as possible in it. Also, you can try to set the death test 27528f6c2f2SEnji Cooperstyle to `"threadsafe"`, which is safer but slower, and see if it helps. 27628f6c2f2SEnji Cooper 27728f6c2f2SEnji CooperIf you go with thread-safe death tests, remember that they rerun the test 27828f6c2f2SEnji Cooperprogram from the beginning in the child process. Therefore make sure your 27928f6c2f2SEnji Cooperprogram can run side-by-side with itself and is deterministic. 28028f6c2f2SEnji Cooper 28128f6c2f2SEnji CooperIn the end, this boils down to good concurrent programming. You have to make 28228f6c2f2SEnji Coopersure that there are no race conditions or deadlocks in your program. No silver 28328f6c2f2SEnji Cooperbullet - sorry! 28428f6c2f2SEnji Cooper 28528f6c2f2SEnji Cooper## Should I use the constructor/destructor of the test fixture or SetUp()/TearDown()? {#CtorVsSetUp} 28628f6c2f2SEnji Cooper 28728f6c2f2SEnji CooperThe first thing to remember is that GoogleTest does **not** reuse the same test 28828f6c2f2SEnji Cooperfixture object across multiple tests. For each `TEST_F`, GoogleTest will create 28928f6c2f2SEnji Coopera **fresh** test fixture object, immediately call `SetUp()`, run the test body, 29028f6c2f2SEnji Coopercall `TearDown()`, and then delete the test fixture object. 29128f6c2f2SEnji Cooper 29228f6c2f2SEnji CooperWhen you need to write per-test set-up and tear-down logic, you have the choice 293*5ca8c28cSEnji Cooperbetween using the test fixture constructor/destructor or `SetUp()`/`TearDown()`. 29428f6c2f2SEnji CooperThe former is usually preferred, as it has the following benefits: 29528f6c2f2SEnji Cooper 29628f6c2f2SEnji Cooper* By initializing a member variable in the constructor, we have the option to 29728f6c2f2SEnji Cooper make it `const`, which helps prevent accidental changes to its value and 29828f6c2f2SEnji Cooper makes the tests more obviously correct. 29928f6c2f2SEnji Cooper* In case we need to subclass the test fixture class, the subclass' 30028f6c2f2SEnji Cooper constructor is guaranteed to call the base class' constructor *first*, and 30128f6c2f2SEnji Cooper the subclass' destructor is guaranteed to call the base class' destructor 30228f6c2f2SEnji Cooper *afterward*. With `SetUp()/TearDown()`, a subclass may make the mistake of 30328f6c2f2SEnji Cooper forgetting to call the base class' `SetUp()/TearDown()` or call them at the 30428f6c2f2SEnji Cooper wrong time. 30528f6c2f2SEnji Cooper 30628f6c2f2SEnji CooperYou may still want to use `SetUp()/TearDown()` in the following cases: 30728f6c2f2SEnji Cooper 30828f6c2f2SEnji Cooper* C++ does not allow virtual function calls in constructors and destructors. 30928f6c2f2SEnji Cooper You can call a method declared as virtual, but it will not use dynamic 31028f6c2f2SEnji Cooper dispatch. It will use the definition from the class the constructor of which 31128f6c2f2SEnji Cooper is currently executing. This is because calling a virtual method before the 31228f6c2f2SEnji Cooper derived class constructor has a chance to run is very dangerous - the 31328f6c2f2SEnji Cooper virtual method might operate on uninitialized data. Therefore, if you need 31428f6c2f2SEnji Cooper to call a method that will be overridden in a derived class, you have to use 31528f6c2f2SEnji Cooper `SetUp()/TearDown()`. 31628f6c2f2SEnji Cooper* In the body of a constructor (or destructor), it's not possible to use the 31728f6c2f2SEnji Cooper `ASSERT_xx` macros. Therefore, if the set-up operation could cause a fatal 31828f6c2f2SEnji Cooper test failure that should prevent the test from running, it's necessary to 31928f6c2f2SEnji Cooper use `abort` and abort the whole test 32028f6c2f2SEnji Cooper executable, or to use `SetUp()` instead of a constructor. 32128f6c2f2SEnji Cooper* If the tear-down operation could throw an exception, you must use 32228f6c2f2SEnji Cooper `TearDown()` as opposed to the destructor, as throwing in a destructor leads 32328f6c2f2SEnji Cooper to undefined behavior and usually will kill your program right away. Note 32428f6c2f2SEnji Cooper that many standard libraries (like STL) may throw when exceptions are 32528f6c2f2SEnji Cooper enabled in the compiler. Therefore you should prefer `TearDown()` if you 32628f6c2f2SEnji Cooper want to write portable tests that work with or without exceptions. 32728f6c2f2SEnji Cooper* The GoogleTest team is considering making the assertion macros throw on 32828f6c2f2SEnji Cooper platforms where exceptions are enabled (e.g. Windows, Mac OS, and Linux 32928f6c2f2SEnji Cooper client-side), which will eliminate the need for the user to propagate 33028f6c2f2SEnji Cooper failures from a subroutine to its caller. Therefore, you shouldn't use 33128f6c2f2SEnji Cooper GoogleTest assertions in a destructor if your code could run on such a 33228f6c2f2SEnji Cooper platform. 33328f6c2f2SEnji Cooper 334*5ca8c28cSEnji Cooper## The compiler complains "no matching function to call" when I use `ASSERT_PRED*`. How do I fix it? 33528f6c2f2SEnji Cooper 33628f6c2f2SEnji CooperSee details for [`EXPECT_PRED*`](reference/assertions.md#EXPECT_PRED) in the 33728f6c2f2SEnji CooperAssertions Reference. 33828f6c2f2SEnji Cooper 33928f6c2f2SEnji Cooper## My compiler complains about "ignoring return value" when I call RUN_ALL_TESTS(). Why? 34028f6c2f2SEnji Cooper 34128f6c2f2SEnji CooperSome people had been ignoring the return value of `RUN_ALL_TESTS()`. That is, 34228f6c2f2SEnji Cooperinstead of 34328f6c2f2SEnji Cooper 34428f6c2f2SEnji Cooper```c++ 34528f6c2f2SEnji Cooper return RUN_ALL_TESTS(); 34628f6c2f2SEnji Cooper``` 34728f6c2f2SEnji Cooper 34828f6c2f2SEnji Cooperthey write 34928f6c2f2SEnji Cooper 35028f6c2f2SEnji Cooper```c++ 35128f6c2f2SEnji Cooper RUN_ALL_TESTS(); 35228f6c2f2SEnji Cooper``` 35328f6c2f2SEnji Cooper 35428f6c2f2SEnji CooperThis is **wrong and dangerous**. The testing services needs to see the return 35528f6c2f2SEnji Coopervalue of `RUN_ALL_TESTS()` in order to determine if a test has passed. If your 35628f6c2f2SEnji Cooper`main()` function ignores it, your test will be considered successful even if it 35728f6c2f2SEnji Cooperhas a GoogleTest assertion failure. Very bad. 35828f6c2f2SEnji Cooper 35928f6c2f2SEnji CooperWe have decided to fix this (thanks to Michael Chastain for the idea). Now, your 36028f6c2f2SEnji Coopercode will no longer be able to ignore `RUN_ALL_TESTS()` when compiled with 36128f6c2f2SEnji Cooper`gcc`. If you do so, you'll get a compiler error. 36228f6c2f2SEnji Cooper 36328f6c2f2SEnji CooperIf you see the compiler complaining about you ignoring the return value of 36428f6c2f2SEnji Cooper`RUN_ALL_TESTS()`, the fix is simple: just make sure its value is used as the 36528f6c2f2SEnji Cooperreturn value of `main()`. 36628f6c2f2SEnji Cooper 36728f6c2f2SEnji CooperBut how could we introduce a change that breaks existing tests? Well, in this 36828f6c2f2SEnji Coopercase, the code was already broken in the first place, so we didn't break it. :-) 36928f6c2f2SEnji Cooper 37028f6c2f2SEnji Cooper## My compiler complains that a constructor (or destructor) cannot return a value. What's going on? 37128f6c2f2SEnji Cooper 37228f6c2f2SEnji CooperDue to a peculiarity of C++, in order to support the syntax for streaming 37328f6c2f2SEnji Coopermessages to an `ASSERT_*`, e.g. 37428f6c2f2SEnji Cooper 37528f6c2f2SEnji Cooper```c++ 37628f6c2f2SEnji Cooper ASSERT_EQ(1, Foo()) << "blah blah" << foo; 37728f6c2f2SEnji Cooper``` 37828f6c2f2SEnji Cooper 37928f6c2f2SEnji Cooperwe had to give up using `ASSERT*` and `FAIL*` (but not `EXPECT*` and 38028f6c2f2SEnji Cooper`ADD_FAILURE*`) in constructors and destructors. The workaround is to move the 38128f6c2f2SEnji Coopercontent of your constructor/destructor to a private void member function, or 38228f6c2f2SEnji Cooperswitch to `EXPECT_*()` if that works. This 38328f6c2f2SEnji Cooper[section](advanced.md#assertion-placement) in the user's guide explains it. 38428f6c2f2SEnji Cooper 38528f6c2f2SEnji Cooper## My SetUp() function is not called. Why? 38628f6c2f2SEnji Cooper 38728f6c2f2SEnji CooperC++ is case-sensitive. Did you spell it as `Setup()`? 38828f6c2f2SEnji Cooper 38928f6c2f2SEnji CooperSimilarly, sometimes people spell `SetUpTestSuite()` as `SetupTestSuite()` and 39028f6c2f2SEnji Cooperwonder why it's never called. 39128f6c2f2SEnji Cooper 392*5ca8c28cSEnji Cooper## 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. 39328f6c2f2SEnji Cooper 39428f6c2f2SEnji CooperYou don't have to. Instead of 39528f6c2f2SEnji Cooper 39628f6c2f2SEnji Cooper```c++ 39728f6c2f2SEnji Cooperclass FooTest : public BaseTest {}; 39828f6c2f2SEnji Cooper 39928f6c2f2SEnji CooperTEST_F(FooTest, Abc) { ... } 40028f6c2f2SEnji CooperTEST_F(FooTest, Def) { ... } 40128f6c2f2SEnji Cooper 40228f6c2f2SEnji Cooperclass BarTest : public BaseTest {}; 40328f6c2f2SEnji Cooper 40428f6c2f2SEnji CooperTEST_F(BarTest, Abc) { ... } 40528f6c2f2SEnji CooperTEST_F(BarTest, Def) { ... } 40628f6c2f2SEnji Cooper``` 40728f6c2f2SEnji Cooper 40828f6c2f2SEnji Cooperyou can simply `typedef` the test fixtures: 40928f6c2f2SEnji Cooper 41028f6c2f2SEnji Cooper```c++ 41128f6c2f2SEnji Coopertypedef BaseTest FooTest; 41228f6c2f2SEnji Cooper 41328f6c2f2SEnji CooperTEST_F(FooTest, Abc) { ... } 41428f6c2f2SEnji CooperTEST_F(FooTest, Def) { ... } 41528f6c2f2SEnji Cooper 41628f6c2f2SEnji Coopertypedef BaseTest BarTest; 41728f6c2f2SEnji Cooper 41828f6c2f2SEnji CooperTEST_F(BarTest, Abc) { ... } 41928f6c2f2SEnji CooperTEST_F(BarTest, Def) { ... } 42028f6c2f2SEnji Cooper``` 42128f6c2f2SEnji Cooper 42228f6c2f2SEnji Cooper## GoogleTest output is buried in a whole bunch of LOG messages. What do I do? 42328f6c2f2SEnji Cooper 42428f6c2f2SEnji CooperThe GoogleTest output is meant to be a concise and human-friendly report. If 42528f6c2f2SEnji Cooperyour test generates textual output itself, it will mix with the GoogleTest 42628f6c2f2SEnji Cooperoutput, making it hard to read. However, there is an easy solution to this 42728f6c2f2SEnji Cooperproblem. 42828f6c2f2SEnji Cooper 42928f6c2f2SEnji CooperSince `LOG` messages go to stderr, we decided to let GoogleTest output go to 43028f6c2f2SEnji Cooperstdout. This way, you can easily separate the two using redirection. For 43128f6c2f2SEnji Cooperexample: 43228f6c2f2SEnji Cooper 43328f6c2f2SEnji Cooper```shell 43428f6c2f2SEnji Cooper$ ./my_test > gtest_output.txt 43528f6c2f2SEnji Cooper``` 43628f6c2f2SEnji Cooper 43728f6c2f2SEnji Cooper## Why should I prefer test fixtures over global variables? 43828f6c2f2SEnji Cooper 43928f6c2f2SEnji CooperThere are several good reasons: 44028f6c2f2SEnji Cooper 44128f6c2f2SEnji Cooper1. It's likely your test needs to change the states of its global variables. 44228f6c2f2SEnji Cooper This makes it difficult to keep side effects from escaping one test and 44328f6c2f2SEnji Cooper contaminating others, making debugging difficult. By using fixtures, each 44428f6c2f2SEnji Cooper test has a fresh set of variables that's different (but with the same 44528f6c2f2SEnji Cooper names). Thus, tests are kept independent of each other. 44628f6c2f2SEnji Cooper2. Global variables pollute the global namespace. 44728f6c2f2SEnji Cooper3. Test fixtures can be reused via subclassing, which cannot be done easily 44828f6c2f2SEnji Cooper with global variables. This is useful if many test suites have something in 44928f6c2f2SEnji Cooper common. 45028f6c2f2SEnji Cooper 45128f6c2f2SEnji Cooper## What can the statement argument in ASSERT_DEATH() be? 45228f6c2f2SEnji Cooper 45328f6c2f2SEnji Cooper`ASSERT_DEATH(statement, matcher)` (or any death assertion macro) can be used 45428f6c2f2SEnji Cooperwherever *`statement`* is valid. So basically *`statement`* can be any C++ 45528f6c2f2SEnji Cooperstatement that makes sense in the current context. In particular, it can 45628f6c2f2SEnji Cooperreference global and/or local variables, and can be: 45728f6c2f2SEnji Cooper 45828f6c2f2SEnji Cooper* a simple function call (often the case), 45928f6c2f2SEnji Cooper* a complex expression, or 46028f6c2f2SEnji Cooper* a compound statement. 46128f6c2f2SEnji Cooper 46228f6c2f2SEnji CooperSome examples are shown here: 46328f6c2f2SEnji Cooper 46428f6c2f2SEnji Cooper```c++ 46528f6c2f2SEnji Cooper// A death test can be a simple function call. 46628f6c2f2SEnji CooperTEST(MyDeathTest, FunctionCall) { 46728f6c2f2SEnji Cooper ASSERT_DEATH(Xyz(5), "Xyz failed"); 46828f6c2f2SEnji Cooper} 46928f6c2f2SEnji Cooper 47028f6c2f2SEnji Cooper// Or a complex expression that references variables and functions. 47128f6c2f2SEnji CooperTEST(MyDeathTest, ComplexExpression) { 47228f6c2f2SEnji Cooper const bool c = Condition(); 47328f6c2f2SEnji Cooper ASSERT_DEATH((c ? Func1(0) : object2.Method("test")), 47428f6c2f2SEnji Cooper "(Func1|Method) failed"); 47528f6c2f2SEnji Cooper} 47628f6c2f2SEnji Cooper 47728f6c2f2SEnji Cooper// Death assertions can be used anywhere in a function. In 47828f6c2f2SEnji Cooper// particular, they can be inside a loop. 47928f6c2f2SEnji CooperTEST(MyDeathTest, InsideLoop) { 48028f6c2f2SEnji Cooper // Verifies that Foo(0), Foo(1), ..., and Foo(4) all die. 48128f6c2f2SEnji Cooper for (int i = 0; i < 5; i++) { 48228f6c2f2SEnji Cooper EXPECT_DEATH_M(Foo(i), "Foo has \\d+ errors", 48328f6c2f2SEnji Cooper ::testing::Message() << "where i is " << i); 48428f6c2f2SEnji Cooper } 48528f6c2f2SEnji Cooper} 48628f6c2f2SEnji Cooper 48728f6c2f2SEnji Cooper// A death assertion can contain a compound statement. 48828f6c2f2SEnji CooperTEST(MyDeathTest, CompoundStatement) { 48928f6c2f2SEnji Cooper // Verifies that at lease one of Bar(0), Bar(1), ..., and 49028f6c2f2SEnji Cooper // Bar(4) dies. 49128f6c2f2SEnji Cooper ASSERT_DEATH({ 49228f6c2f2SEnji Cooper for (int i = 0; i < 5; i++) { 49328f6c2f2SEnji Cooper Bar(i); 49428f6c2f2SEnji Cooper } 49528f6c2f2SEnji Cooper }, 49628f6c2f2SEnji Cooper "Bar has \\d+ errors"); 49728f6c2f2SEnji Cooper} 49828f6c2f2SEnji Cooper``` 49928f6c2f2SEnji Cooper 50028f6c2f2SEnji Cooper## I have a fixture class `FooTest`, but `TEST_F(FooTest, Bar)` gives me error ``"no matching function for call to `FooTest::FooTest()'"``. Why? 50128f6c2f2SEnji Cooper 50228f6c2f2SEnji CooperGoogleTest needs to be able to create objects of your test fixture class, so it 50328f6c2f2SEnji Coopermust have a default constructor. Normally the compiler will define one for you. 50428f6c2f2SEnji CooperHowever, there are cases where you have to define your own: 50528f6c2f2SEnji Cooper 50628f6c2f2SEnji Cooper* If you explicitly declare a non-default constructor for class `FooTest` 50728f6c2f2SEnji Cooper (`DISALLOW_EVIL_CONSTRUCTORS()` does this), then you need to define a 50828f6c2f2SEnji Cooper default constructor, even if it would be empty. 50928f6c2f2SEnji Cooper* If `FooTest` has a const non-static data member, then you have to define the 51028f6c2f2SEnji Cooper default constructor *and* initialize the const member in the initializer 51128f6c2f2SEnji Cooper list of the constructor. (Early versions of `gcc` doesn't force you to 51228f6c2f2SEnji Cooper initialize the const member. It's a bug that has been fixed in `gcc 4`.) 51328f6c2f2SEnji Cooper 51428f6c2f2SEnji Cooper## Why does ASSERT_DEATH complain about previous threads that were already joined? 51528f6c2f2SEnji Cooper 51628f6c2f2SEnji CooperWith the Linux pthread library, there is no turning back once you cross the line 51728f6c2f2SEnji Cooperfrom a single thread to multiple threads. The first time you create a thread, a 51828f6c2f2SEnji Coopermanager thread is created in addition, so you get 3, not 2, threads. Later when 51928f6c2f2SEnji Cooperthe thread you create joins the main thread, the thread count decrements by 1, 52028f6c2f2SEnji Cooperbut the manager thread will never be killed, so you still have 2 threads, which 52128f6c2f2SEnji Coopermeans you cannot safely run a death test. 52228f6c2f2SEnji Cooper 52328f6c2f2SEnji CooperThe new NPTL thread library doesn't suffer from this problem, as it doesn't 52428f6c2f2SEnji Coopercreate a manager thread. However, if you don't control which machine your test 52528f6c2f2SEnji Cooperruns on, you shouldn't depend on this. 52628f6c2f2SEnji Cooper 527*5ca8c28cSEnji Cooper## Why does GoogleTest require the entire test suite, instead of individual tests, to be named `*DeathTest` when it uses `ASSERT_DEATH`? 52828f6c2f2SEnji Cooper 52928f6c2f2SEnji CooperGoogleTest does not interleave tests from different test suites. That is, it 53028f6c2f2SEnji Cooperruns all tests in one test suite first, and then runs all tests in the next test 53128f6c2f2SEnji Coopersuite, and so on. GoogleTest does this because it needs to set up a test suite 53228f6c2f2SEnji Cooperbefore the first test in it is run, and tear it down afterwards. Splitting up 53328f6c2f2SEnji Cooperthe test case would require multiple set-up and tear-down processes, which is 53428f6c2f2SEnji Cooperinefficient and makes the semantics unclean. 53528f6c2f2SEnji Cooper 53628f6c2f2SEnji CooperIf we were to determine the order of tests based on test name instead of test 53728f6c2f2SEnji Coopercase name, then we would have a problem with the following situation: 53828f6c2f2SEnji Cooper 53928f6c2f2SEnji Cooper```c++ 54028f6c2f2SEnji CooperTEST_F(FooTest, AbcDeathTest) { ... } 54128f6c2f2SEnji CooperTEST_F(FooTest, Uvw) { ... } 54228f6c2f2SEnji Cooper 54328f6c2f2SEnji CooperTEST_F(BarTest, DefDeathTest) { ... } 54428f6c2f2SEnji CooperTEST_F(BarTest, Xyz) { ... } 54528f6c2f2SEnji Cooper``` 54628f6c2f2SEnji Cooper 54728f6c2f2SEnji CooperSince `FooTest.AbcDeathTest` needs to run before `BarTest.Xyz`, and we don't 54828f6c2f2SEnji Cooperinterleave tests from different test suites, we need to run all tests in the 54928f6c2f2SEnji Cooper`FooTest` case before running any test in the `BarTest` case. This contradicts 55028f6c2f2SEnji Cooperwith the requirement to run `BarTest.DefDeathTest` before `FooTest.Uvw`. 55128f6c2f2SEnji Cooper 552*5ca8c28cSEnji Cooper## 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? 55328f6c2f2SEnji Cooper 55428f6c2f2SEnji CooperYou don't have to, but if you like, you may split up the test suite into 55528f6c2f2SEnji Cooper`FooTest` and `FooDeathTest`, where the names make it clear that they are 55628f6c2f2SEnji Cooperrelated: 55728f6c2f2SEnji Cooper 55828f6c2f2SEnji Cooper```c++ 55928f6c2f2SEnji Cooperclass FooTest : public ::testing::Test { ... }; 56028f6c2f2SEnji Cooper 56128f6c2f2SEnji CooperTEST_F(FooTest, Abc) { ... } 56228f6c2f2SEnji CooperTEST_F(FooTest, Def) { ... } 56328f6c2f2SEnji Cooper 56428f6c2f2SEnji Cooperusing FooDeathTest = FooTest; 56528f6c2f2SEnji Cooper 56628f6c2f2SEnji CooperTEST_F(FooDeathTest, Uvw) { ... EXPECT_DEATH(...) ... } 56728f6c2f2SEnji CooperTEST_F(FooDeathTest, Xyz) { ... ASSERT_DEATH(...) ... } 56828f6c2f2SEnji Cooper``` 56928f6c2f2SEnji Cooper 57028f6c2f2SEnji Cooper## 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? 57128f6c2f2SEnji Cooper 57228f6c2f2SEnji CooperPrinting the LOG messages generated by the statement inside `EXPECT_DEATH()` 57328f6c2f2SEnji Coopermakes it harder to search for real problems in the parent's log. Therefore, 57428f6c2f2SEnji CooperGoogleTest only prints them when the death test has failed. 57528f6c2f2SEnji Cooper 57628f6c2f2SEnji CooperIf you really need to see such LOG messages, a workaround is to temporarily 57728f6c2f2SEnji Cooperbreak the death test (e.g. by changing the regex pattern it is expected to 57828f6c2f2SEnji Coopermatch). Admittedly, this is a hack. We'll consider a more permanent solution 57928f6c2f2SEnji Cooperafter the fork-and-exec-style death tests are implemented. 58028f6c2f2SEnji Cooper 58128f6c2f2SEnji Cooper## The compiler complains about `no match for 'operator<<'` when I use an assertion. What gives? 58228f6c2f2SEnji Cooper 58328f6c2f2SEnji CooperIf you use a user-defined type `FooType` in an assertion, you must make sure 58428f6c2f2SEnji Cooperthere is an `std::ostream& operator<<(std::ostream&, const FooType&)` function 58528f6c2f2SEnji Cooperdefined such that we can print a value of `FooType`. 58628f6c2f2SEnji Cooper 58728f6c2f2SEnji CooperIn addition, if `FooType` is declared in a name space, the `<<` operator also 58828f6c2f2SEnji Cooperneeds to be defined in the *same* name space. See 589*5ca8c28cSEnji Cooper[Tip of the Week #49](https://abseil.io/tips/49) for details. 59028f6c2f2SEnji Cooper 59128f6c2f2SEnji Cooper## How do I suppress the memory leak messages on Windows? 59228f6c2f2SEnji Cooper 59328f6c2f2SEnji CooperSince the statically initialized GoogleTest singleton requires allocations on 59428f6c2f2SEnji Cooperthe heap, the Visual C++ memory leak detector will report memory leaks at the 59528f6c2f2SEnji Cooperend of the program run. The easiest way to avoid this is to use the 59628f6c2f2SEnji Cooper`_CrtMemCheckpoint` and `_CrtMemDumpAllObjectsSince` calls to not report any 59728f6c2f2SEnji Cooperstatically initialized heap objects. See MSDN for more details and additional 59828f6c2f2SEnji Cooperheap check/debug routines. 59928f6c2f2SEnji Cooper 60028f6c2f2SEnji Cooper## How can my code detect if it is running in a test? 60128f6c2f2SEnji Cooper 60228f6c2f2SEnji CooperIf you write code that sniffs whether it's running in a test and does different 60328f6c2f2SEnji Cooperthings accordingly, you are leaking test-only logic into production code and 60428f6c2f2SEnji Cooperthere is no easy way to ensure that the test-only code paths aren't run by 60528f6c2f2SEnji Coopermistake in production. Such cleverness also leads to 60628f6c2f2SEnji Cooper[Heisenbugs](https://en.wikipedia.org/wiki/Heisenbug). Therefore we strongly 60728f6c2f2SEnji Cooperadvise against the practice, and GoogleTest doesn't provide a way to do it. 60828f6c2f2SEnji Cooper 60928f6c2f2SEnji CooperIn general, the recommended way to cause the code to behave differently under 610*5ca8c28cSEnji Coopertest is [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection). You can inject 61128f6c2f2SEnji Cooperdifferent functionality from the test and from the production code. Since your 61228f6c2f2SEnji Cooperproduction code doesn't link in the for-test logic at all (the 613*5ca8c28cSEnji Cooper[`testonly`](https://docs.bazel.build/versions/master/be/common-definitions.html#common.testonly) attribute for BUILD targets helps to ensure 61428f6c2f2SEnji Cooperthat), there is no danger in accidentally running it. 61528f6c2f2SEnji Cooper 61628f6c2f2SEnji CooperHowever, if you *really*, *really*, *really* have no choice, and if you follow 61728f6c2f2SEnji Cooperthe rule of ending your test program names with `_test`, you can use the 61828f6c2f2SEnji Cooper*horrible* hack of sniffing your executable name (`argv[0]` in `main()`) to know 61928f6c2f2SEnji Cooperwhether the code is under test. 62028f6c2f2SEnji Cooper 62128f6c2f2SEnji Cooper## How do I temporarily disable a test? 62228f6c2f2SEnji Cooper 62328f6c2f2SEnji CooperIf you have a broken test that you cannot fix right away, you can add the 62428f6c2f2SEnji Cooper`DISABLED_` prefix to its name. This will exclude it from execution. This is 62528f6c2f2SEnji Cooperbetter than commenting out the code or using `#if 0`, as disabled tests are 62628f6c2f2SEnji Cooperstill compiled (and thus won't rot). 62728f6c2f2SEnji Cooper 62828f6c2f2SEnji CooperTo include disabled tests in test execution, just invoke the test program with 62928f6c2f2SEnji Cooperthe `--gtest_also_run_disabled_tests` flag. 63028f6c2f2SEnji Cooper 63128f6c2f2SEnji Cooper## Is it OK if I have two separate `TEST(Foo, Bar)` test methods defined in different namespaces? 63228f6c2f2SEnji Cooper 63328f6c2f2SEnji CooperYes. 63428f6c2f2SEnji Cooper 63528f6c2f2SEnji CooperThe rule is **all test methods in the same test suite must use the same fixture 636*5ca8c28cSEnji Cooperclass**. This means that the following is **allowed** because both tests use the 63728f6c2f2SEnji Coopersame fixture class (`::testing::Test`). 63828f6c2f2SEnji Cooper 63928f6c2f2SEnji Cooper```c++ 64028f6c2f2SEnji Coopernamespace foo { 64128f6c2f2SEnji CooperTEST(CoolTest, DoSomething) { 64228f6c2f2SEnji Cooper SUCCEED(); 64328f6c2f2SEnji Cooper} 64428f6c2f2SEnji Cooper} // namespace foo 64528f6c2f2SEnji Cooper 64628f6c2f2SEnji Coopernamespace bar { 64728f6c2f2SEnji CooperTEST(CoolTest, DoSomething) { 64828f6c2f2SEnji Cooper SUCCEED(); 64928f6c2f2SEnji Cooper} 65028f6c2f2SEnji Cooper} // namespace bar 65128f6c2f2SEnji Cooper``` 65228f6c2f2SEnji Cooper 65328f6c2f2SEnji CooperHowever, the following code is **not allowed** and will produce a runtime error 65428f6c2f2SEnji Cooperfrom GoogleTest because the test methods are using different test fixture 65528f6c2f2SEnji Cooperclasses with the same test suite name. 65628f6c2f2SEnji Cooper 65728f6c2f2SEnji Cooper```c++ 65828f6c2f2SEnji Coopernamespace foo { 65928f6c2f2SEnji Cooperclass CoolTest : public ::testing::Test {}; // Fixture foo::CoolTest 66028f6c2f2SEnji CooperTEST_F(CoolTest, DoSomething) { 66128f6c2f2SEnji Cooper SUCCEED(); 66228f6c2f2SEnji Cooper} 66328f6c2f2SEnji Cooper} // namespace foo 66428f6c2f2SEnji Cooper 66528f6c2f2SEnji Coopernamespace bar { 66628f6c2f2SEnji Cooperclass CoolTest : public ::testing::Test {}; // Fixture: bar::CoolTest 66728f6c2f2SEnji CooperTEST_F(CoolTest, DoSomething) { 66828f6c2f2SEnji Cooper SUCCEED(); 66928f6c2f2SEnji Cooper} 67028f6c2f2SEnji Cooper} // namespace bar 67128f6c2f2SEnji Cooper``` 672