问题描述
如何在 Android 原生代码(原生 C/C++,非 Java)上运行单元测试?到目前为止,我只发现 一个 类似的问题,答案是使用 junitJNI,我不想这样做(添加 JNI 调用对于单元测试来说似乎不必要地复杂,并且无论如何都不是对本机代码的真正单元测试).
How do you run unit tests on Android native code (native C/C++, not Java)? So far I've only found one similar question, and the answer says use junit with JNI, which I don't want to do (adding JNI calls seems unnecessarily complicated for unit testing, and is not really a unit test of the native code anyway).
CppUnit(也建议在那里)真的可以在 Android 上运行吗?请注意,我希望测试在设备上本地运行,而不是在主机开发环境上.这个看起来像安卓移植,值得一看吗?
Does CppUnit (also suggested there) really work on Android? Note that I want the tests to run natively on the device, not on the host development environment. This looks like an Android port, is it worth looking at?
像 googletest 这样的官方 Google 测试框架是理想的,但这似乎不适用于 NDK.
An official Google test framework like googletest would be ideal, but that doesn't seem to work with the NDK.
推荐答案
我通过 NDK 使用 googletest 我使用 $(调用 import-module 来引入主 .so,然后在可执行文件中有一个看起来像
I use googletest through NDK I use a $(call import-module to bring in the main .so and then have a single file in the executable that looks like
int main(int argc, char *argv[])
{
#if RUN_GTEST
INIT_GTESTS(&argc,(char**)argv);
RUN_ALL_GTESTS();
#endif
}
然后我用 BUILD_EXECUTABLE 构建它,像这样部署它:
And then I build that with BUILD_EXECUTABLE, deploy it like:
find libs/ -type f -print -exec adb push {} /data/local/tmp ;
然后运行它
adb shell LD_LIBRARY_PATH=/data/local/tmp:/vendor/lib:/system/lib /data/local/tmp/gtest
所以它不测试应用程序生命周期,而是测试所有单元测试.
So it doesn't test the application life cycle but it tests all the unit tests.
如果我需要使用 UI 测试某些东西,我可以做类似的事情,但将现在的主要"功能设为原生函数,并在加载 Activity 时调用它.
If I needed to test something with a UI I could do something similar but make what's now 'main' a native function and invoke it when the activity is loaded.
这篇关于Android NDK 上的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!