简体   繁体   English

如何使用googletest测试在android上调用java的C ++代码?

[英]How to use googletest for testing C++ code that calls into java on android?

I am working on a rather complicated C++ library that I plan to test properly using googletest for Android NDK. 我正在研究一个相当复杂的C ++库,我计划使用googletest for Android NDK正确测试。

So far I follow the google test example and structure the project like this: 到目前为止,我按照谷歌测试示例,并像这样构建项目:

Android.mk: Android.mk:

  LOCAL_PATH := $(call my-dir)

  include $(CLEAR_VARS)
  LOCAL_MODULE := foo
  LOCAL_SRC_FILES := foo.cpp
  include $(BUILD_SHARED_LIBRARY)

  include $(CLEAR_VARS)
  LOCAL_MODULE := foo_unittest
  LOCAL_SRC_FILES := foo_unittest.cpp
  LOCAL_SHARED_LIBRARIES := foo
  LOCAL_STATIC_LIBRARIES := googletest_main
  include $(BUILD_EXECUTABLE)

  $(call import-module,third_party/googletest)

I build and call the test using a script file: 我使用脚本文件构建并调用测试:

adb push libs/armeabi/libfoo.so //data/local/tmp/
adb push libs/armeabi/libgnustl_shared.so //data/local/tmp/
adb push libs/armeabi/foo_unittest //data/local/tmp/
adb shell chmod 775 //data/local/tmp/foo_unittest
adb shell "LD_LIBRARY_PATH=//data/local/tmp //data/local/tmp/foo_unittest"

This works fine with any pure C++ code that doesn't have many references but a lot of my code actually relies on java/jni calls. 这适用于任何没有很多引用的纯C ++代码,但我的很多代码实际上依赖于java / jni调用。 How can I run googletest with a complete apk file that comes not just with C++ code but also java and resources? 如何使用完整的apk文件运行googletest,该文件不仅包含C ++代码,还包含java和资源?

Inside your test program, you will have a main() function which looks something like this: 在你的测试程序中,你将有一个main()函数,它看起来像这样:

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

This allows you to invoke the test program like any other: just type the name of the executable in a shell. 这允许您像调用任何其他程序一样调用测试程序:只需在shell中键入可执行文件的名称即可。

More info on: 更多信息:

https://github.com/google/googletest https://github.com/google/googletest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM