How to use Google Test on Android-4.0.3_r1

来源:互联网 发布:java中求字符串的长度 编辑:程序博客网 时间:2024/05/16 17:20
AOSP has Google Test project in extarnal/gtest directory.

I try to use Google Test in my Application.

Create Android.mk for Google Test

I was referring to the existing Android.mk in extarnal/gtest/test.

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)# Gtest depends on STLPort which does not build on host/simulator.ifeq ($(BUILD_WITH_ASTL),true)libgtest_test_includes := \    bionic/libstdc++/include \    external/astl/include \    external/gtest/include \ $(LOCAL_PATH)/../test \ $(LOCAL_PATH)/..libgtest_test_static_lib := libgtest_main libgtest libastllibgtest_test_shared_lib :=libgtest_test_host_static_lib := libgtest_main_host libgtest_host libastl_hostlibgtest_test_host_shared_lib :=else# BUILD_WITH_ASTL could be undefined, force it to false (for the guard# before the test-target call).BUILD_WITH_ASTL := falselibgtest_test_includes := \    bionic \    external/stlport/stlport \    external/gtest/include \ $(LOCAL_PATH)/../test \ $(LOCAL_PATH)/..libgtest_test_static_lib := libgtest_main libgtestlibgtest_test_shared_lib := libstlportlibgtest_test_host_static_lib :=libgtest_test_host_shared_lib :=endifLOCAL_MODULE_TAGS := TestAppGtestLOCAL_C_INCLUDES := $(libgtest_test_includes)LOCAL_SHARED_LIBRARIES := \    libcutils \    libutils \    libandroid_runtime \    $(libgtest_test_shared_lib) \    $(libgtest_test_host_shared_lib)LOCAL_STATIC_LIBRARIES := \    $(libgtest_test_static_lib) \    $(libgtest_test_host_static_lib)# if LOCAL_SRC_FILES is .cc#LOCAL_CPP_EXTENSION := .cc# YOUR Source and Test SourceLOCAL_SRC_FILES := \    sample1.cpp \    sample1_unittest.cppLOCAL_MODULE := gtest_mytestLOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)include $(BUILD_EXECUTABLE)

Building Google Test

I try to build an apllicaion with google test code.

I uploaded sample project.
https://docs.google.com/open?id=0BwdCdBWuE_7cb2ZzVGt3eHhSOEtEbGVxNnRCSkFJdw

$ cd "YOUR_BUILD_DIRECTORY"$  . build/envsetup.sh$ lunch "YOUR_BUILD_LUNCH"$ make$ cd packages/apps/$ cd TestApp$ mm -B


If building success , executable file is created.
target thumb C++: gtest_mytest <= packages/apps/TestApp/jni/test/sample1.cpptarget thumb C++: gtest_mytest <= packages/apps/TestApp/jni/test/sample1_unittest.cpptarget Executable: gtest_mytest (out/target/product/maguro/obj/EXECUTABLES/gtest_mytest_intermediates/LINKED/gtest_mytest)target Symbolic: gtest_mytest (out/target/product/maguro/symbols/data/app/gtest_mytest)target Strip: gtest_mytest (out/target/product/maguro/obj/EXECUTABLES/gtest_mytest_intermediates/gtest_mytest)Install: out/target/product/maguro/data/app/gtest_mytest

Running Google Test

I try to run Google Test on Galaxy Nexus. My Galaxy Nexus is flashed Android-4.0.3_r1. Copy to executable file.
cd out/target/product/maguro/$ adb push data/app/gtest_mytest /data/app2870 KB/s (48728 bytes in 0.016s)
Running test using ADB.
$ adb shellroot@android:/ # cd data/app                                                   root@android:/data/app # ./gtest_mytest    
Success to run, The test results are displayed.
root@android:/data/app # ./gtest_mytest                                        Running main() from gtest_main.cc[==========] Running 6 tests from 2 test cases.[----------] Global test environment set-up.[----------] 3 tests from FactorialTest[ RUN      ] FactorialTest.Negative[       OK ] FactorialTest.Negative[ RUN      ] FactorialTest.Zero[       OK ] FactorialTest.Zero[ RUN      ] FactorialTest.Positive[       OK ] FactorialTest.Positive[----------] 3 tests from IsPrimeTest[ RUN      ] IsPrimeTest.Negative[       OK ] IsPrimeTest.Negative[ RUN      ] IsPrimeTest.Trivial[       OK ] IsPrimeTest.Trivial[ RUN      ] IsPrimeTest.Positive[       OK ] IsPrimeTest.Positive[----------] Global test environment tear-down[==========] 6 tests from 2 test cases ran.[  PASSED  ] 6 tests.
原创粉丝点击