下載 googleTest 源碼
從github上直接clone 最新的googleTest源碼:
創(chuàng)建一個(gè)本地路徑,如:
c:/test/GoogleTest
然后進(jìn)入到該路徑下直接clone最新的代碼:
git clone https://github.com/google/googletest.git
NDK編譯
當(dāng)前的目錄結(jié)構(gòu):
c:/test/GoogleTest/googletest
c:/test/GoogleTest/googlemock
我們進(jìn)入到googletest子路徑中:
cd c:/test/GoogleTest/googletest
mdkir jni
創(chuàng)建jni路徑后宏怔,我們就需要對(duì)應(yīng)的Android.mk 和 Application.mk去調(diào)用NDK進(jìn)行編譯协饲。
可以從如下地址中下載NDK reference其jni路徑中的Android.mk及Application.mk并拷貝到本地的路徑中:
c:/test/GoogleTest/googletest/jni
拷貝完成后進(jìn)入到如下路徑并執(zhí)行ndk-build進(jìn)行編譯:
cd c:/test/GoogleTest/googletest/
ndk-build
如果還未配置ndk,那就需要先去安裝ndk并且配置到系統(tǒng)環(huán)境變量中去辽故。
當(dāng)編譯完成之后,會(huì)jni同級(jí)路徑下生成obj文件夾,在此路徑下回生成若干平臺(tái)的LIB文件翁潘,當(dāng)然可以根據(jù)需要修改Android.mk去生成對(duì)應(yīng)的動(dòng)態(tài)鏈接庫或者靜態(tài)鏈接庫等。
其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如何使用本篇便不再贅述歼争,當(dāng)編寫了大量的測(cè)試用例進(jìn)行測(cè)試時(shí)拜马,有時(shí)我們需要僅針對(duì)部分用例進(jìn)行驗(yàn)證渗勘。
針對(duì)部分用例測(cè)試
假設(shè)我們編寫了如下測(cè)試用例:
------------------------------------------------------
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();
}
-------------------------------------------------
如果僅需要運(yùn)行case2模塊相關(guān)用例,編譯出來的可執(zhí)行文件為test.exe,那么:
//運(yùn)行所有case2的用例
test.exe --gtest_filter=case2.*
//運(yùn)行某個(gè)具體用例
test.exe --gtest_filter=case3.functionA_test
此時(shí)我們便可以實(shí)現(xiàn)按照實(shí)際需求進(jìn)行用例驗(yàn)證俩莽。
CSDN同步發(fā)布地址