Skip to content

Commit 75f0855

Browse files
danbevevanlucas
authored andcommitted
doc: c++ unit test guide lines
PR-URL: #11956 Ref: #9163 Reviewed-By: James M Snell <[email protected]>
1 parent 9662ca1 commit 75f0855

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

doc/guides/writing-tests.md

+62
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,65 @@ If you want to improve tests that have been imported this way, please send
279279
a PR to the upstream project first. When your proposed change is merged in
280280
the upstream project, send another PR here to update Node.js accordingly.
281281
Be sure to update the hash in the URL following `WPT Refs:`.
282+
283+
## C++ Unit test
284+
C++ code can be tested using [Google Test][]. Most features in Node.js can be
285+
tested using the methods described previously in this document. But there are
286+
cases where these might not be enough, for example writing code for Node.js
287+
that will only be called when Node.js is embedded.
288+
289+
### Adding a new test
290+
The unit test should be placed in `test/cctest` and be named with the prefix
291+
`test` followed by the name of unit being tested. For example, the code below
292+
would be placed in `test/cctest/test_env.cc`:
293+
294+
```c++
295+
#include "gtest/gtest.h"
296+
#include "node_test_fixture.h"
297+
#include "env.h"
298+
#include "node.h"
299+
#include "v8.h"
300+
301+
static bool called_cb = false;
302+
static void at_exit_callback(void* arg);
303+
304+
class EnvTest : public NodeTestFixture { };
305+
306+
TEST_F(EnvTest, RunAtExit) {
307+
v8::HandleScope handle_scope(isolate_);
308+
v8::Local<v8::Context> context = v8::Context::New(isolate_);
309+
node::IsolateData* isolateData = node::CreateIsolateData(isolate_, uv_default_loop());
310+
Argv argv{"node", "-e", ";"};
311+
auto env = Environment:CreateEnvironment(isolateData, context, 1, *argv, 2, *argv);
312+
node::AtExit(at_exit_callback);
313+
node::RunAtExit(env);
314+
EXPECT_TRUE(called_cb);
315+
}
316+
317+
static void at_exit_callback(void* arg) {
318+
called_cb = true;
319+
}
320+
```
321+
322+
Next add the test to the `sources` in the `cctest` target in node.gyp:
323+
```
324+
'sources': [
325+
'test/cctest/test_env.cc',
326+
...
327+
],
328+
```
329+
The test can be executed by running the `cctest` target:
330+
```
331+
$ make cctest
332+
```
333+
334+
### Node test fixture
335+
There is a [test fixture] named `node_test_fixture.h` which can be included by
336+
unit tests. The fixture takes care of setting up the Node.js environment
337+
and tearing it down after the tests have finished.
338+
339+
It also contains a helper to create arguments to be passed into Node.js. It
340+
will depend on what is being tested if this is required or not.
341+
342+
[Google Test]: https://github.com/google/googletest
343+
[Test fixture]: https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#test-fixtures-using-the-same-data-configuration-for-multiple-tests

0 commit comments

Comments
 (0)