Android为Java层应用程序提供了基于JUnit的单元测试框架,本文探讨的是native代码的单元测试。native代码的单元测试框架选择很多,如CppUnit/UnitTest++/gtest等等。惭愧的是,虽然一直在android下从事native程序的开发,却一直没有写过单元测试用例,究其原因,主要有两个:一是赶项目进度,上头关心的是代码何时完成/功能何时完成,这也符合中国的国情,第一时间忽悠客户才是要务。至于代码质量,no one care,只要演示时不出状况就行,至于问题,可以慢慢解决;二是没有顺手的单元测试工具,从单元测试用例的编写,到部署,都是一个非常耗时的工作,如果没有工具自动化,积极性容易受挫。
其实写代码的人都知道,bug发现越晚,修复的代价越大,但是习惯上的惰性使得我将排查问题的时机尽量拖后,基本上没有写过单元测试用例。近期一直在关注chromium for android项目,左等右看,就是看不到完整的android移植代码。兵书上说,兵马未动,粮草先行,最先出现的android编译目标就是base_unittests,细看代码,chromium代码中有着非常多的unittest代码,使用了gtest/gmock单元测试框架。为了达到自动化测试的目的,src/build/android下还有大量的python脚本,可以实现编译/部署/运行的一系列动作的自动化。
- SWIG is not found - The C compiler identification is GNU - The CXX compiler identification is GNU - Check for working C compiler: /home/alex/android/android-toolchain/bin/arm-linux-androideabi-gcc - Check for working C compiler: /home/alex/android/android-toolchain/bin/arm-linux-androideabi-gcc — works - Detecting C compiler ABI info CMake Error: Could not COPY_FILE. OutputFile: ” copyFile: ‘/home/alex/android/android-cmake/samples/hello-cmake/androidbuild/CMakeFiles/CMakeDetermineCompilerABI_C.bin’ Unable to find executable for try_compile: tried "/home/alex/android/android-cmake/samples/hello-cmake/androidbuild/CMakeFiles/CMakeTmp/cmTryCompileExec" and "/home/alex/android/android-cmake/samples/hello-cmake/androidbuild/CMakeFiles/CMakeTmp/Debug/cmTryCompileExec" and "/home/alex/android/android-cmake/samples/hello-cmake/androidbuild/CMakeFiles/CMakeTmp/Development/cmTryCompileExec". - Detecting C compiler ABI info - done CMake Error at /usr/share/cmake-2.8/Modules/CMakeDetermineCompilerABI.cmake:40 (FILE): file STRINGS file "/home/alex/android/android-cmake/samples/hello-cmake/androidbuild/CMakeFiles/CMakeDetermineCompilerABI_C.bin" cannot be read. Call Stack (most recent call first): /usr/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:71 (CMAKE_DETERMINE_COMPILER_ABI) CMakeLists.txt:3 (project) - Check for working CXX compiler: /home/alex/android/android-toolchain/bin/arm-linux-androideabi-g++ - Check for working CXX compiler: /home/alex/android/android-toolchain/bin/arm-linux-androideabi-g++ — works - Detecting CXX compiler ABI info CMake Error: Could not COPY_FILE. OutputFile: ” copyFile: ‘/home/alex/android/android-cmake/samples/hello-cmake/androidbuild/CMakeFiles/CMakeDetermineCompilerABI_CXX.bin’ Unable to find executable for try_compile: tried "/home/alex/android/android-cmake/samples/hello-cmake/androidbuild/CMakeFiles/CMakeTmp/cmTryCompileExec" and "/home/alex/android/android-cmake/samples/hello-cmake/androidbuild/CMakeFiles/CMakeTmp/Debug/cmTryCompileExec" and "/home/alex/android/android-cmake/samples/hello-cmake/androidbuild/CMakeFiles/CMakeTmp/Development/cmTryCompileExec". - Detecting CXX compiler ABI info - done CMake Error at /usr/share/cmake-2.8/Modules/CMakeDetermineCompilerABI.cmake:40 (FILE): file STRINGS file "/home/alex/android/android-cmake/samples/hello-cmake/androidbuild/CMakeFiles/CMakeDetermineCompilerABI_CXX.bin" cannot be read. Call Stack (most recent call first): /usr/share/cmake-2.8/Modules/CMakeTestCXXCompiler.cmake:64 (CMAKE_DETERMINE_COMPILER_ABI) CMakeLists.txt:3 (project) - Configuring incomplete, errors occurred!