Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 1 | # GoogleTest FAQ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 2 | |
Gennadiy Civil | 5d3a2cd | 2019-01-03 17:18:03 -0500 | [diff] [blame] | 3 | ## Why should test suite names and test names not contain underscore? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 4 | |
Abseil Team | d9c309f | 2021-02-18 19:18:34 -0500 | [diff] [blame] | 5 | {: .callout .note} |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 6 | Note: GoogleTest reserves underscore (`_`) for special purpose keywords, such as |
Abseil Team | 1fb1bb2 | 2020-09-29 21:52:15 -0400 | [diff] [blame] | 7 | [the `DISABLED_` prefix](advanced.md#temporarily-disabling-tests), in addition |
| 8 | to the following rationale. |
| 9 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 10 | Underscore (`_`) is special, as C++ reserves the following to be used by the |
| 11 | compiler and the standard library: |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 12 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 13 | 1. any identifier that starts with an `_` followed by an upper-case letter, and |
Krystian Kuzniarek | d384b88 | 2019-07-26 14:46:27 +0200 | [diff] [blame] | 14 | 2. any identifier that contains two consecutive underscores (i.e. `__`) |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 15 | *anywhere* in its name. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 16 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 17 | User code is *prohibited* from using such identifiers. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 18 | |
| 19 | Now let's look at what this means for `TEST` and `TEST_F`. |
| 20 | |
Gennadiy Civil | 5d3a2cd | 2019-01-03 17:18:03 -0500 | [diff] [blame] | 21 | Currently `TEST(TestSuiteName, TestName)` generates a class named |
| 22 | `TestSuiteName_TestName_Test`. What happens if `TestSuiteName` or `TestName` |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 23 | contains `_`? |
| 24 | |
Gennadiy Civil | 5d3a2cd | 2019-01-03 17:18:03 -0500 | [diff] [blame] | 25 | 1. If `TestSuiteName` starts with an `_` followed by an upper-case letter (say, |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 26 | `_Foo`), we end up with `_Foo_TestName_Test`, which is reserved and thus |
| 27 | invalid. |
Krystian Kuzniarek | d384b88 | 2019-07-26 14:46:27 +0200 | [diff] [blame] | 28 | 2. If `TestSuiteName` ends with an `_` (say, `Foo_`), we get |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 29 | `Foo__TestName_Test`, which is invalid. |
Krystian Kuzniarek | d384b88 | 2019-07-26 14:46:27 +0200 | [diff] [blame] | 30 | 3. If `TestName` starts with an `_` (say, `_Bar`), we get |
Gennadiy Civil | 5d3a2cd | 2019-01-03 17:18:03 -0500 | [diff] [blame] | 31 | `TestSuiteName__Bar_Test`, which is invalid. |
Krystian Kuzniarek | d384b88 | 2019-07-26 14:46:27 +0200 | [diff] [blame] | 32 | 4. If `TestName` ends with an `_` (say, `Bar_`), we get |
Gennadiy Civil | 5d3a2cd | 2019-01-03 17:18:03 -0500 | [diff] [blame] | 33 | `TestSuiteName_Bar__Test`, which is invalid. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 34 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 35 | So clearly `TestSuiteName` and `TestName` cannot start or end with `_` |
| 36 | (Actually, `TestSuiteName` can start with `_` -- as long as the `_` isn't |
| 37 | followed by an upper-case letter. But that's getting complicated. So for |
| 38 | simplicity we just say that it cannot start with `_`.). |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 39 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 40 | It may seem fine for `TestSuiteName` and `TestName` to contain `_` in the |
| 41 | middle. However, consider this: |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 42 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 43 | ```c++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 44 | TEST(Time, Flies_Like_An_Arrow) { ... } |
| 45 | TEST(Time_Flies, Like_An_Arrow) { ... } |
| 46 | ``` |
| 47 | |
| 48 | Now, the two `TEST`s will both generate the same class |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 49 | (`Time_Flies_Like_An_Arrow_Test`). That's not good. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 50 | |
Gennadiy Civil | 5d3a2cd | 2019-01-03 17:18:03 -0500 | [diff] [blame] | 51 | So for simplicity, we just ask the users to avoid `_` in `TestSuiteName` and |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 52 | `TestName`. The rule is more constraining than necessary, but it's simple and |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 53 | easy to remember. It also gives GoogleTest some wiggle room in case its |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 54 | implementation needs to change in the future. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 55 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 56 | If you violate the rule, there may not be immediate consequences, but your test |
| 57 | may (just may) break with a new compiler (or a new version of the compiler you |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 58 | are using) or with a new version of GoogleTest. Therefore it's best to follow |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 59 | the rule. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 60 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 61 | ## Why does GoogleTest support `EXPECT_EQ(NULL, ptr)` and `ASSERT_EQ(NULL, ptr)` but not `EXPECT_NE(NULL, ptr)` and `ASSERT_NE(NULL, ptr)`? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 62 | |
Abseil Team | 4595745 | 2021-03-23 13:08:11 -0700 | [diff] [blame] | 63 | First of all, you can use `nullptr` with each of these macros, e.g. |
| 64 | `EXPECT_EQ(ptr, nullptr)`, `EXPECT_NE(ptr, nullptr)`, `ASSERT_EQ(ptr, nullptr)`, |
| 65 | `ASSERT_NE(ptr, nullptr)`. This is the preferred syntax in the style guide |
| 66 | because `nullptr` does not have the type problems that `NULL` does. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 67 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 68 | Due to some peculiarity of C++, it requires some non-trivial template meta |
| 69 | programming tricks to support using `NULL` as an argument of the `EXPECT_XX()` |
| 70 | and `ASSERT_XX()` macros. Therefore we only do it where it's most needed |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 71 | (otherwise we make the implementation of GoogleTest harder to maintain and more |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 72 | error-prone than necessary). |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 73 | |
Abseil Team | 4595745 | 2021-03-23 13:08:11 -0700 | [diff] [blame] | 74 | Historically, the `EXPECT_EQ()` macro took the *expected* value as its first |
| 75 | argument and the *actual* value as the second, though this argument order is now |
| 76 | discouraged. It was reasonable that someone wanted |
| 77 | to write `EXPECT_EQ(NULL, some_expression)`, and this indeed was requested |
| 78 | several times. Therefore we implemented it. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 79 | |
Abseil Team | 4595745 | 2021-03-23 13:08:11 -0700 | [diff] [blame] | 80 | The need for `EXPECT_NE(NULL, ptr)` wasn't nearly as strong. When the assertion |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 81 | fails, you already know that `ptr` must be `NULL`, so it doesn't add any |
| 82 | information to print `ptr` in this case. That means `EXPECT_TRUE(ptr != NULL)` |
| 83 | works just as well. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 84 | |
Abseil Team | 4595745 | 2021-03-23 13:08:11 -0700 | [diff] [blame] | 85 | If we were to support `EXPECT_NE(NULL, ptr)`, for consistency we'd have to |
| 86 | support `EXPECT_NE(ptr, NULL)` as well. This means using the template meta |
| 87 | programming tricks twice in the implementation, making it even harder to |
| 88 | understand and maintain. We believe the benefit doesn't justify the cost. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 89 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 90 | Finally, with the growth of the gMock matcher library, we are encouraging people |
| 91 | to use the unified `EXPECT_THAT(value, matcher)` syntax more often in tests. One |
| 92 | significant advantage of the matcher approach is that matchers can be easily |
| 93 | combined to form new matchers, while the `EXPECT_NE`, etc, macros cannot be |
| 94 | easily combined. Therefore we want to invest more in the matchers than in the |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 95 | `EXPECT_XX()` macros. |
| 96 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 97 | ## I need to test that different implementations of an interface satisfy some common requirements. Should I use typed tests or value-parameterized tests? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 98 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 99 | For testing various implementations of the same interface, either typed tests or |
| 100 | value-parameterized tests can get it done. It's really up to you the user to |
| 101 | decide which is more convenient for you, depending on your particular case. Some |
| 102 | rough guidelines: |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 103 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 104 | * Typed tests can be easier to write if instances of the different |
| 105 | implementations can be created the same way, modulo the type. For example, |
| 106 | if all these implementations have a public default constructor (such that |
| 107 | you can write `new TypeParam`), or if their factory functions have the same |
| 108 | form (e.g. `CreateInstance<TypeParam>()`). |
| 109 | * Value-parameterized tests can be easier to write if you need different code |
| 110 | patterns to create different implementations' instances, e.g. `new Foo` vs |
| 111 | `new Bar(5)`. To accommodate for the differences, you can write factory |
| 112 | function wrappers and pass these function pointers to the tests as their |
| 113 | parameters. |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 114 | * When a typed test fails, the default output includes the name of the type, |
| 115 | which can help you quickly identify which implementation is wrong. |
| 116 | Value-parameterized tests only show the number of the failed iteration by |
| 117 | default. You will need to define a function that returns the iteration name |
| 118 | and pass it as the third parameter to INSTANTIATE_TEST_SUITE_P to have more |
| 119 | useful output. |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 120 | * When using typed tests, you need to make sure you are testing against the |
| 121 | interface type, not the concrete types (in other words, you want to make |
| 122 | sure `implicit_cast<MyInterface*>(my_concrete_impl)` works, not just that |
| 123 | `my_concrete_impl` works). It's less likely to make mistakes in this area |
| 124 | when using value-parameterized tests. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 125 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 126 | I hope I didn't confuse you more. :-) If you don't mind, I'd suggest you to give |
| 127 | both approaches a try. Practice is a much better way to grasp the subtle |
| 128 | differences between the two tools. Once you have some concrete experience, you |
| 129 | can much more easily decide which one to use the next time. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 130 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 131 | ## I got some run-time errors about invalid proto descriptors when using `ProtocolMessageEquals`. Help! |
| 132 | |
Abseil Team | d9c309f | 2021-02-18 19:18:34 -0500 | [diff] [blame] | 133 | {: .callout .note} |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 134 | **Note:** `ProtocolMessageEquals` and `ProtocolMessageEquiv` are *deprecated* |
| 135 | now. Please use `EqualsProto`, etc instead. |
| 136 | |
| 137 | `ProtocolMessageEquals` and `ProtocolMessageEquiv` were redefined recently and |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 138 | are now less tolerant of invalid protocol buffer definitions. In particular, if |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 139 | you have a `foo.proto` that doesn't fully qualify the type of a protocol message |
| 140 | it references (e.g. `message<Bar>` where it should be `message<blah.Bar>`), you |
| 141 | will now get run-time errors like: |
| 142 | |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 143 | ``` |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 144 | ... descriptor.cc:...] Invalid proto descriptor for file "path/to/foo.proto": |
| 145 | ... descriptor.cc:...] blah.MyMessage.my_field: ".Bar" is not defined. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 146 | ``` |
| 147 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 148 | If you see this, your `.proto` file is broken and needs to be fixed by making |
| 149 | the types fully qualified. The new definition of `ProtocolMessageEquals` and |
| 150 | `ProtocolMessageEquiv` just happen to reveal your bug. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 151 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 152 | ## My death test modifies some state, but the change seems lost after the death test finishes. Why? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 153 | |
| 154 | Death tests (`EXPECT_DEATH`, etc) are executed in a sub-process s.t. the |
| 155 | expected crash won't kill the test program (i.e. the parent process). As a |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 156 | result, any in-memory side effects they incur are observable in their respective |
| 157 | sub-processes, but not in the parent process. You can think of them as running |
| 158 | in a parallel universe, more or less. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 159 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 160 | In particular, if you use mocking and the death test statement invokes some mock |
| 161 | methods, the parent process will think the calls have never occurred. Therefore, |
| 162 | you may want to move your `EXPECT_CALL` statements inside the `EXPECT_DEATH` |
| 163 | macro. |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 164 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 165 | ## EXPECT_EQ(htonl(blah), blah_blah) generates weird compiler errors in opt mode. Is this a GoogleTest bug? |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 166 | |
| 167 | Actually, the bug is in `htonl()`. |
| 168 | |
| 169 | According to `'man htonl'`, `htonl()` is a *function*, which means it's valid to |
| 170 | use `htonl` as a function pointer. However, in opt mode `htonl()` is defined as |
| 171 | a *macro*, which breaks this usage. |
| 172 | |
| 173 | Worse, the macro definition of `htonl()` uses a `gcc` extension and is *not* |
| 174 | standard C++. That hacky implementation has some ad hoc limitations. In |
| 175 | particular, it prevents you from writing `Foo<sizeof(htonl(x))>()`, where `Foo` |
| 176 | is a template that has an integral argument. |
| 177 | |
| 178 | The implementation of `EXPECT_EQ(a, b)` uses `sizeof(... a ...)` inside a |
| 179 | template argument, and thus doesn't compile in opt mode when `a` contains a call |
| 180 | to `htonl()`. It is difficult to make `EXPECT_EQ` bypass the `htonl()` bug, as |
| 181 | the solution must work with different compilers on various platforms. |
| 182 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 183 | ## The compiler complains about "undefined references" to some static const member variables, but I did define them in the class body. What's wrong? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 184 | |
| 185 | If your class has a static data member: |
| 186 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 187 | ```c++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 188 | // foo.h |
| 189 | class Foo { |
| 190 | ... |
| 191 | static const int kBar = 100; |
| 192 | }; |
| 193 | ``` |
| 194 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 195 | You also need to define it *outside* of the class body in `foo.cc`: |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 196 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 197 | ```c++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 198 | const int Foo::kBar; // No initializer here. |
| 199 | ``` |
| 200 | |
| 201 | Otherwise your code is **invalid C++**, and may break in unexpected ways. In |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 202 | particular, using it in GoogleTest comparison assertions (`EXPECT_EQ`, etc) will |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 203 | generate an "undefined reference" linker error. The fact that "it used to work" |
| 204 | doesn't mean it's valid. It just means that you were lucky. :-) |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 205 | |
Abseil Team | efe7036 | 2020-11-19 09:13:24 -0500 | [diff] [blame] | 206 | If the declaration of the static data member is `constexpr` then it is |
| 207 | implicitly an `inline` definition, and a separate definition in `foo.cc` is not |
| 208 | needed: |
| 209 | |
| 210 | ```c++ |
| 211 | // foo.h |
| 212 | class Foo { |
| 213 | ... |
| 214 | static constexpr int kBar = 100; // Defines kBar, no need to do it in foo.cc. |
| 215 | }; |
| 216 | ``` |
| 217 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 218 | ## Can I derive a test fixture from another? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 219 | |
| 220 | Yes. |
| 221 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 222 | Each test fixture has a corresponding and same named test suite. This means only |
| 223 | one test suite can use a particular fixture. Sometimes, however, multiple test |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 224 | cases may want to use the same or slightly different fixtures. For example, you |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 225 | may want to make sure that all of a GUI library's test suites don't leak |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 226 | important system resources like fonts and brushes. |
| 227 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 228 | In GoogleTest, you share a fixture among test suites by putting the shared logic |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 229 | in a base test fixture, then deriving from that base a separate fixture for each |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 230 | test suite that wants to use this common logic. You then use `TEST_F()` to write |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 231 | tests using each derived fixture. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 232 | |
| 233 | Typically, your code looks like this: |
| 234 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 235 | ```c++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 236 | // Defines a base test fixture. |
| 237 | class BaseTest : public ::testing::Test { |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 238 | protected: |
| 239 | ... |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | // Derives a fixture FooTest from BaseTest. |
| 243 | class FooTest : public BaseTest { |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 244 | protected: |
| 245 | void SetUp() override { |
| 246 | BaseTest::SetUp(); // Sets up the base fixture first. |
| 247 | ... additional set-up work ... |
| 248 | } |
| 249 | |
| 250 | void TearDown() override { |
| 251 | ... clean-up work for FooTest ... |
| 252 | BaseTest::TearDown(); // Remember to tear down the base fixture |
| 253 | // after cleaning up FooTest! |
| 254 | } |
| 255 | |
| 256 | ... functions and variables for FooTest ... |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | // Tests that use the fixture FooTest. |
| 260 | TEST_F(FooTest, Bar) { ... } |
| 261 | TEST_F(FooTest, Baz) { ... } |
| 262 | |
| 263 | ... additional fixtures derived from BaseTest ... |
| 264 | ``` |
| 265 | |
| 266 | If necessary, you can continue to derive test fixtures from a derived fixture. |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 267 | GoogleTest has no limit on how deep the hierarchy can be. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 268 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 269 | For a complete example using derived test fixtures, see |
assafpr | b59ae84 | 2022-06-22 16:12:54 +0300 | [diff] [blame] | 270 | [sample5_unittest.cc](https://github.com/google/googletest/blob/main/googletest/samples/sample5_unittest.cc). |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 271 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 272 | ## My compiler complains "void value not ignored as it ought to be." What does this mean? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 273 | |
| 274 | You're probably using an `ASSERT_*()` in a function that doesn't return `void`. |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 275 | `ASSERT_*()` can only be used in `void` functions, due to exceptions being |
| 276 | disabled by our build system. Please see more details |
| 277 | [here](advanced.md#assertion-placement). |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 278 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 279 | ## My death test hangs (or seg-faults). How do I fix it? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 280 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 281 | In GoogleTest, death tests are run in a child process and the way they work is |
Abseil Team | d5d6ff9 | 2021-05-25 19:49:11 -0400 | [diff] [blame] | 282 | delicate. To write death tests you really need to understand how they work—see |
| 283 | the details at [Death Assertions](reference/assertions.md#death) in the |
| 284 | Assertions Reference. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 285 | |
| 286 | In particular, death tests don't like having multiple threads in the parent |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 287 | process. So the first thing you can try is to eliminate creating threads outside |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 288 | of `EXPECT_DEATH()`. For example, you may want to use mocks or fake objects |
| 289 | instead of real ones in your tests. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 290 | |
| 291 | Sometimes this is impossible as some library you must use may be creating |
| 292 | threads before `main()` is even reached. In this case, you can try to minimize |
| 293 | the chance of conflicts by either moving as many activities as possible inside |
| 294 | `EXPECT_DEATH()` (in the extreme case, you want to move everything inside), or |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 295 | leaving as few things as possible in it. Also, you can try to set the death test |
| 296 | style to `"threadsafe"`, which is safer but slower, and see if it helps. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 297 | |
| 298 | If you go with thread-safe death tests, remember that they rerun the test |
| 299 | program from the beginning in the child process. Therefore make sure your |
| 300 | program can run side-by-side with itself and is deterministic. |
| 301 | |
| 302 | In the end, this boils down to good concurrent programming. You have to make |
Ashik Paul | c4a5ee3 | 2020-07-11 12:13:05 +0530 | [diff] [blame] | 303 | sure that there are no race conditions or deadlocks in your program. No silver |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 304 | bullet - sorry! |
| 305 | |
Abseil Team | 6123df9 | 2019-09-05 17:40:42 -0400 | [diff] [blame] | 306 | ## Should I use the constructor/destructor of the test fixture or SetUp()/TearDown()? {#CtorVsSetUp} |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 307 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 308 | The first thing to remember is that GoogleTest does **not** reuse the same test |
| 309 | fixture object across multiple tests. For each `TEST_F`, GoogleTest will create |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 310 | a **fresh** test fixture object, immediately call `SetUp()`, run the test body, |
| 311 | call `TearDown()`, and then delete the test fixture object. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 312 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 313 | When you need to write per-test set-up and tear-down logic, you have the choice |
| 314 | between using the test fixture constructor/destructor or `SetUp()/TearDown()`. |
| 315 | The former is usually preferred, as it has the following benefits: |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 316 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 317 | * By initializing a member variable in the constructor, we have the option to |
| 318 | make it `const`, which helps prevent accidental changes to its value and |
| 319 | makes the tests more obviously correct. |
| 320 | * In case we need to subclass the test fixture class, the subclass' |
| 321 | constructor is guaranteed to call the base class' constructor *first*, and |
| 322 | the subclass' destructor is guaranteed to call the base class' destructor |
| 323 | *afterward*. With `SetUp()/TearDown()`, a subclass may make the mistake of |
| 324 | forgetting to call the base class' `SetUp()/TearDown()` or call them at the |
| 325 | wrong time. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 326 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 327 | You may still want to use `SetUp()/TearDown()` in the following cases: |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 328 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 329 | * C++ does not allow virtual function calls in constructors and destructors. |
| 330 | You can call a method declared as virtual, but it will not use dynamic |
Abseil Team | 2d07f12 | 2022-01-12 07:19:16 -0800 | [diff] [blame] | 331 | dispatch. It will use the definition from the class the constructor of which |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 332 | is currently executing. This is because calling a virtual method before the |
| 333 | derived class constructor has a chance to run is very dangerous - the |
| 334 | virtual method might operate on uninitialized data. Therefore, if you need |
| 335 | to call a method that will be overridden in a derived class, you have to use |
| 336 | `SetUp()/TearDown()`. |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 337 | * In the body of a constructor (or destructor), it's not possible to use the |
| 338 | `ASSERT_xx` macros. Therefore, if the set-up operation could cause a fatal |
| 339 | test failure that should prevent the test from running, it's necessary to |
Abseil Team | 8a76186 | 2021-01-25 20:51:26 -0500 | [diff] [blame] | 340 | use `abort` and abort the whole test |
| 341 | executable, or to use `SetUp()` instead of a constructor. |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 342 | * If the tear-down operation could throw an exception, you must use |
| 343 | `TearDown()` as opposed to the destructor, as throwing in a destructor leads |
| 344 | to undefined behavior and usually will kill your program right away. Note |
| 345 | that many standard libraries (like STL) may throw when exceptions are |
| 346 | enabled in the compiler. Therefore you should prefer `TearDown()` if you |
| 347 | want to write portable tests that work with or without exceptions. |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 348 | * The GoogleTest team is considering making the assertion macros throw on |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 349 | platforms where exceptions are enabled (e.g. Windows, Mac OS, and Linux |
| 350 | client-side), which will eliminate the need for the user to propagate |
| 351 | failures from a subroutine to its caller. Therefore, you shouldn't use |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 352 | GoogleTest assertions in a destructor if your code could run on such a |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 353 | platform. |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 354 | |
| 355 | ## The compiler complains "no matching function to call" when I use ASSERT_PRED*. How do I fix it? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 356 | |
Abseil Team | d5d6ff9 | 2021-05-25 19:49:11 -0400 | [diff] [blame] | 357 | See details for [`EXPECT_PRED*`](reference/assertions.md#EXPECT_PRED) in the |
| 358 | Assertions Reference. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 359 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 360 | ## My compiler complains about "ignoring return value" when I call RUN_ALL_TESTS(). Why? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 361 | |
| 362 | Some people had been ignoring the return value of `RUN_ALL_TESTS()`. That is, |
| 363 | instead of |
| 364 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 365 | ```c++ |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 366 | return RUN_ALL_TESTS(); |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 367 | ``` |
| 368 | |
| 369 | they write |
| 370 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 371 | ```c++ |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 372 | RUN_ALL_TESTS(); |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 373 | ``` |
| 374 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 375 | This is **wrong and dangerous**. The testing services needs to see the return |
| 376 | value of `RUN_ALL_TESTS()` in order to determine if a test has passed. If your |
| 377 | `main()` function ignores it, your test will be considered successful even if it |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 378 | has a GoogleTest assertion failure. Very bad. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 379 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 380 | We have decided to fix this (thanks to Michael Chastain for the idea). Now, your |
| 381 | code will no longer be able to ignore `RUN_ALL_TESTS()` when compiled with |
| 382 | `gcc`. If you do so, you'll get a compiler error. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 383 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 384 | If you see the compiler complaining about you ignoring the return value of |
| 385 | `RUN_ALL_TESTS()`, the fix is simple: just make sure its value is used as the |
| 386 | return value of `main()`. |
| 387 | |
| 388 | But how could we introduce a change that breaks existing tests? Well, in this |
| 389 | case, the code was already broken in the first place, so we didn't break it. :-) |
| 390 | |
| 391 | ## My compiler complains that a constructor (or destructor) cannot return a value. What's going on? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 392 | |
| 393 | Due to a peculiarity of C++, in order to support the syntax for streaming |
| 394 | messages to an `ASSERT_*`, e.g. |
| 395 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 396 | ```c++ |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 397 | ASSERT_EQ(1, Foo()) << "blah blah" << foo; |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 398 | ``` |
| 399 | |
| 400 | we had to give up using `ASSERT*` and `FAIL*` (but not `EXPECT*` and |
| 401 | `ADD_FAILURE*`) in constructors and destructors. The workaround is to move the |
| 402 | content of your constructor/destructor to a private void member function, or |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 403 | switch to `EXPECT_*()` if that works. This |
| 404 | [section](advanced.md#assertion-placement) in the user's guide explains it. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 405 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 406 | ## My SetUp() function is not called. Why? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 407 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 408 | C++ is case-sensitive. Did you spell it as `Setup()`? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 409 | |
Gennadiy Civil | 5d3a2cd | 2019-01-03 17:18:03 -0500 | [diff] [blame] | 410 | Similarly, sometimes people spell `SetUpTestSuite()` as `SetupTestSuite()` and |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 411 | wonder why it's never called. |
| 412 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 413 | ## 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. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 414 | |
| 415 | You don't have to. Instead of |
| 416 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 417 | ```c++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 418 | class FooTest : public BaseTest {}; |
| 419 | |
| 420 | TEST_F(FooTest, Abc) { ... } |
| 421 | TEST_F(FooTest, Def) { ... } |
| 422 | |
| 423 | class BarTest : public BaseTest {}; |
| 424 | |
| 425 | TEST_F(BarTest, Abc) { ... } |
| 426 | TEST_F(BarTest, Def) { ... } |
| 427 | ``` |
| 428 | |
| 429 | you can simply `typedef` the test fixtures: |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 430 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 431 | ```c++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 432 | typedef BaseTest FooTest; |
| 433 | |
| 434 | TEST_F(FooTest, Abc) { ... } |
| 435 | TEST_F(FooTest, Def) { ... } |
| 436 | |
| 437 | typedef BaseTest BarTest; |
| 438 | |
| 439 | TEST_F(BarTest, Abc) { ... } |
| 440 | TEST_F(BarTest, Def) { ... } |
| 441 | ``` |
| 442 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 443 | ## GoogleTest output is buried in a whole bunch of LOG messages. What do I do? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 444 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 445 | The GoogleTest output is meant to be a concise and human-friendly report. If |
| 446 | your test generates textual output itself, it will mix with the GoogleTest |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 447 | output, making it hard to read. However, there is an easy solution to this |
| 448 | problem. |
| 449 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 450 | Since `LOG` messages go to stderr, we decided to let GoogleTest output go to |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 451 | stdout. This way, you can easily separate the two using redirection. For |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 452 | example: |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 453 | |
| 454 | ```shell |
| 455 | $ ./my_test > gtest_output.txt |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 456 | ``` |
| 457 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 458 | ## Why should I prefer test fixtures over global variables? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 459 | |
| 460 | There are several good reasons: |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 461 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 462 | 1. It's likely your test needs to change the states of its global variables. |
| 463 | This makes it difficult to keep side effects from escaping one test and |
| 464 | contaminating others, making debugging difficult. By using fixtures, each |
| 465 | test has a fresh set of variables that's different (but with the same |
| 466 | names). Thus, tests are kept independent of each other. |
Krystian Kuzniarek | d384b88 | 2019-07-26 14:46:27 +0200 | [diff] [blame] | 467 | 2. Global variables pollute the global namespace. |
| 468 | 3. Test fixtures can be reused via subclassing, which cannot be done easily |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 469 | with global variables. This is useful if many test suites have something in |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 470 | common. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 471 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 472 | ## What can the statement argument in ASSERT_DEATH() be? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 473 | |
hyuk.myeong | 51f7396 | 2019-10-01 14:24:55 +0900 | [diff] [blame] | 474 | `ASSERT_DEATH(statement, matcher)` (or any death assertion macro) can be used |
| 475 | wherever *`statement`* is valid. So basically *`statement`* can be any C++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 476 | statement that makes sense in the current context. In particular, it can |
| 477 | reference global and/or local variables, and can be: |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 478 | |
| 479 | * a simple function call (often the case), |
| 480 | * a complex expression, or |
| 481 | * a compound statement. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 482 | |
Arkady Shapkin | 83b93ea | 2015-11-28 17:59:51 +0300 | [diff] [blame] | 483 | Some examples are shown here: |
| 484 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 485 | ```c++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 486 | // A death test can be a simple function call. |
| 487 | TEST(MyDeathTest, FunctionCall) { |
| 488 | ASSERT_DEATH(Xyz(5), "Xyz failed"); |
| 489 | } |
| 490 | |
| 491 | // Or a complex expression that references variables and functions. |
| 492 | TEST(MyDeathTest, ComplexExpression) { |
| 493 | const bool c = Condition(); |
| 494 | ASSERT_DEATH((c ? Func1(0) : object2.Method("test")), |
| 495 | "(Func1|Method) failed"); |
| 496 | } |
| 497 | |
Ashik Paul | c4a5ee3 | 2020-07-11 12:13:05 +0530 | [diff] [blame] | 498 | // Death assertions can be used anywhere in a function. In |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 499 | // particular, they can be inside a loop. |
| 500 | TEST(MyDeathTest, InsideLoop) { |
| 501 | // Verifies that Foo(0), Foo(1), ..., and Foo(4) all die. |
| 502 | for (int i = 0; i < 5; i++) { |
| 503 | EXPECT_DEATH_M(Foo(i), "Foo has \\d+ errors", |
| 504 | ::testing::Message() << "where i is " << i); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // A death assertion can contain a compound statement. |
| 509 | TEST(MyDeathTest, CompoundStatement) { |
| 510 | // Verifies that at lease one of Bar(0), Bar(1), ..., and |
| 511 | // Bar(4) dies. |
| 512 | ASSERT_DEATH({ |
| 513 | for (int i = 0; i < 5; i++) { |
| 514 | Bar(i); |
| 515 | } |
| 516 | }, |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 517 | "Bar has \\d+ errors"); |
| 518 | } |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 519 | ``` |
| 520 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 521 | ## I have a fixture class `FooTest`, but `TEST_F(FooTest, Bar)` gives me error ``"no matching function for call to `FooTest::FooTest()'"``. Why? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 522 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 523 | GoogleTest needs to be able to create objects of your test fixture class, so it |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 524 | must have a default constructor. Normally the compiler will define one for you. |
| 525 | However, there are cases where you have to define your own: |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 526 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 527 | * If you explicitly declare a non-default constructor for class `FooTest` |
| 528 | (`DISALLOW_EVIL_CONSTRUCTORS()` does this), then you need to define a |
| 529 | default constructor, even if it would be empty. |
| 530 | * If `FooTest` has a const non-static data member, then you have to define the |
| 531 | default constructor *and* initialize the const member in the initializer |
| 532 | list of the constructor. (Early versions of `gcc` doesn't force you to |
| 533 | initialize the const member. It's a bug that has been fixed in `gcc 4`.) |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 534 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 535 | ## Why does ASSERT_DEATH complain about previous threads that were already joined? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 536 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 537 | With the Linux pthread library, there is no turning back once you cross the line |
Ashik Paul | c4a5ee3 | 2020-07-11 12:13:05 +0530 | [diff] [blame] | 538 | from a single thread to multiple threads. The first time you create a thread, a |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 539 | manager thread is created in addition, so you get 3, not 2, threads. Later when |
| 540 | the thread you create joins the main thread, the thread count decrements by 1, |
| 541 | but the manager thread will never be killed, so you still have 2 threads, which |
| 542 | means you cannot safely run a death test. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 543 | |
| 544 | The new NPTL thread library doesn't suffer from this problem, as it doesn't |
| 545 | create a manager thread. However, if you don't control which machine your test |
| 546 | runs on, you shouldn't depend on this. |
| 547 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 548 | ## Why does GoogleTest require the entire test suite, instead of individual tests, to be named *DeathTest when it uses ASSERT_DEATH? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 549 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 550 | GoogleTest does not interleave tests from different test suites. That is, it |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 551 | runs all tests in one test suite first, and then runs all tests in the next test |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 552 | suite, and so on. GoogleTest does this because it needs to set up a test suite |
Ashik Paul | c4a5ee3 | 2020-07-11 12:13:05 +0530 | [diff] [blame] | 553 | before the first test in it is run, and tear it down afterwards. Splitting up |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 554 | the test case would require multiple set-up and tear-down processes, which is |
| 555 | inefficient and makes the semantics unclean. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 556 | |
| 557 | If we were to determine the order of tests based on test name instead of test |
| 558 | case name, then we would have a problem with the following situation: |
| 559 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 560 | ```c++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 561 | TEST_F(FooTest, AbcDeathTest) { ... } |
| 562 | TEST_F(FooTest, Uvw) { ... } |
| 563 | |
| 564 | TEST_F(BarTest, DefDeathTest) { ... } |
| 565 | TEST_F(BarTest, Xyz) { ... } |
| 566 | ``` |
| 567 | |
| 568 | Since `FooTest.AbcDeathTest` needs to run before `BarTest.Xyz`, and we don't |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 569 | interleave tests from different test suites, we need to run all tests in the |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 570 | `FooTest` case before running any test in the `BarTest` case. This contradicts |
| 571 | with the requirement to run `BarTest.DefDeathTest` before `FooTest.Uvw`. |
| 572 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 573 | ## But I don't like calling my entire test suite \*DeathTest when it contains both death tests and non-death tests. What do I do? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 574 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 575 | You don't have to, but if you like, you may split up the test suite into |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 576 | `FooTest` and `FooDeathTest`, where the names make it clear that they are |
| 577 | related: |
| 578 | |
Gennadiy Civil | 3847aec | 2018-06-13 14:29:26 -0400 | [diff] [blame] | 579 | ```c++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 580 | class FooTest : public ::testing::Test { ... }; |
| 581 | |
| 582 | TEST_F(FooTest, Abc) { ... } |
| 583 | TEST_F(FooTest, Def) { ... } |
| 584 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 585 | using FooDeathTest = FooTest; |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 586 | |
| 587 | TEST_F(FooDeathTest, Uvw) { ... EXPECT_DEATH(...) ... } |
| 588 | TEST_F(FooDeathTest, Xyz) { ... ASSERT_DEATH(...) ... } |
| 589 | ``` |
| 590 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 591 | ## GoogleTest prints the LOG messages in a death test's child process only when the test fails. How can I see the LOG messages when the death test succeeds? |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 592 | |
| 593 | Printing the LOG messages generated by the statement inside `EXPECT_DEATH()` |
| 594 | makes it harder to search for real problems in the parent's log. Therefore, |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 595 | GoogleTest only prints them when the death test has failed. |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 596 | |
| 597 | If you really need to see such LOG messages, a workaround is to temporarily |
| 598 | break the death test (e.g. by changing the regex pattern it is expected to |
| 599 | match). Admittedly, this is a hack. We'll consider a more permanent solution |
| 600 | after the fork-and-exec-style death tests are implemented. |
| 601 | |
Abseil Team | 1a5a78b | 2021-02-11 20:50:30 -0500 | [diff] [blame] | 602 | ## The compiler complains about `no match for 'operator<<'` when I use an assertion. What gives? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 603 | |
| 604 | If you use a user-defined type `FooType` in an assertion, you must make sure |
| 605 | there is an `std::ostream& operator<<(std::ostream&, const FooType&)` function |
| 606 | defined such that we can print a value of `FooType`. |
| 607 | |
| 608 | In addition, if `FooType` is declared in a name space, the `<<` operator also |
Abseil Team | 1a5a78b | 2021-02-11 20:50:30 -0500 | [diff] [blame] | 609 | needs to be defined in the *same* name space. See |
| 610 | [Tip of the Week #49](http://abseil.io/tips/49) for details. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 611 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 612 | ## How do I suppress the memory leak messages on Windows? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 613 | |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 614 | Since the statically initialized GoogleTest singleton requires allocations on |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 615 | the heap, the Visual C++ memory leak detector will report memory leaks at the |
| 616 | end of the program run. The easiest way to avoid this is to use the |
| 617 | `_CrtMemCheckpoint` and `_CrtMemDumpAllObjectsSince` calls to not report any |
| 618 | statically initialized heap objects. See MSDN for more details and additional |
| 619 | heap check/debug routines. |
| 620 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 621 | ## How can my code detect if it is running in a test? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 622 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 623 | If you write code that sniffs whether it's running in a test and does different |
| 624 | things accordingly, you are leaking test-only logic into production code and |
| 625 | there is no easy way to ensure that the test-only code paths aren't run by |
| 626 | mistake in production. Such cleverness also leads to |
| 627 | [Heisenbugs](https://en.wikipedia.org/wiki/Heisenbug). Therefore we strongly |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 628 | advise against the practice, and GoogleTest doesn't provide a way to do it. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 629 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 630 | In general, the recommended way to cause the code to behave differently under |
Abseil Team | 1a5a78b | 2021-02-11 20:50:30 -0500 | [diff] [blame] | 631 | test is [Dependency Injection](http://en.wikipedia.org/wiki/Dependency_injection). You can inject |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 632 | different functionality from the test and from the production code. Since your |
| 633 | production code doesn't link in the for-test logic at all (the |
Abseil Team | 1a5a78b | 2021-02-11 20:50:30 -0500 | [diff] [blame] | 634 | [`testonly`](http://docs.bazel.build/versions/master/be/common-definitions.html#common.testonly) attribute for BUILD targets helps to ensure |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 635 | that), there is no danger in accidentally running it. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 636 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 637 | However, if you *really*, *really*, *really* have no choice, and if you follow |
| 638 | the rule of ending your test program names with `_test`, you can use the |
| 639 | *horrible* hack of sniffing your executable name (`argv[0]` in `main()`) to know |
| 640 | whether the code is under test. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 641 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 642 | ## How do I temporarily disable a test? |
| 643 | |
| 644 | If you have a broken test that you cannot fix right away, you can add the |
Abseil Team | 66836f0 | 2021-03-24 18:20:36 -0700 | [diff] [blame] | 645 | `DISABLED_` prefix to its name. This will exclude it from execution. This is |
| 646 | better than commenting out the code or using `#if 0`, as disabled tests are |
| 647 | still compiled (and thus won't rot). |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 648 | |
| 649 | To include disabled tests in test execution, just invoke the test program with |
Abseil Team | 66836f0 | 2021-03-24 18:20:36 -0700 | [diff] [blame] | 650 | the `--gtest_also_run_disabled_tests` flag. |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 651 | |
| 652 | ## Is it OK if I have two separate `TEST(Foo, Bar)` test methods defined in different namespaces? |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 653 | |
| 654 | Yes. |
| 655 | |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 656 | The rule is **all test methods in the same test suite must use the same fixture |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 657 | class.** This means that the following is **allowed** because both tests use the |
| 658 | same fixture class (`::testing::Test`). |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 659 | |
Gennadiy Civil | 6a484ba | 2018-06-13 14:26:24 -0400 | [diff] [blame] | 660 | ```c++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 661 | namespace foo { |
| 662 | TEST(CoolTest, DoSomething) { |
| 663 | SUCCEED(); |
| 664 | } |
| 665 | } // namespace foo |
| 666 | |
| 667 | namespace bar { |
| 668 | TEST(CoolTest, DoSomething) { |
| 669 | SUCCEED(); |
| 670 | } |
Herbert Thielen | 14cf7f5 | 2017-08-31 16:10:36 +0200 | [diff] [blame] | 671 | } // namespace bar |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 672 | ``` |
| 673 | |
Gennadiy Civil | 5437926 | 2018-07-17 17:47:25 -0400 | [diff] [blame] | 674 | However, the following code is **not allowed** and will produce a runtime error |
Abseil Team | cf942a5 | 2022-05-25 19:16:56 -0700 | [diff] [blame] | 675 | from GoogleTest because the test methods are using different test fixture |
Gennadiy Civil | 834dff3 | 2019-06-24 11:16:58 -0400 | [diff] [blame] | 676 | classes with the same test suite name. |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 677 | |
Gennadiy Civil | 6a484ba | 2018-06-13 14:26:24 -0400 | [diff] [blame] | 678 | ```c++ |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 679 | namespace foo { |
| 680 | class CoolTest : public ::testing::Test {}; // Fixture foo::CoolTest |
| 681 | TEST_F(CoolTest, DoSomething) { |
| 682 | SUCCEED(); |
| 683 | } |
| 684 | } // namespace foo |
| 685 | |
| 686 | namespace bar { |
| 687 | class CoolTest : public ::testing::Test {}; // Fixture: bar::CoolTest |
| 688 | TEST_F(CoolTest, DoSomething) { |
| 689 | SUCCEED(); |
| 690 | } |
Herbert Thielen | 14cf7f5 | 2017-08-31 16:10:36 +0200 | [diff] [blame] | 691 | } // namespace bar |
Google Code Exporter | 642acbd | 2015-07-28 11:15:35 -0400 | [diff] [blame] | 692 | ``` |