@@ -76,7 +76,7 @@ for simplicity we just say that it cannot start with `_`.).
...
@@ -76,7 +76,7 @@ for simplicity we just say that it cannot start with `_`.).
It may seem fine for `TestCaseName` and `TestName` to contain `_` in the
It may seem fine for `TestCaseName` and `TestName` to contain `_` in the
middle. However, consider this:
middle. However, consider this:
``` cpp
```c++
TEST(Time,Flies_Like_An_Arrow){...}
TEST(Time,Flies_Like_An_Arrow){...}
TEST(Time_Flies,Like_An_Arrow){...}
TEST(Time_Flies,Like_An_Arrow){...}
```
```
...
@@ -242,7 +242,7 @@ of this approach:
...
@@ -242,7 +242,7 @@ of this approach:
1. Throwing in a destructor is undefined behavior in C++. Not using exceptions means Google Test's assertions are safe to use in destructors.
1. Throwing in a destructor is undefined behavior in C++. Not using exceptions means Google Test's assertions are safe to use in destructors.
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.
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.
1. If assertions are implemented using exceptions, a test may falsely ignore a failure if it's caught by user code:
1. If assertions are implemented using exceptions, a test may falsely ignore a failure if it's caught by user code:
``` cpp
```c++
try{...ASSERT_TRUE(...)...}
try{...ASSERT_TRUE(...)...}
catch(...){...}
catch(...){...}
```
```
...
@@ -259,13 +259,13 @@ macro for both cases. One possibility is to provide only one macro
...
@@ -259,13 +259,13 @@ macro for both cases. One possibility is to provide only one macro
for tests with fixtures, and require the user to define an empty
for tests with fixtures, and require the user to define an empty
fixture sometimes:
fixture sometimes:
``` cpp
```c++
classFooTest:public::testing::Test{};
classFooTest:public::testing::Test{};
TEST_F(FooTest,DoesThis){...}
TEST_F(FooTest,DoesThis){...}
```
```
or
or
``` cpp
```c++
typedef::testing::TestFooTest;
typedef::testing::TestFooTest;
TEST_F(FooTest,DoesThat){...}
TEST_F(FooTest,DoesThat){...}
...
@@ -293,7 +293,7 @@ possibly allows. In particular:
...
@@ -293,7 +293,7 @@ possibly allows. In particular:
* 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.
* 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.
*`ASSERT_DEATH` has a similar syntax and error-reporting semantics as other Google Test assertions, and thus is easy to learn.
*`ASSERT_DEATH` has a similar syntax and error-reporting semantics as other Google Test assertions, and thus is easy to learn.
*`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:
*`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:
``` cpp
```c++
if(FooCondition()){
if(FooCondition()){
ASSERT_DEATH(Bar(),"blah");
ASSERT_DEATH(Bar(),"blah");
}else{
}else{
...
@@ -302,7 +302,7 @@ possibly allows. In particular:
...
@@ -302,7 +302,7 @@ possibly allows. In particular:
```
```
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.
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.
*`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,
*`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,
``` cpp
```c++
constintcount=GetCount();// Only known at run time.
constintcount=GetCount();// Only known at run time.
for(inti=1;i<=count;i++){
for(inti=1;i<=count;i++){
ASSERT_DEATH({
ASSERT_DEATH({
...
@@ -335,7 +335,7 @@ as running in a parallel universe, more or less.
...
@@ -335,7 +335,7 @@ as running in a parallel universe, more or less.
If your class has a static data member:
If your class has a static data member:
``` cpp
```c++
// foo.h
// foo.h
classFoo{
classFoo{
...
...
...
@@ -345,7 +345,7 @@ class Foo {
...
@@ -345,7 +345,7 @@ class Foo {
You also need to define it _outside_ of the class body in `foo.cc`:
You also need to define it _outside_ of the class body in `foo.cc`:
``` cpp
```c++
constintFoo::kBar;// No initializer here.
constintFoo::kBar;// No initializer here.
```
```
...
@@ -376,7 +376,7 @@ to write tests using each derived fixture.
...
@@ -376,7 +376,7 @@ to write tests using each derived fixture.
Typically, your code looks like this:
Typically, your code looks like this:
``` cpp
```c++
// Defines a base test fixture.
// Defines a base test fixture.
classBaseTest:public::testing::Test{
classBaseTest:public::testing::Test{
protected:
protected:
...
@@ -476,7 +476,7 @@ explicitly telling the compiler which version to pick.
...
@@ -476,7 +476,7 @@ explicitly telling the compiler which version to pick.
@@ -774,7 +774,7 @@ Then `foo.cc` can be easily tested.
...
@@ -774,7 +774,7 @@ Then `foo.cc` can be easily tested.
If you are adding tests to an existing file and don't want an intrusive change
If you are adding tests to an existing file and don't want an intrusive change
like this, there is a hack: just include the entire `foo.cc` file in your unit
like this, there is a hack: just include the entire `foo.cc` file in your unit
test. For example:
test. For example:
``` cpp
```c++
// File foo_unittest.cc
// File foo_unittest.cc
// The headers section
// The headers section
...
@@ -805,7 +805,7 @@ reference global and/or local variables, and can be:
...
@@ -805,7 +805,7 @@ reference global and/or local variables, and can be:
Some examples are shown here:
Some examples are shown here:
``` cpp
```c++
// A death test can be a simple function call.
// A death test can be a simple function call.
TEST(MyDeathTest, FunctionCall) {
TEST(MyDeathTest, FunctionCall) {
ASSERT_DEATH(Xyz(5), "Xyz failed");
ASSERT_DEATH(Xyz(5), "Xyz failed");
...
@@ -884,7 +884,7 @@ inefficient and makes the semantics unclean.
...
@@ -884,7 +884,7 @@ 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:
``` cpp
```c++
TEST_F(FooTest, AbcDeathTest) { ... }
TEST_F(FooTest, AbcDeathTest) { ... }
TEST_F(FooTest, Uvw) { ... }
TEST_F(FooTest, Uvw) { ... }
...
@@ -903,7 +903,7 @@ You don't have to, but if you like, you may split up the test case into
...
@@ -903,7 +903,7 @@ You don't have to, but if you like, you may split up the test case into
`FooTest` and `FooDeathTest`, where the names make it clear that they are
`FooTest` and `FooDeathTest`, where the names make it clear that they are
related:
related:
``` cpp
```c++
class FooTest : public ::testing::Test { ... };
class FooTest : public ::testing::Test { ... };
TEST_F(FooTest, Abc) { ... }
TEST_F(FooTest, Abc) { ... }
...
@@ -1005,11 +1005,11 @@ Specifically, if both Google Test and some other code define macro
...
@@ -1005,11 +1005,11 @@ Specifically, if both Google Test and some other code define macro
```
```
to the compiler flags to tell Google Test to change the macro's name
to the compiler flags to tell Google Test to change the macro's name
from `FOO` to `GTEST_FOO`. For example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll need to write
from `FOO` to `GTEST_FOO`. For example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll need to write
``` cpp
```c++
GTEST_TEST(SomeTest, DoesThis) { ... }
GTEST_TEST(SomeTest, DoesThis) { ... }
```
```
instead of
instead of
``` cpp
```c++
TEST(SomeTest, DoesThis) { ... }
TEST(SomeTest, DoesThis) { ... }
```
```
in order to define a test.
in order to define a test.
...
@@ -1023,7 +1023,7 @@ Yes.
...
@@ -1023,7 +1023,7 @@ Yes.
The 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`).
The 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`).
However, 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.
However, 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.
``` cpp
```c++
namespace foo {
namespace foo {
class CoolTest : public ::testing::Test {}; // Fixture foo::CoolTest
class CoolTest : public ::testing::Test {}; // Fixture foo::CoolTest