chromium - create new gtest project
程序员文章站
2022-06-04 11:02:02
...
前言
准备在chromium中建立新的gtest工程, 写一些单元测试, 用来理解和验证chromium的知识点.
实验
增加工程配置
在Z:\chromium\src\base\BUILD.gn 尾部加入新的工程配置
值得注意的是, 测试工程的配置必须加入 testonly = true 才能编译的过
# how to build prj_test_task
# put args.gn on out\test_task
# ################################################################################
# args.gn begin
# ################################################################################
# target_os="win"
# target_cpu = "x86"
# is_component_build = true
#
# dcheck_always_on = true
# enable_nacl = false
#
# use_goma = false
# symbol_level = 2
# is_debug = true
# is_win_fastlink = false
# remove_webcore_debug_symbols = false
# ################################################################################
# args.gn end
# ################################################################################
# cd /d Z:\chromium\src\
# gn --ide=vs args out\test_task
# autoninja -C out\test_task prj_test_task
executable("prj_test_task") {
testonly = true
sources = [
"test_task/main.cpp",
]
deps = [
":base",
":i18n",
"//testing/gtest",
]
}
加入编译参数文件
在编译的输出目录加入args.gn
增加 Z:\chromium\src\out\test_task\args.gn
# ################################################################################
# args.gn begin
# ################################################################################
target_os="win"
target_cpu = "x86"
is_component_build = true
dcheck_always_on = true
enable_nacl = false
use_goma = false
symbol_level = 2
is_debug = true
is_win_fastlink = false
remove_webcore_debug_symbols = false
# ################################################################################
# args.gn end
# ################################################################################
加入测试工程的实现
Z:\chromium\src\base\test_task\main.cpp
// @file Z:\chromium\src\base\test_task\main.cpp
// @brief test how to use PostTaskAndReplyWithResult
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "testing/gtest/include/gtest/gtest.h"
TEST(case_name_main, test_name_main)
{
ASSERT_EQ(1,1);
}
int main(int argc, char** argv)
{
printf("gtest from %s\n", __FILE__);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
产生新加入的工程
这次编译,是在vscode中做的
vscode中可以开控制台, 挺方便的。*菜单上,点击‘Terminal’, 开一个新的控制台.
cd Z:\chromium\src
gn --ide=vs args out\test_task
PS C:\Users\LostSpeed> cd Z:\chromium\src\
PS Z:\chromium\src> gn --ide=vs args out\test_task
Waiting for editor on "Z:\chromium\src\out\test_task\args.gn"...
Generating files...
Generating Visual Studio projects took 1411ms
Done. Made 8155 targets from 1512 files in 6141ms
PS Z:\chromium\src> autoninja -C out\test_task prj_test_task
ninja.exe -C out\test_task prj_test_task -l 16
ninja: Entering directory `out\test_task'
[777/777] LINK prj_test_task.exe prj_test_task.exe.pdb
Longest build steps:
0.3 weighted s to build obj/third_party/icu/icuuc/uresbund.obj (1.3 s CPU time)
0.3 weighted s to build obj/base/base/activity_tracker.obj (4.8 s CPU time)
0.3 weighted s to build obj/base/base/memory_dump_manager.obj (5.1 s CPU time)
0.3 weighted s to build obj/base/base/field_trial.obj (5.4 s CPU time)
0.3 weighted s to build obj/third_party/googletest/gtest/gtest.obj (6.0 s CPU time)
0.4 weighted s to build icuuc.dll, icuuc.dll.lib, icuuc.dll.pdb (0.4 s CPU time)
0.4 weighted s to build icui18n.dll, icui18n.dll.lib, icui18n.dll.pdb (0.4 s CPU time)
0.4 weighted s to build obj/base/base/trace_log.obj (7.7 s CPU time)
0.8 weighted s to build obj/base/base/winrt_storage_util.obj (13.7 s CPU time)
0.9 weighted s to build obj/base/base/win_util.obj (16.3 s CPU time)
Time by build-step type:
0.0 s weighted time to generate 1 .dat files (0.1 s CPU time)
0.0 s weighted time to generate 6 .stamp files (0.2 s CPU time)
0.1 s weighted time to generate 5 .lib files (1.2 s CPU time)
1.3 s weighted time to generate 5 PEFile (linking) files (3.5 s CPU time)
64.4 s weighted time to generate 760 .obj files (1147.0 s CPU time)
65.9 s weighted time (1152.1 s CPU time, 17.5x parallelism)
777 build steps completed, average of 11.79/s
运行工程
在vscode中再开一个控制台
cd Z:\chromium\src
cd out
cd test_task
.\prj_test_task.exe
PS Z:\chromium\src\out\test_task> .\prj_test_task.exe
gtest from ../../base/test_task/main.cpp
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from case_name_main
[ RUN ] case_name_main.test_name_main
[ OK ] case_name_main.test_name_main (0 ms)
[----------] 1 test from case_name_main (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (3 ms total)
[ PASSED ] 1 test.
完成
到这,一个最小化的新gtest工程就建立完了。
在这个基础上,就可以添加测试用例了。