Blame googletest/docs/V1_5_FAQ.md

Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
If you cannot find the answer to your question here, and you have read
Packit bd1cd8
[Primer](V1_5_Primer.md) and [AdvancedGuide](V1_5_AdvancedGuide.md), send it to
Packit bd1cd8
googletestframework@googlegroups.com.
Packit bd1cd8
Packit bd1cd8
## Why should I use Google Test instead of my favorite C++ testing framework? ##
Packit bd1cd8
Packit bd1cd8
First, let's say clearly that we don't want to get into the debate of
Packit bd1cd8
which C++ testing framework is **the best**.  There exist many fine
Packit bd1cd8
frameworks for writing C++ tests, and we have tremendous respect for
Packit bd1cd8
the developers and users of them.  We don't think there is (or will
Packit bd1cd8
be) a single best framework - you have to pick the right tool for the
Packit bd1cd8
particular task you are tackling.
Packit bd1cd8
Packit bd1cd8
We created Google Test because we couldn't find the right combination
Packit bd1cd8
of features and conveniences in an existing framework to satisfy _our_
Packit bd1cd8
needs.  The following is a list of things that _we_ like about Google
Packit bd1cd8
Test.  We don't claim them to be unique to Google Test - rather, the
Packit bd1cd8
combination of them makes Google Test the choice for us.  We hope this
Packit bd1cd8
list can help you decide whether it is for you too.
Packit bd1cd8
Packit bd1cd8
  * Google Test is designed to be portable.  It works where many STL types (e.g. `std::string` and `std::vector`) don't compile.  It doesn't require exceptions or RTTI.  As a result, it runs on Linux, Mac OS X, Windows and several embedded operating systems.
Packit bd1cd8
  * 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.
Packit bd1cd8
  * 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.
Packit bd1cd8
  * Google Test automatically detects your tests and doesn't require you to enumerate them in order to run them.
Packit bd1cd8
  * No framework can anticipate all your needs, so Google Test provides `EXPECT_PRED*` to make it easy to extend your assertion vocabulary.  For a nicer syntax, you can define your own assertion macros trivially in terms of `EXPECT_PRED*`.
Packit bd1cd8
  * Death tests are pretty handy for ensuring that your asserts in production code are triggered by the right conditions.
Packit bd1cd8
  * `SCOPED_TRACE` helps you understand the context of an assertion failure when it comes from inside a sub-routine or loop.
Packit bd1cd8
  * You can decide which tests to run using name patterns.  This saves time when you want to quickly reproduce a test failure.
Packit bd1cd8
Packit bd1cd8
## How do I generate 64-bit binaries on Windows (using Visual Studio 2008)? ##
Packit bd1cd8
Packit bd1cd8
(Answered by Trevor Robinson)
Packit bd1cd8
Packit bd1cd8
Load the supplied Visual Studio solution file, either `msvc\gtest-md.sln` or
Packit bd1cd8
`msvc\gtest.sln`. Go through the migration wizard to migrate the
Packit bd1cd8
solution and project files to Visual Studio 2008. Select
Packit bd1cd8
`Configuration Manager...` from the `Build` menu. Select `<New...>` from
Packit bd1cd8
the `Active solution platform` dropdown.  Select `x64` from the new
Packit bd1cd8
platform dropdown, leave `Copy settings from` set to `Win32` and
Packit bd1cd8
`Create new project platforms` checked, then click `OK`. You now have
Packit bd1cd8
`Win32` and `x64` platform configurations, selectable from the
Packit bd1cd8
`Standard` toolbar, which allow you to toggle between building 32-bit or
Packit bd1cd8
64-bit binaries (or both at once using Batch Build).
Packit bd1cd8
Packit bd1cd8
In order to prevent build output files from overwriting one another,
Packit bd1cd8
you'll need to change the `Intermediate Directory` settings for the
Packit bd1cd8
newly created platform configuration across all the projects. To do
Packit bd1cd8
this, multi-select (e.g. using shift-click) all projects (but not the
Packit bd1cd8
solution) in the `Solution Explorer`. Right-click one of them and
Packit bd1cd8
select `Properties`. In the left pane, select `Configuration Properties`,
Packit bd1cd8
and from the `Configuration` dropdown, select `All Configurations`.
Packit bd1cd8
Make sure the selected platform is `x64`. For the
Packit bd1cd8
`Intermediate Directory` setting, change the value from
Packit bd1cd8
`$(PlatformName)\$(ConfigurationName)` to
Packit bd1cd8
`$(OutDir)\$(ProjectName)`. Click `OK` and then build the
Packit bd1cd8
solution. When the build is complete, the 64-bit binaries will be in
Packit bd1cd8
the `msvc\x64\Debug` directory.
Packit bd1cd8
Packit bd1cd8
## Can I use Google Test on MinGW? ##
Packit bd1cd8
Packit bd1cd8
We haven't tested this ourselves, but Per Abrahamsen reported that he
Packit bd1cd8
was able to compile and install Google Test successfully when using
Packit bd1cd8
MinGW from Cygwin.  You'll need to configure it with:
Packit bd1cd8
Packit bd1cd8
`PATH/TO/configure CC="gcc -mno-cygwin" CXX="g++ -mno-cygwin"`
Packit bd1cd8
Packit bd1cd8
You should be able to replace the `-mno-cygwin` option with direct links
Packit bd1cd8
to the real MinGW binaries, but we haven't tried that.
Packit bd1cd8
Packit bd1cd8
Caveats:
Packit bd1cd8
Packit bd1cd8
  * There are many warnings when compiling.
Packit bd1cd8
  * `make check` will produce some errors as not all tests for Google Test itself are compatible with MinGW.
Packit bd1cd8
Packit bd1cd8
We also have reports on successful cross compilation of Google Test MinGW binaries on Linux using [these instructions](http://wiki.wxwidgets.org/Cross-Compiling_Under_Linux#Cross-compiling_under_Linux_for_MS_Windows) on the WxWidgets site.
Packit bd1cd8
Packit bd1cd8
Please contact `googletestframework@googlegroups.com` if you are
Packit bd1cd8
interested in improving the support for MinGW.
Packit bd1cd8
Packit bd1cd8
## 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)? ##
Packit bd1cd8
Packit bd1cd8
Due to some peculiarity of C++, it requires some non-trivial template
Packit bd1cd8
meta programming tricks to support using `NULL` as an argument of the
Packit bd1cd8
`EXPECT_XX()` and `ASSERT_XX()` macros. Therefore we only do it where
Packit bd1cd8
it's most needed (otherwise we make the implementation of Google Test
Packit bd1cd8
harder to maintain and more error-prone than necessary).
Packit bd1cd8
Packit bd1cd8
The `EXPECT_EQ()` macro takes the _expected_ value as its first
Packit bd1cd8
argument and the _actual_ value as the second. It's reasonable that
Packit bd1cd8
someone wants to write `EXPECT_EQ(NULL, some_expression)`, and this
Packit bd1cd8
indeed was requested several times. Therefore we implemented it.
Packit bd1cd8
Packit bd1cd8
The need for `EXPECT_NE(NULL, ptr)` isn't nearly as strong. When the
Packit bd1cd8
assertion fails, you already know that `ptr` must be `NULL`, so it
Packit bd1cd8
doesn't add any information to print ptr in this case. That means
Packit bd1cd8
`EXPECT_TRUE(ptr ! NULL)` works just as well.
Packit bd1cd8
Packit bd1cd8
If we were to support `EXPECT_NE(NULL, ptr)`, for consistency we'll
Packit bd1cd8
have to support `EXPECT_NE(ptr, NULL)` as well, as unlike `EXPECT_EQ`,
Packit bd1cd8
we don't have a convention on the order of the two arguments for
Packit bd1cd8
`EXPECT_NE`. This means using the template meta programming tricks
Packit bd1cd8
twice in the implementation, making it even harder to understand and
Packit bd1cd8
maintain. We believe the benefit doesn't justify the cost.
Packit bd1cd8
Packit bd1cd8
Finally, with the growth of Google Mock's [matcher](../../CookBook.md#using-matchers-in-google-test-assertions) library, we are
Packit bd1cd8
encouraging people to use the unified `EXPECT_THAT(value, matcher)`
Packit bd1cd8
syntax more often in tests. One significant advantage of the matcher
Packit bd1cd8
approach is that matchers can be easily combined to form new matchers,
Packit bd1cd8
while the `EXPECT_NE`, etc, macros cannot be easily
Packit bd1cd8
combined. Therefore we want to invest more in the matchers than in the
Packit bd1cd8
`EXPECT_XX()` macros.
Packit bd1cd8
Packit bd1cd8
## Does Google Test support running tests in parallel? ##
Packit bd1cd8
Packit bd1cd8
Test runners tend to be tightly coupled with the build/test
Packit bd1cd8
environment, and Google Test doesn't try to solve the problem of
Packit bd1cd8
running tests in parallel.  Instead, we tried to make Google Test work
Packit bd1cd8
nicely with test runners.  For example, Google Test's XML report
Packit bd1cd8
contains the time spent on each test, and its `gtest_list_tests` and
Packit bd1cd8
`gtest_filter` flags can be used for splitting the execution of test
Packit bd1cd8
methods into multiple processes.  These functionalities can help the
Packit bd1cd8
test runner run the tests in parallel.
Packit bd1cd8
Packit bd1cd8
## Why don't Google Test run the tests in different threads to speed things up? ##
Packit bd1cd8
Packit bd1cd8
It's difficult to write thread-safe code.  Most tests are not written
Packit bd1cd8
with thread-safety in mind, and thus may not work correctly in a
Packit bd1cd8
multi-threaded setting.
Packit bd1cd8
Packit bd1cd8
If you think about it, it's already hard to make your code work when
Packit bd1cd8
you know what other threads are doing.  It's much harder, and
Packit bd1cd8
sometimes even impossible, to make your code work when you don't know
Packit bd1cd8
what other threads are doing (remember that test methods can be added,
Packit bd1cd8
deleted, or modified after your test was written).  If you want to run
Packit bd1cd8
the tests in parallel, you'd better run them in different processes.
Packit bd1cd8
Packit bd1cd8
## Why aren't Google Test assertions implemented using exceptions? ##
Packit bd1cd8
Packit bd1cd8
Our original motivation was to be able to use Google Test in projects
Packit bd1cd8
that disable exceptions.  Later we realized some additional benefits
Packit bd1cd8
of this approach:
Packit bd1cd8
Packit bd1cd8
  1. Throwing in a destructor is undefined behavior in C++.  Not using exceptions means Google Test's assertions are safe to use in destructors.
Packit bd1cd8
  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.
Packit bd1cd8
  1. If assertions are implemented using exceptions, a test may falsely ignore a failure if it's caught by user code:
Packit bd1cd8
```
Packit bd1cd8
try { ... ASSERT_TRUE(...) ... }
Packit bd1cd8
catch (...) { ... }
Packit bd1cd8
```
Packit bd1cd8
The 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.
Packit bd1cd8
Packit bd1cd8
The downside of not using exceptions is that `ASSERT_*` (implemented
Packit bd1cd8
using `return`) will only abort the current function, not the current
Packit bd1cd8
`TEST`.
Packit bd1cd8
Packit bd1cd8
## Why do we use two different macros for tests with and without fixtures? ##
Packit bd1cd8
Packit bd1cd8
Unfortunately, C++'s macro system doesn't allow us to use the same
Packit bd1cd8
macro for both cases.  One possibility is to provide only one macro
Packit bd1cd8
for tests with fixtures, and require the user to define an empty
Packit bd1cd8
fixture sometimes:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
class FooTest : public ::testing::Test {};
Packit bd1cd8
Packit bd1cd8
TEST_F(FooTest, DoesThis) { ... }
Packit bd1cd8
```
Packit bd1cd8
or
Packit bd1cd8
```
Packit bd1cd8
typedef ::testing::Test FooTest;
Packit bd1cd8
Packit bd1cd8
TEST_F(FooTest, DoesThat) { ... }
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
Yet, many people think this is one line too many. :-) Our goal was to
Packit bd1cd8
make it really easy to write tests, so we tried to make simple tests
Packit bd1cd8
trivial to create.  That means using a separate macro for such tests.
Packit bd1cd8
Packit bd1cd8
We think neither approach is ideal, yet either of them is reasonable.
Packit bd1cd8
In the end, it probably doesn't matter much either way.
Packit bd1cd8
Packit bd1cd8
## Why don't we use structs as test fixtures? ##
Packit bd1cd8
Packit bd1cd8
We like to use structs only when representing passive data.  This
Packit bd1cd8
distinction between structs and classes is good for documenting the
Packit bd1cd8
intent of the code's author.  Since test fixtures have logic like
Packit bd1cd8
`SetUp()` and `TearDown()`, they are better defined as classes.
Packit bd1cd8
Packit bd1cd8
## Why are death tests implemented as assertions instead of using a test runner? ##
Packit bd1cd8
Packit bd1cd8
Our goal was to make death tests as convenient for a user as C++
Packit bd1cd8
possibly allows.  In particular:
Packit bd1cd8
Packit bd1cd8
  * 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.
Packit bd1cd8
  * `ASSERT_DEATH` has a similar syntax and error-reporting semantics as other Google Test assertions, and thus is easy to learn.
Packit bd1cd8
  * `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:
Packit bd1cd8
```
Packit bd1cd8
    if (FooCondition()) {
Packit bd1cd8
      ASSERT_DEATH(Bar(), "blah");
Packit bd1cd8
    } else {
Packit bd1cd8
      ASSERT_EQ(5, Bar());
Packit bd1cd8
    }
Packit bd1cd8
```
Packit bd1cd8
If 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.
Packit bd1cd8
  * `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,
Packit bd1cd8
```
Packit bd1cd8
    const int count = GetCount();  // Only known at run time.
Packit bd1cd8
    for (int i = 1; i <= count; i++) {
Packit bd1cd8
      ASSERT_DEATH({
Packit bd1cd8
        double* buffer = new double[i];
Packit bd1cd8
        ... initializes buffer ...
Packit bd1cd8
        Foo(buffer, i)
Packit bd1cd8
      }, "blah blah");
Packit bd1cd8
    }
Packit bd1cd8
```
Packit bd1cd8
The runner-based approach tends to be more static and less flexible, or requires more user effort to get this kind of flexibility.
Packit bd1cd8
Packit bd1cd8
Another interesting thing about `ASSERT_DEATH` is that it calls `fork()`
Packit bd1cd8
to create a child process to run the death test.  This is lightening
Packit bd1cd8
fast, as `fork()` uses copy-on-write pages and incurs almost zero
Packit bd1cd8
overhead, and the child process starts from the user-supplied
Packit bd1cd8
statement directly, skipping all global and local initialization and
Packit bd1cd8
any code leading to the given statement.  If you launch the child
Packit bd1cd8
process from scratch, it can take seconds just to load everything and
Packit bd1cd8
start running if the test links to many libraries dynamically.
Packit bd1cd8
Packit bd1cd8
## My death test modifies some state, but the change seems lost after the death test finishes. Why? ##
Packit bd1cd8
Packit bd1cd8
Death tests (`EXPECT_DEATH`, etc) are executed in a sub-process s.t. the
Packit bd1cd8
expected crash won't kill the test program (i.e. the parent process). As a
Packit bd1cd8
result, any in-memory side effects they incur are observable in their
Packit bd1cd8
respective sub-processes, but not in the parent process. You can think of them
Packit bd1cd8
as running in a parallel universe, more or less.
Packit bd1cd8
Packit bd1cd8
## The compiler complains about "undefined references" to some static const member variables, but I did define them in the class body. What's wrong? ##
Packit bd1cd8
Packit bd1cd8
If your class has a static data member:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
// foo.h
Packit bd1cd8
class Foo {
Packit bd1cd8
  ...
Packit bd1cd8
  static const int kBar = 100;
Packit bd1cd8
};
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
You also need to define it _outside_ of the class body in `foo.cc`:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
const int Foo::kBar;  // No initializer here.
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
Otherwise your code is **invalid C++**, and may break in unexpected ways. In
Packit bd1cd8
particular, using it in Google Test comparison assertions (`EXPECT_EQ`, etc)
Packit bd1cd8
will generate an "undefined reference" linker error.
Packit bd1cd8
Packit bd1cd8
## I have an interface that has several implementations. Can I write a set of tests once and repeat them over all the implementations? ##
Packit bd1cd8
Packit bd1cd8
Google Test doesn't yet have good support for this kind of tests, or
Packit bd1cd8
data-driven tests in general. We hope to be able to make improvements in this
Packit bd1cd8
area soon.
Packit bd1cd8
Packit bd1cd8
## Can I derive a test fixture from another? ##
Packit bd1cd8
Packit bd1cd8
Yes.
Packit bd1cd8
Packit bd1cd8
Each test fixture has a corresponding and same named test case. This means only
Packit bd1cd8
one test case can use a particular fixture. Sometimes, however, multiple test
Packit bd1cd8
cases may want to use the same or slightly different fixtures. For example, you
Packit bd1cd8
may want to make sure that all of a GUI library's test cases don't leak
Packit bd1cd8
important system resources like fonts and brushes.
Packit bd1cd8
Packit bd1cd8
In Google Test, you share a fixture among test cases by putting the shared
Packit bd1cd8
logic in a base test fixture, then deriving from that base a separate fixture
Packit bd1cd8
for each test case that wants to use this common logic. You then use `TEST_F()`
Packit bd1cd8
to write tests using each derived fixture.
Packit bd1cd8
Packit bd1cd8
Typically, your code looks like this:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
// Defines a base test fixture.
Packit bd1cd8
class BaseTest : public ::testing::Test {
Packit bd1cd8
  protected:
Packit bd1cd8
   ...
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Derives a fixture FooTest from BaseTest.
Packit bd1cd8
class FooTest : public BaseTest {
Packit bd1cd8
  protected:
Packit bd1cd8
    virtual void SetUp() {
Packit bd1cd8
      BaseTest::SetUp();  // Sets up the base fixture first.
Packit bd1cd8
      ... additional set-up work ...
Packit bd1cd8
    }
Packit bd1cd8
    virtual void TearDown() {
Packit bd1cd8
      ... clean-up work for FooTest ...
Packit bd1cd8
      BaseTest::TearDown();  // Remember to tear down the base fixture
Packit bd1cd8
                             // after cleaning up FooTest!
Packit bd1cd8
    }
Packit bd1cd8
    ... functions and variables for FooTest ...
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Tests that use the fixture FooTest.
Packit bd1cd8
TEST_F(FooTest, Bar) { ... }
Packit bd1cd8
TEST_F(FooTest, Baz) { ... }
Packit bd1cd8
Packit bd1cd8
... additional fixtures derived from BaseTest ...
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
If necessary, you can continue to derive test fixtures from a derived fixture.
Packit bd1cd8
Google Test has no limit on how deep the hierarchy can be.
Packit bd1cd8
Packit bd1cd8
For a complete example using derived test fixtures, see
Packit bd1cd8
`samples/sample5_unittest.cc`.
Packit bd1cd8
Packit bd1cd8
## My compiler complains "void value not ignored as it ought to be." What does this mean? ##
Packit bd1cd8
Packit bd1cd8
You're probably using an `ASSERT_*()` in a function that doesn't return `void`.
Packit bd1cd8
`ASSERT_*()` can only be used in `void` functions.
Packit bd1cd8
Packit bd1cd8
## My death test hangs (or seg-faults). How do I fix it? ##
Packit bd1cd8
Packit bd1cd8
In Google Test, death tests are run in a child process and the way they work is
Packit bd1cd8
delicate. To write death tests you really need to understand how they work.
Packit bd1cd8
Please make sure you have read this.
Packit bd1cd8
Packit bd1cd8
In particular, death tests don't like having multiple threads in the parent
Packit bd1cd8
process. So the first thing you can try is to eliminate creating threads
Packit bd1cd8
outside of `EXPECT_DEATH()`.
Packit bd1cd8
Packit bd1cd8
Sometimes this is impossible as some library you must use may be creating
Packit bd1cd8
threads before `main()` is even reached. In this case, you can try to minimize
Packit bd1cd8
the chance of conflicts by either moving as many activities as possible inside
Packit bd1cd8
`EXPECT_DEATH()` (in the extreme case, you want to move everything inside), or
Packit bd1cd8
leaving as few things as possible in it. Also, you can try to set the death
Packit bd1cd8
test style to `"threadsafe"`, which is safer but slower, and see if it helps.
Packit bd1cd8
Packit bd1cd8
If you go with thread-safe death tests, remember that they rerun the test
Packit bd1cd8
program from the beginning in the child process. Therefore make sure your
Packit bd1cd8
program can run side-by-side with itself and is deterministic.
Packit bd1cd8
Packit bd1cd8
In the end, this boils down to good concurrent programming. You have to make
Packit bd1cd8
sure that there is no race conditions or dead locks in your program. No silver
Packit bd1cd8
bullet - sorry!
Packit bd1cd8
Packit bd1cd8
## Should I use the constructor/destructor of the test fixture or the set-up/tear-down function? ##
Packit bd1cd8
Packit bd1cd8
The first thing to remember is that Google Test does not reuse the
Packit bd1cd8
same test fixture object across multiple tests. For each `TEST_F`,
Packit bd1cd8
Google Test will create a fresh test fixture object, _immediately_
Packit bd1cd8
call `SetUp()`, run the test, call `TearDown()`, and then
Packit bd1cd8
_immediately_ delete the test fixture object. Therefore, there is no
Packit bd1cd8
need to write a `SetUp()` or `TearDown()` function if the constructor
Packit bd1cd8
or destructor already does the job.
Packit bd1cd8
Packit bd1cd8
You may still want to use `SetUp()/TearDown()` in the following cases:
Packit bd1cd8
  * 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.
Packit bd1cd8
  * The Google Test team is considering making the assertion macros throw on platforms where exceptions are enabled (e.g. Windows, Mac OS, and Linux client-side), which will eliminate the need for the user to propagate failures from a subroutine to its caller. Therefore, you shouldn't use Google Test assertions in a destructor if your code could run on such a platform.
Packit bd1cd8
  * 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()`.
Packit bd1cd8
Packit bd1cd8
## The compiler complains "no matching function to call" when I use ASSERT\_PREDn. How do I fix it? ##
Packit bd1cd8
Packit bd1cd8
If the predicate function you use in `ASSERT_PRED*` or `EXPECT_PRED*` is
Packit bd1cd8
overloaded or a template, the compiler will have trouble figuring out which
Packit bd1cd8
overloaded version it should use. `ASSERT_PRED_FORMAT*` and
Packit bd1cd8
`EXPECT_PRED_FORMAT*` don't have this problem.
Packit bd1cd8
Packit bd1cd8
If you see this error, you might want to switch to
Packit bd1cd8
`(ASSERT|EXPECT)_PRED_FORMAT*`, which will also give you a better failure
Packit bd1cd8
message. If, however, that is not an option, you can resolve the problem by
Packit bd1cd8
explicitly telling the compiler which version to pick.
Packit bd1cd8
Packit bd1cd8
For example, suppose you have
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
bool IsPositive(int n) {
Packit bd1cd8
  return n > 0;
Packit bd1cd8
}
Packit bd1cd8
bool IsPositive(double x) {
Packit bd1cd8
  return x > 0;
Packit bd1cd8
}
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
you will get a compiler error if you write
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
EXPECT_PRED1(IsPositive, 5);
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
However, this will work:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
EXPECT_PRED1(*static_cast<bool (*)(int)>*(IsPositive), 5);
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
(The stuff inside the angled brackets for the `static_cast` operator is the
Packit bd1cd8
type of the function pointer for the `int`-version of `IsPositive()`.)
Packit bd1cd8
Packit bd1cd8
As another example, when you have a template function
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
template <typename T>
Packit bd1cd8
bool IsNegative(T x) {
Packit bd1cd8
  return x < 0;
Packit bd1cd8
}
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
you can use it in a predicate assertion like this:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
ASSERT_PRED1(IsNegative*<int>*, -5);
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
Things are more interesting if your template has more than one parameters. The
Packit bd1cd8
following won't compile:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
ASSERT_PRED2(*GreaterThan<int, int>*, 5, 0);
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
as the C++ pre-processor thinks you are giving `ASSERT_PRED2` 4 arguments,
Packit bd1cd8
which is one more than expected. The workaround is to wrap the predicate
Packit bd1cd8
function in parentheses:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
ASSERT_PRED2(*(GreaterThan<int, int>)*, 5, 0);
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
## My compiler complains about "ignoring return value" when I call RUN\_ALL\_TESTS(). Why? ##
Packit bd1cd8
Packit bd1cd8
Some people had been ignoring the return value of `RUN_ALL_TESTS()`. That is,
Packit bd1cd8
instead of
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
return RUN_ALL_TESTS();
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
they write
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
RUN_ALL_TESTS();
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
This is wrong and dangerous. A test runner needs to see the return value of
Packit bd1cd8
`RUN_ALL_TESTS()` in order to determine if a test has passed. If your `main()`
Packit bd1cd8
function ignores it, your test will be considered successful even if it has a
Packit bd1cd8
Google Test assertion failure. Very bad.
Packit bd1cd8
Packit bd1cd8
To help the users avoid this dangerous bug, the implementation of
Packit bd1cd8
`RUN_ALL_TESTS()` causes gcc to raise this warning, when the return value is
Packit bd1cd8
ignored. If you see this warning, the fix is simple: just make sure its value
Packit bd1cd8
is used as the return value of `main()`.
Packit bd1cd8
Packit bd1cd8
## My compiler complains that a constructor (or destructor) cannot return a value. What's going on? ##
Packit bd1cd8
Packit bd1cd8
Due to a peculiarity of C++, in order to support the syntax for streaming
Packit bd1cd8
messages to an `ASSERT_*`, e.g.
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
ASSERT_EQ(1, Foo()) << "blah blah" << foo;
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
we had to give up using `ASSERT*` and `FAIL*` (but not `EXPECT*` and
Packit bd1cd8
`ADD_FAILURE*`) in constructors and destructors. The workaround is to move the
Packit bd1cd8
content of your constructor/destructor to a private void member function, or
Packit bd1cd8
switch to `EXPECT_*()` if that works. This section in the user's guide explains
Packit bd1cd8
it.
Packit bd1cd8
Packit bd1cd8
## My set-up function is not called. Why? ##
Packit bd1cd8
Packit bd1cd8
C++ is case-sensitive. It should be spelled as `SetUp()`.  Did you
Packit bd1cd8
spell it as `Setup()`?
Packit bd1cd8
Packit bd1cd8
Similarly, sometimes people spell `SetUpTestCase()` as `SetupTestCase()` and
Packit bd1cd8
wonder why it's never called.
Packit bd1cd8
Packit bd1cd8
## How do I jump to the line of a failure in Emacs directly? ##
Packit bd1cd8
Packit bd1cd8
Google Test's failure message format is understood by Emacs and many other
Packit bd1cd8
IDEs, like acme and XCode. If a Google Test message is in a compilation buffer
Packit bd1cd8
in Emacs, then it's clickable. You can now hit `enter` on a message to jump to
Packit bd1cd8
the corresponding source code, or use `C-x `` to jump to the next failure.
Packit bd1cd8
Packit bd1cd8
## 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. ##
Packit bd1cd8
Packit bd1cd8
You don't have to. Instead of
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
class FooTest : public BaseTest {};
Packit bd1cd8
Packit bd1cd8
TEST_F(FooTest, Abc) { ... }
Packit bd1cd8
TEST_F(FooTest, Def) { ... }
Packit bd1cd8
Packit bd1cd8
class BarTest : public BaseTest {};
Packit bd1cd8
Packit bd1cd8
TEST_F(BarTest, Abc) { ... }
Packit bd1cd8
TEST_F(BarTest, Def) { ... }
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
you can simply `typedef` the test fixtures:
Packit bd1cd8
```
Packit bd1cd8
typedef BaseTest FooTest;
Packit bd1cd8
Packit bd1cd8
TEST_F(FooTest, Abc) { ... }
Packit bd1cd8
TEST_F(FooTest, Def) { ... }
Packit bd1cd8
Packit bd1cd8
typedef BaseTest BarTest;
Packit bd1cd8
Packit bd1cd8
TEST_F(BarTest, Abc) { ... }
Packit bd1cd8
TEST_F(BarTest, Def) { ... }
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
## The Google Test output is buried in a whole bunch of log messages. What do I do? ##
Packit bd1cd8
Packit bd1cd8
The Google Test output is meant to be a concise and human-friendly report. If
Packit bd1cd8
your test generates textual output itself, it will mix with the Google Test
Packit bd1cd8
output, making it hard to read. However, there is an easy solution to this
Packit bd1cd8
problem.
Packit bd1cd8
Packit bd1cd8
Since most log messages go to stderr, we decided to let Google Test output go
Packit bd1cd8
to stdout. This way, you can easily separate the two using redirection. For
Packit bd1cd8
example:
Packit bd1cd8
```
Packit bd1cd8
./my_test > googletest_output.txt
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
## Why should I prefer test fixtures over global variables? ##
Packit bd1cd8
Packit bd1cd8
There are several good reasons:
Packit bd1cd8
  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.
Packit bd1cd8
  1. Global variables pollute the global namespace.
Packit bd1cd8
  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.
Packit bd1cd8
Packit bd1cd8
## How do I test private class members without writing FRIEND\_TEST()s? ##
Packit bd1cd8
Packit bd1cd8
You should try to write testable code, which means classes should be easily
Packit bd1cd8
tested from their public interface. One way to achieve this is the Pimpl idiom:
Packit bd1cd8
you move all private members of a class into a helper class, and make all
Packit bd1cd8
members of the helper class public.
Packit bd1cd8
Packit bd1cd8
You have several other options that don't require using `FRIEND_TEST`:
Packit bd1cd8
  * Write the tests as members of the fixture class:
Packit bd1cd8
```
Packit bd1cd8
class Foo {
Packit bd1cd8
  friend class FooTest;
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
class FooTest : public ::testing::Test {
Packit bd1cd8
 protected:
Packit bd1cd8
  ...
Packit bd1cd8
  void Test1() {...} // This accesses private members of class Foo.
Packit bd1cd8
  void Test2() {...} // So does this one.
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
TEST_F(FooTest, Test1) {
Packit bd1cd8
  Test1();
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
TEST_F(FooTest, Test2) {
Packit bd1cd8
  Test2();
Packit bd1cd8
}
Packit bd1cd8
```
Packit bd1cd8
  * In the fixture class, write accessors for the tested class' private members, then use the accessors in your tests:
Packit bd1cd8
```
Packit bd1cd8
class Foo {
Packit bd1cd8
  friend class FooTest;
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
class FooTest : public ::testing::Test {
Packit bd1cd8
 protected:
Packit bd1cd8
  ...
Packit bd1cd8
  T1 get_private_member1(Foo* obj) {
Packit bd1cd8
    return obj->private_member1_;
Packit bd1cd8
  }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
TEST_F(FooTest, Test1) {
Packit bd1cd8
  ...
Packit bd1cd8
  get_private_member1(x)
Packit bd1cd8
  ...
Packit bd1cd8
}
Packit bd1cd8
```
Packit bd1cd8
  * If the methods are declared **protected**, you can change their access level in a test-only subclass:
Packit bd1cd8
```
Packit bd1cd8
class YourClass {
Packit bd1cd8
  ...
Packit bd1cd8
 protected: // protected access for testability.
Packit bd1cd8
  int DoSomethingReturningInt();
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// in the your_class_test.cc file:
Packit bd1cd8
class TestableYourClass : public YourClass {
Packit bd1cd8
  ...
Packit bd1cd8
 public: using YourClass::DoSomethingReturningInt; // changes access rights
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
TEST_F(YourClassTest, DoSomethingTest) {
Packit bd1cd8
  TestableYourClass obj;
Packit bd1cd8
  assertEquals(expected_value, obj.DoSomethingReturningInt());
Packit bd1cd8
}
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
## How do I test private class static members without writing FRIEND\_TEST()s? ##
Packit bd1cd8
Packit bd1cd8
We find private static methods clutter the header file.  They are
Packit bd1cd8
implementation details and ideally should be kept out of a .h. So often I make
Packit bd1cd8
them free functions instead.
Packit bd1cd8
Packit bd1cd8
Instead of:
Packit bd1cd8
```
Packit bd1cd8
// foo.h
Packit bd1cd8
class Foo {
Packit bd1cd8
  ...
Packit bd1cd8
 private:
Packit bd1cd8
  static bool Func(int n);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// foo.cc
Packit bd1cd8
bool Foo::Func(int n) { ... }
Packit bd1cd8
Packit bd1cd8
// foo_test.cc
Packit bd1cd8
EXPECT_TRUE(Foo::Func(12345));
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
You probably should better write:
Packit bd1cd8
```
Packit bd1cd8
// foo.h
Packit bd1cd8
class Foo {
Packit bd1cd8
  ...
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// foo.cc
Packit bd1cd8
namespace internal {
Packit bd1cd8
  bool Func(int n) { ... }
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// foo_test.cc
Packit bd1cd8
namespace internal {
Packit bd1cd8
  bool Func(int n);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
EXPECT_TRUE(internal::Func(12345));
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
## I would like to run a test several times with different parameters. Do I need to write several similar copies of it? ##
Packit bd1cd8
Packit bd1cd8
No. You can use a feature called [value-parameterized tests](V1_5_AdvancedGuide.md#Value_Parameterized_Tests) which
Packit bd1cd8
lets you repeat your tests with different parameters, without defining it more than once.
Packit bd1cd8
Packit bd1cd8
## How do I test a file that defines main()? ##
Packit bd1cd8
Packit bd1cd8
To test a `foo.cc` file, you need to compile and link it into your unit test
Packit bd1cd8
program. However, when the file contains a definition for the `main()`
Packit bd1cd8
function, it will clash with the `main()` of your unit test, and will result in
Packit bd1cd8
a build error.
Packit bd1cd8
Packit bd1cd8
The right solution is to split it into three files:
Packit bd1cd8
  1. `foo.h` which contains the declarations,
Packit bd1cd8
  1. `foo.cc` which contains the definitions except `main()`, and
Packit bd1cd8
  1. `foo_main.cc` which contains nothing but the definition of `main()`.
Packit bd1cd8
Packit bd1cd8
Then `foo.cc` can be easily tested.
Packit bd1cd8
Packit bd1cd8
If you are adding tests to an existing file and don't want an intrusive change
Packit bd1cd8
like this, there is a hack: just include the entire `foo.cc` file in your unit
Packit bd1cd8
test. For example:
Packit bd1cd8
```
Packit bd1cd8
// File foo_unittest.cc
Packit bd1cd8
Packit bd1cd8
// The headers section
Packit bd1cd8
...
Packit bd1cd8
Packit bd1cd8
// Renames main() in foo.cc to make room for the unit test main()
Packit bd1cd8
#define main FooMain
Packit bd1cd8
Packit bd1cd8
#include "a/b/foo.cc"
Packit bd1cd8
Packit bd1cd8
// The tests start here.
Packit bd1cd8
...
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
However, please remember this is a hack and should only be used as the last
Packit bd1cd8
resort.
Packit bd1cd8
Packit bd1cd8
## What can the statement argument in ASSERT\_DEATH() be? ##
Packit bd1cd8
Packit bd1cd8
`ASSERT_DEATH(_statement_, _regex_)` (or any death assertion macro) can be used
Packit bd1cd8
wherever `_statement_` is valid. So basically `_statement_` can be any C++
Packit bd1cd8
statement that makes sense in the current context. In particular, it can
Packit bd1cd8
reference global and/or local variables, and can be:
Packit bd1cd8
  * a simple function call (often the case),
Packit bd1cd8
  * a complex expression, or
Packit bd1cd8
  * a compound statement.
Packit bd1cd8
Packit bd1cd8
> Some examples are shown here:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
// A death test can be a simple function call.
Packit bd1cd8
TEST(MyDeathTest, FunctionCall) {
Packit bd1cd8
  ASSERT_DEATH(Xyz(5), "Xyz failed");
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Or a complex expression that references variables and functions.
Packit bd1cd8
TEST(MyDeathTest, ComplexExpression) {
Packit bd1cd8
  const bool c = Condition();
Packit bd1cd8
  ASSERT_DEATH((c ? Func1(0) : object2.Method("test")),
Packit bd1cd8
               "(Func1|Method) failed");
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Death assertions can be used any where in a function. In
Packit bd1cd8
// particular, they can be inside a loop.
Packit bd1cd8
TEST(MyDeathTest, InsideLoop) {
Packit bd1cd8
  // Verifies that Foo(0), Foo(1), ..., and Foo(4) all die.
Packit bd1cd8
  for (int i = 0; i < 5; i++) {
Packit bd1cd8
    EXPECT_DEATH_M(Foo(i), "Foo has \\d+ errors",
Packit bd1cd8
                   ::testing::Message() << "where i is " << i);
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// A death assertion can contain a compound statement.
Packit bd1cd8
TEST(MyDeathTest, CompoundStatement) {
Packit bd1cd8
  // Verifies that at lease one of Bar(0), Bar(1), ..., and
Packit bd1cd8
  // Bar(4) dies.
Packit bd1cd8
  ASSERT_DEATH({
Packit bd1cd8
    for (int i = 0; i < 5; i++) {
Packit bd1cd8
      Bar(i);
Packit bd1cd8
    }
Packit bd1cd8
  },
Packit bd1cd8
  "Bar has \\d+ errors");}
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
`googletest_unittest.cc` contains more examples if you are interested.
Packit bd1cd8
Packit bd1cd8
## What syntax does the regular expression in ASSERT\_DEATH use? ##
Packit bd1cd8
Packit bd1cd8
On POSIX systems, Google Test uses the POSIX Extended regular
Packit bd1cd8
expression syntax
Packit bd1cd8
(http://en.wikipedia.org/wiki/Regular_expression#POSIX_Extended_Regular_Expressions). On
Packit bd1cd8
Windows, it uses a limited variant of regular expression syntax. For
Packit bd1cd8
more details, see the [regular expression syntax](V1_5_AdvancedGuide.md#Regular_Expression_Syntax).
Packit bd1cd8
Packit bd1cd8
## I have a fixture class Foo, but TEST\_F(Foo, Bar) gives me error "no matching function for call to Foo::Foo()". Why? ##
Packit bd1cd8
Packit bd1cd8
Google Test needs to be able to create objects of your test fixture class, so
Packit bd1cd8
it must have a default constructor. Normally the compiler will define one for
Packit bd1cd8
you. However, there are cases where you have to define your own:
Packit bd1cd8
  * 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.
Packit bd1cd8
  * 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`.)
Packit bd1cd8
Packit bd1cd8
## Why does ASSERT\_DEATH complain about previous threads that were already joined? ##
Packit bd1cd8
Packit bd1cd8
With the Linux pthread library, there is no turning back once you cross the
Packit bd1cd8
line from single thread to multiple threads. The first time you create a
Packit bd1cd8
thread, a manager thread is created in addition, so you get 3, not 2, threads.
Packit bd1cd8
Later when the thread you create joins the main thread, the thread count
Packit bd1cd8
decrements by 1, but the manager thread will never be killed, so you still have
Packit bd1cd8
2 threads, which means you cannot safely run a death test.
Packit bd1cd8
Packit bd1cd8
The new NPTL thread library doesn't suffer from this problem, as it doesn't
Packit bd1cd8
create a manager thread. However, if you don't control which machine your test
Packit bd1cd8
runs on, you shouldn't depend on this.
Packit bd1cd8
Packit bd1cd8
## Why does Google Test require the entire test case, instead of individual tests, to be named FOODeathTest when it uses ASSERT\_DEATH? ##
Packit bd1cd8
Packit bd1cd8
Google Test does not interleave tests from different test cases. That is, it
Packit bd1cd8
runs all tests in one test case first, and then runs all tests in the next test
Packit bd1cd8
case, and so on. Google Test does this because it needs to set up a test case
Packit bd1cd8
before the first test in it is run, and tear it down afterwords. Splitting up
Packit bd1cd8
the test case would require multiple set-up and tear-down processes, which is
Packit bd1cd8
inefficient and makes the semantics unclean.
Packit bd1cd8
Packit bd1cd8
If we were to determine the order of tests based on test name instead of test
Packit bd1cd8
case name, then we would have a problem with the following situation:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
TEST_F(FooTest, AbcDeathTest) { ... }
Packit bd1cd8
TEST_F(FooTest, Uvw) { ... }
Packit bd1cd8
Packit bd1cd8
TEST_F(BarTest, DefDeathTest) { ... }
Packit bd1cd8
TEST_F(BarTest, Xyz) { ... }
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
Since `FooTest.AbcDeathTest` needs to run before `BarTest.Xyz`, and we don't
Packit bd1cd8
interleave tests from different test cases, we need to run all tests in the
Packit bd1cd8
`FooTest` case before running any test in the `BarTest` case. This contradicts
Packit bd1cd8
with the requirement to run `BarTest.DefDeathTest` before `FooTest.Uvw`.
Packit bd1cd8
Packit bd1cd8
## 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? ##
Packit bd1cd8
Packit bd1cd8
You don't have to, but if you like, you may split up the test case into
Packit bd1cd8
`FooTest` and `FooDeathTest`, where the names make it clear that they are
Packit bd1cd8
related:
Packit bd1cd8
Packit bd1cd8
```
Packit bd1cd8
class FooTest : public ::testing::Test { ... };
Packit bd1cd8
Packit bd1cd8
TEST_F(FooTest, Abc) { ... }
Packit bd1cd8
TEST_F(FooTest, Def) { ... }
Packit bd1cd8
Packit bd1cd8
typedef FooTest FooDeathTest;
Packit bd1cd8
Packit bd1cd8
TEST_F(FooDeathTest, Uvw) { ... EXPECT_DEATH(...) ... }
Packit bd1cd8
TEST_F(FooDeathTest, Xyz) { ... ASSERT_DEATH(...) ... }
Packit bd1cd8
```
Packit bd1cd8
Packit bd1cd8
## The compiler complains about "no match for 'operator<<'" when I use an assertion. What gives? ##
Packit bd1cd8
Packit bd1cd8
If you use a user-defined type `FooType` in an assertion, you must make sure
Packit bd1cd8
there is an `std::ostream& operator<<(std::ostream&, const FooType&)` function
Packit bd1cd8
defined such that we can print a value of `FooType`.
Packit bd1cd8
Packit bd1cd8
In addition, if `FooType` is declared in a name space, the `<<` operator also
Packit bd1cd8
needs to be defined in the _same_ name space.
Packit bd1cd8
Packit bd1cd8
## How do I suppress the memory leak messages on Windows? ##
Packit bd1cd8
Packit bd1cd8
Since the statically initialized Google Test singleton requires allocations on
Packit bd1cd8
the heap, the Visual C++ memory leak detector will report memory leaks at the
Packit bd1cd8
end of the program run. The easiest way to avoid this is to use the
Packit bd1cd8
`_CrtMemCheckpoint` and `_CrtMemDumpAllObjectsSince` calls to not report any
Packit bd1cd8
statically initialized heap objects. See MSDN for more details and additional
Packit bd1cd8
heap check/debug routines.
Packit bd1cd8
Packit bd1cd8
## 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! ##
Packit bd1cd8
Packit bd1cd8
You may get a number of the following linker error or warnings if you
Packit bd1cd8
attempt to link your test project with the Google Test library when
Packit bd1cd8
your project and the are not built using the same compiler settings.
Packit bd1cd8
Packit bd1cd8
  * LNK2005: symbol already defined in object
Packit bd1cd8
  * LNK4217: locally defined symbol 'symbol' imported in function 'function'
Packit bd1cd8
  * LNK4049: locally defined symbol 'symbol' imported
Packit bd1cd8
Packit bd1cd8
The Google Test project (gtest.vcproj) has the Runtime Library option
Packit bd1cd8
set to /MT (use multi-threaded static libraries, /MTd for debug). If
Packit bd1cd8
your project uses something else, for example /MD (use multi-threaded
Packit bd1cd8
DLLs, /MDd for debug), you need to change the setting in the Google
Packit bd1cd8
Test project to match your project's.
Packit bd1cd8
Packit bd1cd8
To update this setting open the project properties in the Visual
Packit bd1cd8
Studio IDE then select the branch Configuration Properties | C/C++ |
Packit bd1cd8
Code Generation and change the option "Runtime Library".  You may also try
Packit bd1cd8
using gtest-md.vcproj instead of gtest.vcproj.
Packit bd1cd8
Packit bd1cd8
## I put my tests in a library and Google Test doesn't run them. What's happening? ##
Packit bd1cd8
Have you read a
Packit bd1cd8
[warning](V1_5_Primer.md#important-note-for-visual-c-users) on
Packit bd1cd8
the Google Test Primer page?
Packit bd1cd8
Packit bd1cd8
## I want to use Google Test with Visual Studio but don't know where to start. ##
Packit bd1cd8
Many people are in your position and one of the posted his solution to
Packit bd1cd8
our mailing list. Here is his link:
Packit bd1cd8
http://hassanjamilahmad.blogspot.com/2009/07/gtest-starters-help.html.
Packit bd1cd8
Packit bd1cd8
## My question is not covered in your FAQ! ##
Packit bd1cd8
Packit bd1cd8
If you cannot find the answer to your question in this FAQ, there are
Packit bd1cd8
some other resources you can use:
Packit bd1cd8
Packit bd1cd8
  1. read other [wiki pages](http://code.google.com/p/googletest/w/list),
Packit bd1cd8
  1. search the mailing list [archive](http://groups.google.com/group/googletestframework/topics),
Packit bd1cd8
  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.).
Packit bd1cd8
Packit bd1cd8
Please note that creating an issue in the
Packit bd1cd8
[issue tracker](http://code.google.com/p/googletest/issues/list) is _not_
Packit bd1cd8
a good way to get your answer, as it is monitored infrequently by a
Packit bd1cd8
very small number of people.
Packit bd1cd8
Packit bd1cd8
When asking a question, it's helpful to provide as much of the
Packit bd1cd8
following information as possible (people cannot help you if there's
Packit bd1cd8
not enough information in your question):
Packit bd1cd8
Packit bd1cd8
  * the version (or the revision number if you check out from SVN 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),
Packit bd1cd8
  * your operating system,
Packit bd1cd8
  * the name and version of your compiler,
Packit bd1cd8
  * the complete command line flags you give to your compiler,
Packit bd1cd8
  * the complete compiler error messages (if the question is about compilation),
Packit bd1cd8
  * the _actual_ code (ideally, a minimal but complete program) that has the problem you encounter.