primer.md (googletest-release-1.11.0) | : | primer.md (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 163 | skipping to change at line 163 | |||
entire test fails. Otherwise, it succeeds. | entire test fails. Otherwise, it succeeds. | |||
```c++ | ```c++ | |||
TEST(TestSuiteName, TestName) { | TEST(TestSuiteName, TestName) { | |||
... test body ... | ... test body ... | |||
} | } | |||
``` | ``` | |||
`TEST()` arguments go from general to specific. The *first* argument is the name | `TEST()` arguments go from general to specific. The *first* argument is the name | |||
of the test suite, and the *second* argument is the test's name within the test | of the test suite, and the *second* argument is the test's name within the test | |||
suite. Both names must be valid C++ identifiers, and they should not contain | suite. Both names must be valid C++ identifiers, and they should not contain any | |||
any underscores (`_`). A test's *full name* consists of its containing test suit | underscores (`_`). A test's *full name* consists of its containing test suite | |||
e and | and its individual name. Tests from different test suites can have the same | |||
its individual name. Tests from different test suites can have the same | ||||
individual name. | individual name. | |||
For example, let's take a simple integer function: | For example, let's take a simple integer function: | |||
```c++ | ```c++ | |||
int Factorial(int n); // Returns the factorial of n | int Factorial(int n); // Returns the factorial of n | |||
``` | ``` | |||
A test suite for this function might look like: | A test suite for this function might look like: | |||
skipping to change at line 246 | skipping to change at line 246 | |||
Unfortunately, the C++ macro system does not allow us to create a single macro | Unfortunately, the C++ macro system does not allow us to create a single macro | |||
that can handle both types of tests. Using the wrong macro causes a compiler | that can handle both types of tests. Using the wrong macro causes a compiler | |||
error. | error. | |||
Also, you must first define a test fixture class before using it in a | Also, you must first define a test fixture class before using it in a | |||
`TEST_F()`, or you'll get the compiler error "`virtual outside class | `TEST_F()`, or you'll get the compiler error "`virtual outside class | |||
declaration`". | declaration`". | |||
For each test defined with `TEST_F()`, googletest will create a *fresh* test | For each test defined with `TEST_F()`, googletest will create a *fresh* test | |||
fixture at runtime, immediately initialize it via `SetUp()`, run the test, | fixture at runtime, immediately initialize it via `SetUp()`, run the test, clean | |||
clean up by calling `TearDown()`, and then delete the test fixture. Note that | up by calling `TearDown()`, and then delete the test fixture. Note that | |||
different tests in the same test suite have different test fixture objects, and | different tests in the same test suite have different test fixture objects, and | |||
googletest always deletes a test fixture before it creates the next one. | googletest always deletes a test fixture before it creates the next one. | |||
googletest does **not** reuse the same test fixture for multiple tests. Any | googletest does **not** reuse the same test fixture for multiple tests. Any | |||
changes one test makes to the fixture do not affect other tests. | changes one test makes to the fixture do not affect other tests. | |||
As an example, let's write tests for a FIFO queue class named `Queue`, which has | As an example, let's write tests for a FIFO queue class named `Queue`, which has | |||
the following interface: | the following interface: | |||
```c++ | ```c++ | |||
template <typename E> // E is the element type. | template <typename E> // E is the element type. | |||
skipping to change at line 343 | skipping to change at line 343 | |||
**Availability**: Linux, Windows, Mac. | **Availability**: Linux, Windows, Mac. | |||
## Invoking the Tests | ## Invoking the Tests | |||
`TEST()` and `TEST_F()` implicitly register their tests with googletest. So, | `TEST()` and `TEST_F()` implicitly register their tests with googletest. So, | |||
unlike with many other C++ testing frameworks, you don't have to re-list all | unlike with many other C++ testing frameworks, you don't have to re-list all | |||
your defined tests in order to run them. | your defined tests in order to run them. | |||
After defining your tests, you can run them with `RUN_ALL_TESTS()`, which | After defining your tests, you can run them with `RUN_ALL_TESTS()`, which | |||
returns `0` if all the tests are successful, or `1` otherwise. Note that | returns `0` if all the tests are successful, or `1` otherwise. Note that | |||
`RUN_ALL_TESTS()` runs *all tests* in your link unit--they can be from | `RUN_ALL_TESTS()` runs *all tests* in your link unit--they can be from different | |||
different test suites, or even different source files. | test suites, or even different source files. | |||
When invoked, the `RUN_ALL_TESTS()` macro: | When invoked, the `RUN_ALL_TESTS()` macro: | |||
* Saves the state of all googletest flags. | * Saves the state of all googletest flags. | |||
* Creates a test fixture object for the first test. | * Creates a test fixture object for the first test. | |||
* Initializes it via `SetUp()`. | * Initializes it via `SetUp()`. | |||
* Runs the test on the fixture object. | * Runs the test on the fixture object. | |||
skipping to change at line 457 | skipping to change at line 457 | |||
} // namespace my | } // namespace my | |||
int main(int argc, char **argv) { | int main(int argc, char **argv) { | |||
::testing::InitGoogleTest(&argc, argv); | ::testing::InitGoogleTest(&argc, argv); | |||
return RUN_ALL_TESTS(); | return RUN_ALL_TESTS(); | |||
} | } | |||
``` | ``` | |||
The `::testing::InitGoogleTest()` function parses the command line for | The `::testing::InitGoogleTest()` function parses the command line for | |||
googletest flags, and removes all recognized flags. This allows the user to | googletest flags, and removes all recognized flags. This allows the user to | |||
control a test program's behavior via various flags, which we'll cover in | control a test program's behavior via various flags, which we'll cover in the | |||
the [AdvancedGuide](advanced.md). You **must** call this function before calling | [AdvancedGuide](advanced.md). You **must** call this function before calling | |||
`RUN_ALL_TESTS()`, or the flags won't be properly initialized. | `RUN_ALL_TESTS()`, or the flags won't be properly initialized. | |||
On Windows, `InitGoogleTest()` also works with wide strings, so it can be used | On Windows, `InitGoogleTest()` also works with wide strings, so it can be used | |||
in programs compiled in `UNICODE` mode as well. | in programs compiled in `UNICODE` mode as well. | |||
But maybe you think that writing all those `main` functions is too much work? We | But maybe you think that writing all those `main` functions is too much work? We | |||
agree with you completely, and that's why Google Test provides a basic | agree with you completely, and that's why Google Test provides a basic | |||
implementation of main(). If it fits your needs, then just link your test with | implementation of main(). If it fits your needs, then just link your test with | |||
the `gtest_main` library and you are good to go. | the `gtest_main` library and you are good to go. | |||
End of changes. 4 change blocks. | ||||
10 lines changed or deleted | 9 lines changed or added |