1 2 3If you cannot find the answer to your question here, and you have read 4[Primer](Primer.md) and [AdvancedGuide](AdvancedGuide.md), send it to 5googletestframework@googlegroups.com. 6 7## Why should I use Google Test instead of my favorite C++ testing framework? ## 8 9First, let us say clearly that we don't want to get into the debate of 10which C++ testing framework is **the best**. There exist many fine 11frameworks for writing C++ tests, and we have tremendous respect for 12the developers and users of them. We don't think there is (or will 13be) a single best framework - you have to pick the right tool for the 14particular task you are tackling. 15 16We created Google Test because we couldn't find the right combination 17of features and conveniences in an existing framework to satisfy _our_ 18needs. The following is a list of things that _we_ like about Google 19Test. We don't claim them to be unique to Google Test - rather, the 20combination of them makes Google Test the choice for us. We hope this 21list can help you decide whether it is for you too. 22 23 * Google Test is designed to be portable: it doesn't require exceptions or RTTI; it works around various bugs in various compilers and environments; etc. As a result, it works on Linux, Mac OS X, Windows and several embedded operating systems. 24 * Nonfatal assertions (`EXPECT_*`) have proven to be great time savers, as they allow a test to report multiple failures in a single edit-compile-test cycle. 25 * It's easy to write assertions that generate informative messages: you just use the stream syntax to append any additional information, e.g. `ASSERT_EQ(5, Foo(i)) << " where i = " << i;`. It doesn't require a new set of macros or special functions. 26 * Google Test automatically detects your tests and doesn't require you to enumerate them in order to run them. 27 * Death tests are pretty handy for ensuring that your asserts in production code are triggered by the right conditions. 28 * `SCOPED_TRACE` helps you understand the context of an assertion failure when it comes from inside a sub-routine or loop. 29 * You can decide which tests to run using name patterns. This saves time when you want to quickly reproduce a test failure. 30 * Google Test can generate XML test result reports that can be parsed by popular continuous build system like Hudson. 31 * Simple things are easy in Google Test, while hard things are possible: in addition to advanced features like [global test environments](AdvancedGuide.md#global-set-up-and-tear-down) and tests parameterized by [values](AdvancedGuide.md#value-parameterized-tests) or [types](docs/AdvancedGuide.md#typed-tests), Google Test supports various ways for the user to extend the framework -- if Google Test doesn't do something out of the box, chances are that a user can implement the feature using Google Test's public API, without changing Google Test itself. In particular, you can: 32 * expand your testing vocabulary by defining [custom predicates](AdvancedGuide.md#predicate-assertions-for-better-error-messages), 33 * teach Google Test how to [print your types](AdvancedGuide.md#teaching-google-test-how-to-print-your-values), 34 * define your own testing macros or utilities and verify them using Google Test's [Service Provider Interface](AdvancedGuide.md#catching-failures), and 35 * reflect on the test cases or change the test output format by intercepting the [test events](AdvancedGuide.md#extending-google-test-by-handling-test-events). 36 37## I'm getting warnings when compiling Google Test. Would you fix them? ## 38 39We strive to minimize compiler warnings Google Test generates. Before releasing a new version, we test to make sure that it doesn't generate warnings when compiled using its CMake script on Windows, Linux, and Mac OS. 40 41Unfortunately, this doesn't mean you are guaranteed to see no warnings when compiling Google Test in your environment: 42 43 * You may be using a different compiler as we use, or a different version of the same compiler. We cannot possibly test for all compilers. 44 * You may be compiling on a different platform as we do. 45 * Your project may be using different compiler flags as we do. 46 47It is not always possible to make Google Test warning-free for everyone. Or, it may not be desirable if the warning is rarely enabled and fixing the violations makes the code more complex. 48 49If you see warnings when compiling Google Test, we suggest that you use the `-isystem` flag (assuming your are using GCC) to mark Google Test headers as system headers. That'll suppress warnings from Google Test headers. 50 51## Why should not test case names and test names contain underscore? ## 52 53Underscore (`_`) is special, as C++ reserves the following to be used by 54the compiler and the standard library: 55 56 1. any identifier that starts with an `_` followed by an upper-case letter, and 57 1. any identifier that containers two consecutive underscores (i.e. `__`) _anywhere_ in its name. 58 59User code is _prohibited_ from using such identifiers. 60 61Now let's look at what this means for `TEST` and `TEST_F`. 62 63Currently `TEST(TestCaseName, TestName)` generates a class named 64`TestCaseName_TestName_Test`. What happens if `TestCaseName` or `TestName` 65contains `_`? 66 67 1. If `TestCaseName` starts with an `_` followed by an upper-case letter (say, `_Foo`), we end up with `_Foo_TestName_Test`, which is reserved and thus invalid. 68 1. If `TestCaseName` ends with an `_` (say, `Foo_`), we get `Foo__TestName_Test`, which is invalid. 69 1. If `TestName` starts with an `_` (say, `_Bar`), we get `TestCaseName__Bar_Test`, which is invalid. 70 1. If `TestName` ends with an `_` (say, `Bar_`), we get `TestCaseName_Bar__Test`, which is invalid. 71 72So clearly `TestCaseName` and `TestName` cannot start or end with `_` 73(Actually, `TestCaseName` can start with `_` -- as long as the `_` isn't 74followed by an upper-case letter. But that's getting complicated. So 75for simplicity we just say that it cannot start with `_`.). 76 77It may seem fine for `TestCaseName` and `TestName` to contain `_` in the 78middle. However, consider this: 79``` cpp 80TEST(Time, Flies_Like_An_Arrow) { ... } 81TEST(Time_Flies, Like_An_Arrow) { ... } 82``` 83 84Now, the two `TEST`s will both generate the same class 85(`Time_Files_Like_An_Arrow_Test`). That's not good. 86 87So for simplicity, we just ask the users to avoid `_` in `TestCaseName` 88and `TestName`. The rule is more constraining than necessary, but it's 89simple and easy to remember. It also gives Google Test some wiggle 90room in case its implementation needs to change in the future. 91 92If you violate the rule, there may not be immediately consequences, 93but your test may (just may) break with a new compiler (or a new 94version of the compiler you are using) or with a new version of Google 95Test. Therefore it's best to follow the rule. 96 97## Why is it not recommended to install a pre-compiled copy of Google Test (for example, into /usr/local)? ## 98 99In the early days, we said that you could install 100compiled Google Test libraries on `*`nix systems using `make install`. 101Then every user of your machine can write tests without 102recompiling Google Test. 103 104This seemed like a good idea, but it has a 105got-cha: every user needs to compile his tests using the _same_ compiler 106flags used to compile the installed Google Test libraries; otherwise 107he may run into undefined behaviors (i.e. the tests can behave 108strangely and may even crash for no obvious reasons). 109 110Why? Because C++ has this thing called the One-Definition Rule: if 111two C++ source files contain different definitions of the same 112class/function/variable, and you link them together, you violate the 113rule. The linker may or may not catch the error (in many cases it's 114not required by the C++ standard to catch the violation). If it 115doesn't, you get strange run-time behaviors that are unexpected and 116hard to debug. 117 118If you compile Google Test and your test code using different compiler 119flags, they may see different definitions of the same 120class/function/variable (e.g. due to the use of `#if` in Google Test). 121Therefore, for your sanity, we recommend to avoid installing pre-compiled 122Google Test libraries. Instead, each project should compile 123Google Test itself such that it can be sure that the same flags are 124used for both Google Test and the tests. 125 126## How do I generate 64-bit binaries on Windows (using Visual Studio 2008)? ## 127 128(Answered by Trevor Robinson) 129 130Load the supplied Visual Studio solution file, either `msvc\gtest-md.sln` or 131`msvc\gtest.sln`. Go through the migration wizard to migrate the 132solution and project files to Visual Studio 2008. Select 133`Configuration Manager...` from the `Build` menu. Select `<New...>` from 134the `Active solution platform` dropdown. Select `x64` from the new 135platform dropdown, leave `Copy settings from` set to `Win32` and 136`Create new project platforms` checked, then click `OK`. You now have 137`Win32` and `x64` platform configurations, selectable from the 138`Standard` toolbar, which allow you to toggle between building 32-bit or 13964-bit binaries (or both at once using Batch Build). 140 141In order to prevent build output files from overwriting one another, 142you'll need to change the `Intermediate Directory` settings for the 143newly created platform configuration across all the projects. To do 144this, multi-select (e.g. using shift-click) all projects (but not the 145solution) in the `Solution Explorer`. Right-click one of them and 146select `Properties`. In the left pane, select `Configuration Properties`, 147and from the `Configuration` dropdown, select `All Configurations`. 148Make sure the selected platform is `x64`. For the 149`Intermediate Directory` setting, change the value from 150`$(PlatformName)\$(ConfigurationName)` to 151`$(OutDir)\$(ProjectName)`. Click `OK` and then build the 152solution. When the build is complete, the 64-bit binaries will be in 153the `msvc\x64\Debug` directory. 154 155## Can I use Google Test on MinGW? ## 156 157We haven't tested this ourselves, but Per Abrahamsen reported that he 158was able to compile and install Google Test successfully when using 159MinGW from Cygwin. You'll need to configure it with: 160 161`PATH/TO/configure CC="gcc -mno-cygwin" CXX="g++ -mno-cygwin"` 162 163You should be able to replace the `-mno-cygwin` option with direct links 164to the real MinGW binaries, but we haven't tried that. 165 166Caveats: 167 168 * There are many warnings when compiling. 169 * `make check` will produce some errors as not all tests for Google Test itself are compatible with MinGW. 170 171We also have reports on successful cross compilation of Google Test 172MinGW binaries on Linux using 173[these instructions](http://wiki.wxwidgets.org/Cross-Compiling_Under_Linux#Cross-compiling_under_Linux_for_MS_Windows) 174on the WxWidgets site. 175 176Please contact `googletestframework@googlegroups.com` if you are 177interested in improving the support for MinGW. 178 179## Why does Google Test support EXPECT\_EQ(NULL, ptr) and ASSERT\_EQ(NULL, ptr) but not EXPECT\_NE(NULL, ptr) and ASSERT\_NE(NULL, ptr)? ## 180 181Due to some peculiarity of C++, it requires some non-trivial template 182meta programming tricks to support using `NULL` as an argument of the 183`EXPECT_XX()` and `ASSERT_XX()` macros. Therefore we only do it where 184it's most needed (otherwise we make the implementation of Google Test 185harder to maintain and more error-prone than necessary). 186 187The `EXPECT_EQ()` macro takes the _expected_ value as its first 188argument and the _actual_ value as the second. It's reasonable that 189someone wants to write `EXPECT_EQ(NULL, some_expression)`, and this 190indeed was requested several times. Therefore we implemented it. 191 192The need for `EXPECT_NE(NULL, ptr)` isn't nearly as strong. When the 193assertion fails, you already know that `ptr` must be `NULL`, so it 194doesn't add any information to print ptr in this case. That means 195`EXPECT_TRUE(ptr != NULL)` works just as well. 196 197If we were to support `EXPECT_NE(NULL, ptr)`, for consistency we'll 198have to support `EXPECT_NE(ptr, NULL)` as well, as unlike `EXPECT_EQ`, 199we don't have a convention on the order of the two arguments for 200`EXPECT_NE`. This means using the template meta programming tricks 201twice in the implementation, making it even harder to understand and 202maintain. We believe the benefit doesn't justify the cost. 203 204Finally, with the growth of Google Mock's [matcher](../../googlemock/docs/CookBook.md#using-matchers-in-google-test-assertions) library, we are 205encouraging people to use the unified `EXPECT_THAT(value, matcher)` 206syntax more often in tests. One significant advantage of the matcher 207approach is that matchers can be easily combined to form new matchers, 208while the `EXPECT_NE`, etc, macros cannot be easily 209combined. Therefore we want to invest more in the matchers than in the 210`EXPECT_XX()` macros. 211 212## Does Google Test support running tests in parallel? ## 213 214Test runners tend to be tightly coupled with the build/test 215environment, and Google Test doesn't try to solve the problem of 216running tests in parallel. Instead, we tried to make Google Test work 217nicely with test runners. For example, Google Test's XML report 218contains the time spent on each test, and its `gtest_list_tests` and 219`gtest_filter` flags can be used for splitting the execution of test 220methods into multiple processes. These functionalities can help the 221test runner run the tests in parallel. 222 223## Why don't Google Test run the tests in different threads to speed things up? ## 224 225It's difficult to write thread-safe code. Most tests are not written 226with thread-safety in mind, and thus may not work correctly in a 227multi-threaded setting. 228 229If you think about it, it's already hard to make your code work when 230you know what other threads are doing. It's much harder, and 231sometimes even impossible, to make your code work when you don't know 232what other threads are doing (remember that test methods can be added, 233deleted, or modified after your test was written). If you want to run 234the tests in parallel, you'd better run them in different processes. 235 236## Why aren't Google Test assertions implemented using exceptions? ## 237 238Our original motivation was to be able to use Google Test in projects 239that disable exceptions. Later we realized some additional benefits 240of this approach: 241 242 1. Throwing in a destructor is undefined behavior in C++. Not using exceptions means Google Test's assertions are safe to use in destructors. 243 1. The `EXPECT_*` family of macros will continue even after a failure, allowing multiple failures in a `TEST` to be reported in a single run. This is a popular feature, as in C++ the edit-compile-test cycle is usually quite long and being able to fixing more than one thing at a time is a blessing. 244 1. If assertions are implemented using exceptions, a test may falsely ignore a failure if it's caught by user code: 245``` cpp 246try { ... ASSERT_TRUE(...) ... } 247catch (...) { ... } 248``` 249The above code will pass even if the `ASSERT_TRUE` throws. While it's unlikely for someone to write this in a test, it's possible to run into this pattern when you write assertions in callbacks that are called by the code under test. 250 251The downside of not using exceptions is that `ASSERT_*` (implemented 252using `return`) will only abort the current function, not the current 253`TEST`. 254 255## Why do we use two different macros for tests with and without fixtures? ## 256 257Unfortunately, C++'s macro system doesn't allow us to use the same 258macro for both cases. One possibility is to provide only one macro 259for tests with fixtures, and require the user to define an empty 260fixture sometimes: 261 262``` cpp 263class FooTest : public ::testing::Test {}; 264 265TEST_F(FooTest, DoesThis) { ... } 266``` 267or 268``` cpp 269typedef ::testing::Test FooTest; 270 271TEST_F(FooTest, DoesThat) { ... } 272``` 273 274Yet, many people think this is one line too many. :-) Our goal was to 275make it really easy to write tests, so we tried to make simple tests 276trivial to create. That means using a separate macro for such tests. 277 278We think neither approach is ideal, yet either of them is reasonable. 279In the end, it probably doesn't matter much either way. 280 281## Why don't we use structs as test fixtures? ## 282 283We like to use structs only when representing passive data. This 284distinction between structs and classes is good for documenting the 285intent of the code's author. Since test fixtures have logic like 286`SetUp()` and `TearDown()`, they are better defined as classes. 287 288## Why are death tests implemented as assertions instead of using a test runner? ## 289 290Our goal was to make death tests as convenient for a user as C++ 291possibly allows. In particular: 292 293 * The runner-style requires to split the information into two pieces: the definition of the death test itself, and the specification for the runner on how to run the death test and what to expect. The death test would be written in C++, while the runner spec may or may not be. A user needs to carefully keep the two in sync. `ASSERT_DEATH(statement, expected_message)` specifies all necessary information in one place, in one language, without boilerplate code. It is very declarative. 294 * `ASSERT_DEATH` has a similar syntax and error-reporting semantics as other Google Test assertions, and thus is easy to learn. 295 * `ASSERT_DEATH` can be mixed with other assertions and other logic at your will. You are not limited to one death test per test method. For example, you can write something like: 296``` cpp 297 if (FooCondition()) { 298 ASSERT_DEATH(Bar(), "blah"); 299 } else { 300 ASSERT_EQ(5, Bar()); 301 } 302``` 303If you prefer one death test per test method, you can write your tests in that style too, but we don't want to impose that on the users. The fewer artificial limitations the better. 304 * `ASSERT_DEATH` can reference local variables in the current function, and you can decide how many death tests you want based on run-time information. For example, 305``` cpp 306 const int count = GetCount(); // Only known at run time. 307 for (int i = 1; i <= count; i++) { 308 ASSERT_DEATH({ 309 double* buffer = new double[i]; 310 ... initializes buffer ... 311 Foo(buffer, i) 312 }, "blah blah"); 313 } 314``` 315The runner-based approach tends to be more static and less flexible, or requires more user effort to get this kind of flexibility. 316 317Another interesting thing about `ASSERT_DEATH` is that it calls `fork()` 318to create a child process to run the death test. This is lightening 319fast, as `fork()` uses copy-on-write pages and incurs almost zero 320overhead, and the child process starts from the user-supplied 321statement directly, skipping all global and local initialization and 322any code leading to the given statement. If you launch the child 323process from scratch, it can take seconds just to load everything and 324start running if the test links to many libraries dynamically. 325 326## My death test modifies some state, but the change seems lost after the death test finishes. Why? ## 327 328Death tests (`EXPECT_DEATH`, etc) are executed in a sub-process s.t. the 329expected crash won't kill the test program (i.e. the parent process). As a 330result, any in-memory side effects they incur are observable in their 331respective sub-processes, but not in the parent process. You can think of them 332as running in a parallel universe, more or less. 333 334## The compiler complains about "undefined references" to some static const member variables, but I did define them in the class body. What's wrong? ## 335 336If your class has a static data member: 337 338``` cpp 339// foo.h 340class Foo { 341 ... 342 static const int kBar = 100; 343}; 344``` 345 346You also need to define it _outside_ of the class body in `foo.cc`: 347 348``` cpp 349const int Foo::kBar; // No initializer here. 350``` 351 352Otherwise your code is **invalid C++**, and may break in unexpected ways. In 353particular, using it in Google Test comparison assertions (`EXPECT_EQ`, etc) 354will generate an "undefined reference" linker error. 355 356## I have an interface that has several implementations. Can I write a set of tests once and repeat them over all the implementations? ## 357 358Google Test doesn't yet have good support for this kind of tests, or 359data-driven tests in general. We hope to be able to make improvements in this 360area soon. 361 362## Can I derive a test fixture from another? ## 363 364Yes. 365 366Each test fixture has a corresponding and same named test case. This means only 367one test case can use a particular fixture. Sometimes, however, multiple test 368cases may want to use the same or slightly different fixtures. For example, you 369may want to make sure that all of a GUI library's test cases don't leak 370important system resources like fonts and brushes. 371 372In Google Test, you share a fixture among test cases by putting the shared 373logic in a base test fixture, then deriving from that base a separate fixture 374for each test case that wants to use this common logic. You then use `TEST_F()` 375to write tests using each derived fixture. 376 377Typically, your code looks like this: 378 379``` cpp 380// Defines a base test fixture. 381class BaseTest : public ::testing::Test { 382 protected: 383 ... 384}; 385 386// Derives a fixture FooTest from BaseTest. 387class FooTest : public BaseTest { 388 protected: 389 virtual void SetUp() { 390 BaseTest::SetUp(); // Sets up the base fixture first. 391 ... additional set-up work ... 392 } 393 virtual void TearDown() { 394 ... clean-up work for FooTest ... 395 BaseTest::TearDown(); // Remember to tear down the base fixture 396 // after cleaning up FooTest! 397 } 398 ... functions and variables for FooTest ... 399}; 400 401// Tests that use the fixture FooTest. 402TEST_F(FooTest, Bar) { ... } 403TEST_F(FooTest, Baz) { ... } 404 405... additional fixtures derived from BaseTest ... 406``` 407 408If necessary, you can continue to derive test fixtures from a derived fixture. 409Google Test has no limit on how deep the hierarchy can be. 410 411For a complete example using derived test fixtures, see 412[sample5](../samples/sample5_unittest.cc). 413 414## My compiler complains "void value not ignored as it ought to be." What does this mean? ## 415 416You're probably using an `ASSERT_*()` in a function that doesn't return `void`. 417`ASSERT_*()` can only be used in `void` functions. 418 419## My death test hangs (or seg-faults). How do I fix it? ## 420 421In Google Test, death tests are run in a child process and the way they work is 422delicate. To write death tests you really need to understand how they work. 423Please make sure you have read this. 424 425In particular, death tests don't like having multiple threads in the parent 426process. So the first thing you can try is to eliminate creating threads 427outside of `EXPECT_DEATH()`. 428 429Sometimes this is impossible as some library you must use may be creating 430threads before `main()` is even reached. In this case, you can try to minimize 431the chance of conflicts by either moving as many activities as possible inside 432`EXPECT_DEATH()` (in the extreme case, you want to move everything inside), or 433leaving as few things as possible in it. Also, you can try to set the death 434test style to `"threadsafe"`, which is safer but slower, and see if it helps. 435 436If you go with thread-safe death tests, remember that they rerun the test 437program from the beginning in the child process. Therefore make sure your 438program can run side-by-side with itself and is deterministic. 439 440In the end, this boils down to good concurrent programming. You have to make 441sure that there is no race conditions or dead locks in your program. No silver 442bullet - sorry! 443 444## Should I use the constructor/destructor of the test fixture or the set-up/tear-down function? ## 445 446The first thing to remember is that Google Test does not reuse the 447same test fixture object across multiple tests. For each `TEST_F`, 448Google Test will create a fresh test fixture object, _immediately_ 449call `SetUp()`, run the test body, call `TearDown()`, and then 450_immediately_ delete the test fixture object. 451 452When you need to write per-test set-up and tear-down logic, you have 453the choice between using the test fixture constructor/destructor or 454`SetUp()/TearDown()`. The former is usually preferred, as it has the 455following benefits: 456 457 * By initializing a member variable in the constructor, we have the option to make it `const`, which helps prevent accidental changes to its value and makes the tests more obviously correct. 458 * In case we need to subclass the test fixture class, the subclass' constructor is guaranteed to call the base class' constructor first, and the subclass' destructor is guaranteed to call the base class' destructor afterward. With `SetUp()/TearDown()`, a subclass may make the mistake of forgetting to call the base class' `SetUp()/TearDown()` or call them at the wrong moment. 459 460You may still want to use `SetUp()/TearDown()` in the following rare cases: 461 * If the tear-down operation could throw an exception, you must use `TearDown()` as opposed to the destructor, as throwing in a destructor leads to undefined behavior and usually will kill your program right away. Note that many standard libraries (like STL) may throw when exceptions are enabled in the compiler. Therefore you should prefer `TearDown()` if you want to write portable tests that work with or without exceptions. 462 * The assertion macros throw an exception when flag `--gtest_throw_on_failure` is specified. Therefore, you shouldn't use Google Test assertions in a destructor if you plan to run your tests with this flag. 463 * In a constructor or destructor, you cannot make a virtual function call on this object. (You can call a method declared as virtual, but it will be statically bound.) Therefore, if you need to call a method that will be overriden in a derived class, you have to use `SetUp()/TearDown()`. 464 465## The compiler complains "no matching function to call" when I use ASSERT\_PREDn. How do I fix it? ## 466 467If the predicate function you use in `ASSERT_PRED*` or `EXPECT_PRED*` is 468overloaded or a template, the compiler will have trouble figuring out which 469overloaded version it should use. `ASSERT_PRED_FORMAT*` and 470`EXPECT_PRED_FORMAT*` don't have this problem. 471 472If you see this error, you might want to switch to 473`(ASSERT|EXPECT)_PRED_FORMAT*`, which will also give you a better failure 474message. If, however, that is not an option, you can resolve the problem by 475explicitly telling the compiler which version to pick. 476 477For example, suppose you have 478 479``` cpp 480bool IsPositive(int n) { 481 return n > 0; 482} 483bool IsPositive(double x) { 484 return x > 0; 485} 486``` 487 488you will get a compiler error if you write 489 490``` cpp 491EXPECT_PRED1(IsPositive, 5); 492``` 493 494However, this will work: 495 496``` cpp 497EXPECT_PRED1(*static_cast<bool (*)(int)>*(IsPositive), 5); 498``` 499 500(The stuff inside the angled brackets for the `static_cast` operator is the 501type of the function pointer for the `int`-version of `IsPositive()`.) 502 503As another example, when you have a template function 504 505``` cpp 506template <typename T> 507bool IsNegative(T x) { 508 return x < 0; 509} 510``` 511 512you can use it in a predicate assertion like this: 513 514``` cpp 515ASSERT_PRED1(IsNegative*<int>*, -5); 516``` 517 518Things are more interesting if your template has more than one parameters. The 519following won't compile: 520 521``` cpp 522ASSERT_PRED2(*GreaterThan<int, int>*, 5, 0); 523``` 524 525 526as the C++ pre-processor thinks you are giving `ASSERT_PRED2` 4 arguments, 527which is one more than expected. The workaround is to wrap the predicate 528function in parentheses: 529 530``` cpp 531ASSERT_PRED2(*(GreaterThan<int, int>)*, 5, 0); 532``` 533 534 535## My compiler complains about "ignoring return value" when I call RUN\_ALL\_TESTS(). Why? ## 536 537Some people had been ignoring the return value of `RUN_ALL_TESTS()`. That is, 538instead of 539 540``` cpp 541return RUN_ALL_TESTS(); 542``` 543 544they write 545 546``` cpp 547RUN_ALL_TESTS(); 548``` 549 550This is wrong and dangerous. A test runner needs to see the return value of 551`RUN_ALL_TESTS()` in order to determine if a test has passed. If your `main()` 552function ignores it, your test will be considered successful even if it has a 553Google Test assertion failure. Very bad. 554 555To help the users avoid this dangerous bug, the implementation of 556`RUN_ALL_TESTS()` causes gcc to raise this warning, when the return value is 557ignored. If you see this warning, the fix is simple: just make sure its value 558is used as the return value of `main()`. 559 560## My compiler complains that a constructor (or destructor) cannot return a value. What's going on? ## 561 562Due to a peculiarity of C++, in order to support the syntax for streaming 563messages to an `ASSERT_*`, e.g. 564 565``` cpp 566ASSERT_EQ(1, Foo()) << "blah blah" << foo; 567``` 568 569we had to give up using `ASSERT*` and `FAIL*` (but not `EXPECT*` and 570`ADD_FAILURE*`) in constructors and destructors. The workaround is to move the 571content of your constructor/destructor to a private void member function, or 572switch to `EXPECT_*()` if that works. This section in the user's guide explains 573it. 574 575## My set-up function is not called. Why? ## 576 577C++ is case-sensitive. It should be spelled as `SetUp()`. Did you 578spell it as `Setup()`? 579 580Similarly, sometimes people spell `SetUpTestCase()` as `SetupTestCase()` and 581wonder why it's never called. 582 583## How do I jump to the line of a failure in Emacs directly? ## 584 585Google Test's failure message format is understood by Emacs and many other 586IDEs, like acme and XCode. If a Google Test message is in a compilation buffer 587in Emacs, then it's clickable. You can now hit `enter` on a message to jump to 588the corresponding source code, or use `C-x `` to jump to the next failure. 589 590## I have several test cases 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. ## 591 592You don't have to. Instead of 593 594``` cpp 595class FooTest : public BaseTest {}; 596 597TEST_F(FooTest, Abc) { ... } 598TEST_F(FooTest, Def) { ... } 599 600class BarTest : public BaseTest {}; 601 602TEST_F(BarTest, Abc) { ... } 603TEST_F(BarTest, Def) { ... } 604``` 605 606you can simply `typedef` the test fixtures: 607``` cpp 608typedef BaseTest FooTest; 609 610TEST_F(FooTest, Abc) { ... } 611TEST_F(FooTest, Def) { ... } 612 613typedef BaseTest BarTest; 614 615TEST_F(BarTest, Abc) { ... } 616TEST_F(BarTest, Def) { ... } 617``` 618 619## The Google Test output is buried in a whole bunch of log messages. What do I do? ## 620 621The Google Test output is meant to be a concise and human-friendly report. If 622your test generates textual output itself, it will mix with the Google Test 623output, making it hard to read. However, there is an easy solution to this 624problem. 625 626Since most log messages go to stderr, we decided to let Google Test output go 627to stdout. This way, you can easily separate the two using redirection. For 628example: 629``` 630./my_test > googletest_output.txt 631``` 632 633## Why should I prefer test fixtures over global variables? ## 634 635There are several good reasons: 636 1. It's likely your test needs to change the states of its global variables. This makes it difficult to keep side effects from escaping one test and contaminating others, making debugging difficult. By using fixtures, each test has a fresh set of variables that's different (but with the same names). Thus, tests are kept independent of each other. 637 1. Global variables pollute the global namespace. 638 1. Test fixtures can be reused via subclassing, which cannot be done easily with global variables. This is useful if many test cases have something in common. 639 640## How do I test private class members without writing FRIEND\_TEST()s? ## 641 642You should try to write testable code, which means classes should be easily 643tested from their public interface. One way to achieve this is the Pimpl idiom: 644you move all private members of a class into a helper class, and make all 645members of the helper class public. 646 647You have several other options that don't require using `FRIEND_TEST`: 648 * Write the tests as members of the fixture class: 649``` cpp 650class Foo { 651 friend class FooTest; 652 ... 653}; 654 655class FooTest : public ::testing::Test { 656 protected: 657 ... 658 void Test1() {...} // This accesses private members of class Foo. 659 void Test2() {...} // So does this one. 660}; 661 662TEST_F(FooTest, Test1) { 663 Test1(); 664} 665 666TEST_F(FooTest, Test2) { 667 Test2(); 668} 669``` 670 * In the fixture class, write accessors for the tested class' private members, then use the accessors in your tests: 671``` cpp 672class Foo { 673 friend class FooTest; 674 ... 675}; 676 677class FooTest : public ::testing::Test { 678 protected: 679 ... 680 T1 get_private_member1(Foo* obj) { 681 return obj->private_member1_; 682 } 683}; 684 685TEST_F(FooTest, Test1) { 686 ... 687 get_private_member1(x) 688 ... 689} 690``` 691 * If the methods are declared **protected**, you can change their access level in a test-only subclass: 692``` cpp 693class YourClass { 694 ... 695 protected: // protected access for testability. 696 int DoSomethingReturningInt(); 697 ... 698}; 699 700// in the your_class_test.cc file: 701class TestableYourClass : public YourClass { 702 ... 703 public: using YourClass::DoSomethingReturningInt; // changes access rights 704 ... 705}; 706 707TEST_F(YourClassTest, DoSomethingTest) { 708 TestableYourClass obj; 709 assertEquals(expected_value, obj.DoSomethingReturningInt()); 710} 711``` 712 713## How do I test private class static members without writing FRIEND\_TEST()s? ## 714 715We find private static methods clutter the header file. They are 716implementation details and ideally should be kept out of a .h. So often I make 717them free functions instead. 718 719Instead of: 720``` cpp 721// foo.h 722class Foo { 723 ... 724 private: 725 static bool Func(int n); 726}; 727 728// foo.cc 729bool Foo::Func(int n) { ... } 730 731// foo_test.cc 732EXPECT_TRUE(Foo::Func(12345)); 733``` 734 735You probably should better write: 736``` cpp 737// foo.h 738class Foo { 739 ... 740}; 741 742// foo.cc 743namespace internal { 744 bool Func(int n) { ... } 745} 746 747// foo_test.cc 748namespace internal { 749 bool Func(int n); 750} 751 752EXPECT_TRUE(internal::Func(12345)); 753``` 754 755## I would like to run a test several times with different parameters. Do I need to write several similar copies of it? ## 756 757No. You can use a feature called [value-parameterized tests](AdvancedGuide.md#Value_Parameterized_Tests) which 758lets you repeat your tests with different parameters, without defining it more than once. 759 760## How do I test a file that defines main()? ## 761 762To test a `foo.cc` file, you need to compile and link it into your unit test 763program. However, when the file contains a definition for the `main()` 764function, it will clash with the `main()` of your unit test, and will result in 765a build error. 766 767The right solution is to split it into three files: 768 1. `foo.h` which contains the declarations, 769 1. `foo.cc` which contains the definitions except `main()`, and 770 1. `foo_main.cc` which contains nothing but the definition of `main()`. 771 772Then `foo.cc` can be easily tested. 773 774If you are adding tests to an existing file and don't want an intrusive change 775like this, there is a hack: just include the entire `foo.cc` file in your unit 776test. For example: 777``` cpp 778// File foo_unittest.cc 779 780// The headers section 781... 782 783// Renames main() in foo.cc to make room for the unit test main() 784#define main FooMain 785 786#include "a/b/foo.cc" 787 788// The tests start here. 789... 790``` 791 792 793However, please remember this is a hack and should only be used as the last 794resort. 795 796## What can the statement argument in ASSERT\_DEATH() be? ## 797 798`ASSERT_DEATH(_statement_, _regex_)` (or any death assertion macro) can be used 799wherever `_statement_` is valid. So basically `_statement_` can be any C++ 800statement that makes sense in the current context. In particular, it can 801reference global and/or local variables, and can be: 802 * a simple function call (often the case), 803 * a complex expression, or 804 * a compound statement. 805 806Some examples are shown here: 807 808``` cpp 809// A death test can be a simple function call. 810TEST(MyDeathTest, FunctionCall) { 811 ASSERT_DEATH(Xyz(5), "Xyz failed"); 812} 813 814// Or a complex expression that references variables and functions. 815TEST(MyDeathTest, ComplexExpression) { 816 const bool c = Condition(); 817 ASSERT_DEATH((c ? Func1(0) : object2.Method("test")), 818 "(Func1|Method) failed"); 819} 820 821// Death assertions can be used any where in a function. In 822// particular, they can be inside a loop. 823TEST(MyDeathTest, InsideLoop) { 824 // Verifies that Foo(0), Foo(1), ..., and Foo(4) all die. 825 for (int i = 0; i < 5; i++) { 826 EXPECT_DEATH_M(Foo(i), "Foo has \\d+ errors", 827 ::testing::Message() << "where i is " << i); 828 } 829} 830 831// A death assertion can contain a compound statement. 832TEST(MyDeathTest, CompoundStatement) { 833 // Verifies that at lease one of Bar(0), Bar(1), ..., and 834 // Bar(4) dies. 835 ASSERT_DEATH({ 836 for (int i = 0; i < 5; i++) { 837 Bar(i); 838 } 839 }, 840 "Bar has \\d+ errors");} 841``` 842 843`googletest_unittest.cc` contains more examples if you are interested. 844 845## What syntax does the regular expression in ASSERT\_DEATH use? ## 846 847On POSIX systems, Google Test uses the POSIX Extended regular 848expression syntax 849(http://en.wikipedia.org/wiki/Regular_expression#POSIX_Extended_Regular_Expressions). 850On Windows, it uses a limited variant of regular expression 851syntax. For more details, see the 852[regular expression syntax](AdvancedGuide.md#Regular_Expression_Syntax). 853 854## I have a fixture class Foo, but TEST\_F(Foo, Bar) gives me error "no matching function for call to Foo::Foo()". Why? ## 855 856Google Test needs to be able to create objects of your test fixture class, so 857it must have a default constructor. Normally the compiler will define one for 858you. However, there are cases where you have to define your own: 859 * If you explicitly declare a non-default constructor for class `Foo`, then you need to define a default constructor, even if it would be empty. 860 * If `Foo` has a const non-static data member, then you have to define the default constructor _and_ initialize the const member in the initializer list of the constructor. (Early versions of `gcc` doesn't force you to initialize the const member. It's a bug that has been fixed in `gcc 4`.) 861 862## Why does ASSERT\_DEATH complain about previous threads that were already joined? ## 863 864With the Linux pthread library, there is no turning back once you cross the 865line from single thread to multiple threads. The first time you create a 866thread, a manager thread is created in addition, so you get 3, not 2, threads. 867Later when the thread you create joins the main thread, the thread count 868decrements by 1, but the manager thread will never be killed, so you still have 8692 threads, which means you cannot safely run a death test. 870 871The new NPTL thread library doesn't suffer from this problem, as it doesn't 872create a manager thread. However, if you don't control which machine your test 873runs on, you shouldn't depend on this. 874 875## Why does Google Test require the entire test case, instead of individual tests, to be named FOODeathTest when it uses ASSERT\_DEATH? ## 876 877Google Test does not interleave tests from different test cases. That is, it 878runs all tests in one test case first, and then runs all tests in the next test 879case, and so on. Google Test does this because it needs to set up a test case 880before the first test in it is run, and tear it down afterwords. Splitting up 881the test case would require multiple set-up and tear-down processes, which is 882inefficient and makes the semantics unclean. 883 884If we were to determine the order of tests based on test name instead of test 885case name, then we would have a problem with the following situation: 886 887``` cpp 888TEST_F(FooTest, AbcDeathTest) { ... } 889TEST_F(FooTest, Uvw) { ... } 890 891TEST_F(BarTest, DefDeathTest) { ... } 892TEST_F(BarTest, Xyz) { ... } 893``` 894 895Since `FooTest.AbcDeathTest` needs to run before `BarTest.Xyz`, and we don't 896interleave tests from different test cases, we need to run all tests in the 897`FooTest` case before running any test in the `BarTest` case. This contradicts 898with the requirement to run `BarTest.DefDeathTest` before `FooTest.Uvw`. 899 900## But I don't like calling my entire test case FOODeathTest when it contains both death tests and non-death tests. What do I do? ## 901 902You don't have to, but if you like, you may split up the test case into 903`FooTest` and `FooDeathTest`, where the names make it clear that they are 904related: 905 906``` cpp 907class FooTest : public ::testing::Test { ... }; 908 909TEST_F(FooTest, Abc) { ... } 910TEST_F(FooTest, Def) { ... } 911 912typedef FooTest FooDeathTest; 913 914TEST_F(FooDeathTest, Uvw) { ... EXPECT_DEATH(...) ... } 915TEST_F(FooDeathTest, Xyz) { ... ASSERT_DEATH(...) ... } 916``` 917 918## The compiler complains about "no match for 'operator<<'" when I use an assertion. What gives? ## 919 920If you use a user-defined type `FooType` in an assertion, you must make sure 921there is an `std::ostream& operator<<(std::ostream&, const FooType&)` function 922defined such that we can print a value of `FooType`. 923 924In addition, if `FooType` is declared in a name space, the `<<` operator also 925needs to be defined in the _same_ name space. 926 927## How do I suppress the memory leak messages on Windows? ## 928 929Since the statically initialized Google Test singleton requires allocations on 930the heap, the Visual C++ memory leak detector will report memory leaks at the 931end of the program run. The easiest way to avoid this is to use the 932`_CrtMemCheckpoint` and `_CrtMemDumpAllObjectsSince` calls to not report any 933statically initialized heap objects. See MSDN for more details and additional 934heap check/debug routines. 935 936## I am building my project with Google Test in Visual Studio and all I'm getting is a bunch of linker errors (or warnings). Help! ## 937 938You may get a number of the following linker error or warnings if you 939attempt to link your test project with the Google Test library when 940your project and the are not built using the same compiler settings. 941 942 * LNK2005: symbol already defined in object 943 * LNK4217: locally defined symbol 'symbol' imported in function 'function' 944 * LNK4049: locally defined symbol 'symbol' imported 945 946The Google Test project (gtest.vcproj) has the Runtime Library option 947set to /MT (use multi-threaded static libraries, /MTd for debug). If 948your project uses something else, for example /MD (use multi-threaded 949DLLs, /MDd for debug), you need to change the setting in the Google 950Test project to match your project's. 951 952To update this setting open the project properties in the Visual 953Studio IDE then select the branch Configuration Properties | C/C++ | 954Code Generation and change the option "Runtime Library". You may also try 955using gtest-md.vcproj instead of gtest.vcproj. 956 957## I put my tests in a library and Google Test doesn't run them. What's happening? ## 958Have you read a 959[warning](Primer.md#important-note-for-visual-c-users) on 960the Google Test Primer page? 961 962## I want to use Google Test with Visual Studio but don't know where to start. ## 963Many people are in your position and one of the posted his solution to 964our mailing list. 965 966## I am seeing compile errors mentioning std::type\_traits when I try to use Google Test on Solaris. ## 967Google Test uses parts of the standard C++ library that SunStudio does not support. 968Our users reported success using alternative implementations. Try running the build after runing this commad: 969 970`export CC=cc CXX=CC CXXFLAGS='-library=stlport4'` 971 972## How can my code detect if it is running in a test? ## 973 974If you write code that sniffs whether it's running in a test and does 975different things accordingly, you are leaking test-only logic into 976production code and there is no easy way to ensure that the test-only 977code paths aren't run by mistake in production. Such cleverness also 978leads to 979[Heisenbugs](http://en.wikipedia.org/wiki/Unusual_software_bug#Heisenbug). 980Therefore we strongly advise against the practice, and Google Test doesn't 981provide a way to do it. 982 983In general, the recommended way to cause the code to behave 984differently under test is [dependency injection](http://jamesshore.com/Blog/Dependency-Injection-Demystified.html). 985You can inject different functionality from the test and from the 986production code. Since your production code doesn't link in the 987for-test logic at all, there is no danger in accidentally running it. 988 989However, if you _really_, _really_, _really_ have no choice, and if 990you follow the rule of ending your test program names with `_test`, 991you can use the _horrible_ hack of sniffing your executable name 992(`argv[0]` in `main()`) to know whether the code is under test. 993 994## Google Test defines a macro that clashes with one defined by another library. How do I deal with that? ## 995 996In C++, macros don't obey namespaces. Therefore two libraries that 997both define a macro of the same name will clash if you `#include` both 998definitions. In case a Google Test macro clashes with another 999library, you can force Google Test to rename its macro to avoid the 1000conflict. 1001 1002Specifically, if both Google Test and some other code define macro 1003`FOO`, you can add 1004``` 1005 -DGTEST_DONT_DEFINE_FOO=1 1006``` 1007to the compiler flags to tell Google Test to change the macro's name 1008from `FOO` to `GTEST_FOO`. For example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll need to write 1009``` cpp 1010 GTEST_TEST(SomeTest, DoesThis) { ... } 1011``` 1012instead of 1013``` cpp 1014 TEST(SomeTest, DoesThis) { ... } 1015``` 1016in order to define a test. 1017 1018Currently, the following `TEST`, `FAIL`, `SUCCEED`, and the basic comparison assertion macros can have alternative names. You can see the full list of covered macros [here](http://www.google.com/codesearch?q=if+!GTEST_DONT_DEFINE_\w%2B+package:http://googletest\.googlecode\.com+file:/include/gtest/gtest.h). More information can be found in the "Avoiding Macro Name Clashes" section of the README file. 1019 1020 1021## Is it OK if I have two separate `TEST(Foo, Bar)` test methods defined in different namespaces? ## 1022 1023Yes. 1024 1025The rule is **all test methods in the same test case must use the same fixture class**. This means that the following is **allowed** because both tests use the same fixture class (`::testing::Test`). 1026 1027``` cpp 1028namespace foo { 1029TEST(CoolTest, DoSomething) { 1030 SUCCEED(); 1031} 1032} // namespace foo 1033 1034namespace bar { 1035TEST(CoolTest, DoSomething) { 1036 SUCCEED(); 1037} 1038} // namespace foo 1039``` 1040 1041However, the following code is **not allowed** and will produce a runtime error from Google Test because the test methods are using different test fixture classes with the same test case name. 1042 1043``` cpp 1044namespace foo { 1045class CoolTest : public ::testing::Test {}; // Fixture foo::CoolTest 1046TEST_F(CoolTest, DoSomething) { 1047 SUCCEED(); 1048} 1049} // namespace foo 1050 1051namespace bar { 1052class CoolTest : public ::testing::Test {}; // Fixture: bar::CoolTest 1053TEST_F(CoolTest, DoSomething) { 1054 SUCCEED(); 1055} 1056} // namespace foo 1057``` 1058 1059## How do I build Google Testing Framework with Xcode 4? ## 1060 1061If you try to build Google Test's Xcode project with Xcode 4.0 or later, you may encounter an error message that looks like 1062"Missing SDK in target gtest\_framework: /Developer/SDKs/MacOSX10.4u.sdk". That means that Xcode does not support the SDK the project is targeting. See the Xcode section in the [README](../README.md) file on how to resolve this. 1063 1064## My question is not covered in your FAQ! ## 1065 1066If you cannot find the answer to your question in this FAQ, there are 1067some other resources you can use: 1068 1069 1. read other [wiki pages](../docs), 1070 1. search the mailing list [archive](https://groups.google.com/forum/#!forum/googletestframework), 1071 1. ask it on [googletestframework@googlegroups.com](mailto:googletestframework@googlegroups.com) and someone will answer it (to prevent spam, we require you to join the [discussion group](http://groups.google.com/group/googletestframework) before you can post.). 1072 1073Please note that creating an issue in the 1074[issue tracker](https://github.com/google/googletest/issues) is _not_ 1075a good way to get your answer, as it is monitored infrequently by a 1076very small number of people. 1077 1078When asking a question, it's helpful to provide as much of the 1079following information as possible (people cannot help you if there's 1080not enough information in your question): 1081 1082 * the version (or the commit hash if you check out from Git directly) of Google Test you use (Google Test is under active development, so it's possible that your problem has been solved in a later version), 1083 * your operating system, 1084 * the name and version of your compiler, 1085 * the complete command line flags you give to your compiler, 1086 * the complete compiler error messages (if the question is about compilation), 1087 * the _actual_ code (ideally, a minimal but complete program) that has the problem you encounter. 1088