下载 googleTest 源码
从github上直接clone 最新的googleTest源码:
创建一个本地路径,如:
c:/test/GoogleTest
然后进入到该路径下直接clone最新的代码:
git clone https://github.com/google/googletest.git
NDK编译
当前的目录结构:
c:/test/GoogleTest/googletest
c:/test/GoogleTest/googlemock
我们进入到googletest子路径中:
cd c:/test/GoogleTest/googletest
mdkir jni
创建jni路径后,我们就需要对应的Android.mk 和 Application.mk去调用NDK进行编译。
可以从如下地址中下载NDK reference其jni路径中的Android.mk及Application.mk并拷贝到本地的路径中:
c:/test/GoogleTest/googletest/jni
拷贝完成后进入到如下路径并执行ndk-build进行编译:
cd c:/test/GoogleTest/googletest/
ndk-build
如果还未配置ndk,那就需要先去安装ndk并且配置到系统环境变量中去。
当编译完成之后,会jni同级路径下生成obj文件夹,在此路径下回生成若干平台的LIB文件,当然可以根据需要修改Android.mk去生成对应的动态链接库或者静态链接库等。
其Android.mk 参考如下:
# The MIT License (MIT)
#
# Copyright (c) 2013 Fukuta, Shinya.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CPP_EXTENSION := .cc
LOCAL_MODULE := libgtest
LOCAL_C_INCLUDES := include .
LOCAL_SRC_FILES := ../src/gtest-all.cc
include $(BUILD_SHARED_LIBRARY)
其Application.mk 参考如下:
# The MIT License (MIT)
#
# Copyright (c) 2013 Fukuta, Shinya.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
APP_MODULES := libgtest
APP_STL := gnustl_static
NDK_TOOLCHAIN_VERSION := clang
Google Test的使用
Google Test如何使用本篇便不再赘述,当编写了大量的测试用例进行测试时,有时我们需要仅针对部分用例进行验证。
针对部分用例测试
假设我们编写了如下测试用例:
------------------------------------------------------
TEST(case1, functionA_test) {
....
}
TEST(case1, functionB_test) {
....
}
TEST(case2, functionA_test) {
.....
}
TEST(case2, functionB_test){
....
}
TEST(case3, functionA_test) {
....
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
-------------------------------------------------
如果仅需要运行case2模块相关用例,编译出来的可执行文件为test.exe,那么:
//运行所有case2的用例
test.exe --gtest_filter=case2.*
//运行某个具体用例
test.exe --gtest_filter=case3.functionA_test
此时我们便可以实现按照实际需求进行用例验证。
CSDN同步发布地址