## My compiler complains "void value not ignored as it ought to be." What does this mean?
## My compiler complains "void value not ignored as it ought to be." What does this mean?
...
@@ -289,8 +280,8 @@ Please make sure you have read [this](advanced.md#how-it-works).
...
@@ -289,8 +280,8 @@ Please make sure you have read [this](advanced.md#how-it-works).
In particular, death tests don't like having multiple threads in the parent
In particular, death tests don't like having multiple threads in the parent
process. So the first thing you can try is to eliminate creating threads outside
process. So the first thing you can try is to eliminate creating threads outside
of `EXPECT_DEATH()`. For example, you may want to use [mocks](../../googlemock)
of `EXPECT_DEATH()`. For example, you may want to use mocks or fake objects
or fake objects instead of real ones in your tests.
instead of real ones in your tests.
Sometimes this is impossible as some library you must use may be creating
Sometimes this is impossible as some library you must use may be creating
threads before `main()` is even reached. In this case, you can try to minimize
threads before `main()` is even reached. In this case, you can try to minimize
...
@@ -328,12 +319,21 @@ The former is usually preferred, as it has the following benefits:
...
@@ -328,12 +319,21 @@ The former is usually preferred, as it has the following benefits:
forgetting to call the base class' `SetUp()/TearDown()` or call them at the
forgetting to call the base class' `SetUp()/TearDown()` or call them at the
wrong time.
wrong time.
You may still want to use `SetUp()/TearDown()` in the following rare cases:
You may still want to use `SetUp()/TearDown()` in the following cases:
* C++ does not allow virtual function calls in constructors and destructors.
You can call a method declared as virtual, but it will not use dynamic
dispatch, it will use the definition from the class the constructor of which
is currently executing. This is because calling a virtual method before the
derived class constructor has a chance to run is very dangerous - the
virtual method might operate on uninitialized data. Therefore, if you need
to call a method that will be overridden in a derived class, you have to use
`SetUp()/TearDown()`.
* In the body of a constructor (or destructor), it's not possible to use the
* In the body of a constructor (or destructor), it's not possible to use the
`ASSERT_xx` macros. Therefore, if the set-up operation could cause a fatal
`ASSERT_xx` macros. Therefore, if the set-up operation could cause a fatal
test failure that should prevent the test from running, it's necessary to
test failure that should prevent the test from running, it's necessary to
use a `CHECK` macro or to use `SetUp()` instead of a constructor.
use `abort`<!-- GOOGLETEST_CM0014 DO NOT DELETE --> and abort the whole test executable,
or to use `SetUp()` instead of a constructor.
* If the tear-down operation could throw an exception, you must use
* If the tear-down operation could throw an exception, you must use
`TearDown()` as opposed to the destructor, as throwing in a destructor leads
`TearDown()` as opposed to the destructor, as throwing in a destructor leads
to undefined behavior and usually will kill your program right away. Note
to undefined behavior and usually will kill your program right away. Note
...
@@ -346,11 +346,6 @@ You may still want to use `SetUp()/TearDown()` in the following rare cases:
...
@@ -346,11 +346,6 @@ You may still want to use `SetUp()/TearDown()` in the following rare cases:
failures from a subroutine to its caller. Therefore, you shouldn't use
failures from a subroutine to its caller. Therefore, you shouldn't use
googletest assertions in a destructor if your code could run on such a
googletest assertions in a destructor if your code could run on such a
platform.
platform.
* 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
overridden in a derived class, you have to use `SetUp()/TearDown()`.
## The compiler complains "no matching function to call" when I use ASSERT_PRED*. How do I fix it?
## The compiler complains "no matching function to call" when I use ASSERT_PRED*. How do I fix it?
...
@@ -421,7 +416,6 @@ parentheses:
...
@@ -421,7 +416,6 @@ parentheses:
ASSERT_PRED2((GreaterThan<int,int>),5,0);
ASSERT_PRED2((GreaterThan<int,int>),5,0);
```
```
## My compiler complains about "ignoring return value" when I call RUN_ALL_TESTS(). Why?
## My compiler complains about "ignoring return value" when I call RUN_ALL_TESTS(). Why?
Some people had been ignoring the return value of `RUN_ALL_TESTS()`. That is,
Some people had been ignoring the return value of `RUN_ALL_TESTS()`. That is,
...
@@ -475,14 +469,8 @@ C++ is case-sensitive. Did you spell it as `Setup()`?
...
@@ -475,14 +469,8 @@ C++ is case-sensitive. Did you spell it as `Setup()`?
Similarly, sometimes people spell `SetUpTestSuite()` as `SetupTestSuite()` and
Similarly, sometimes people spell `SetUpTestSuite()` as `SetupTestSuite()` and
wonder why it's never called.
wonder why it's never called.
## How do I jump to the line of a failure in Emacs directly?
googletest's failure message format is understood by Emacs and many other IDEs,
like acme and XCode. If a googletest message is in a compilation buffer in
Emacs, then it's clickable.
## I have several test suites which share the same test fixture logic, do I have to define a new test fixture class for each of them? This seems pretty tedious.
## 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.
You don't have to. Instead of
You don't have to. Instead of
...
@@ -527,7 +515,6 @@ example:
...
@@ -527,7 +515,6 @@ example:
$ ./my_test > gtest_output.txt
$ ./my_test > gtest_output.txt
```
```
## Why should I prefer test fixtures over global variables?
## Why should I prefer test fixtures over global variables?
There are several good reasons:
There are several good reasons:
...
@@ -539,11 +526,10 @@ There are several good reasons:
...
@@ -539,11 +526,10 @@ There are several good reasons:
names). Thus, tests are kept independent of each other.
names). Thus, tests are kept independent of each other.
1. Global variables pollute the global namespace.
1. Global variables pollute the global namespace.
1. Test fixtures can be reused via subclassing, which cannot be done easily
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
with global variables. This is useful if many test suites have something in
common.
common.
## What can the statement argument in ASSERT_DEATH() be?
## What can the statement argument in ASSERT_DEATH() be?
`ASSERT_DEATH(*statement*, *regex*)` (or any death assertion macro) can be used
`ASSERT_DEATH(*statement*, *regex*)` (or any death assertion macro) can be used
wherever `*statement*` is valid. So basically `*statement*` can be any C++
wherever `*statement*` is valid. So basically `*statement*` can be any C++
...
@@ -621,14 +607,14 @@ The new NPTL thread library doesn't suffer from this problem, as it doesn't
...
@@ -621,14 +607,14 @@ The new NPTL thread library doesn't suffer from this problem, as it doesn't
create a manager thread. However, if you don't control which machine your test
create a manager thread. However, if you don't control which machine your test
runs on, you shouldn't depend on this.
runs on, you shouldn't depend on this.
## Why does googletest require the entire test case, instead of individual tests, to be named *DeathTest when it uses ASSERT_DEATH?
## Why does googletest require the entire test suite, instead of individual tests, to be named *DeathTest when it uses ASSERT_DEATH?
googletest does not interleave tests from different test cases. That is, it runs
googletest does not interleave tests from different test suites. That is, it
all tests in one test case first, and then runs all tests in the next test case,
runs all tests in one test suite first, and then runs all tests in the next test
and so on. googletest does this because it needs to set up a test case before
suite, and so on. googletest does this because it needs to set up a test suite
the first test in it is run, and tear it down afterwords. Splitting up the test
before the first test in it is run, and tear it down afterwords. Splitting up
case would require multiple set-up and tear-down processes, which is inefficient
the test case would require multiple set-up and tear-down processes, which is
and makes the semantics unclean.
inefficient and makes the semantics unclean.
If we were to determine the order of tests based on test name instead of test
If we were to determine the order of tests based on test name instead of test
case name, then we would have a problem with the following situation:
case name, then we would have a problem with the following situation: