23

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).

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?

An official Google test framework like googletest would be ideal, but that doesn't seem to work with the NDK.

Indivara
  • 735
  • 1
  • 7
  • 19

1 Answers1

18

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
}

And then I build that with BUILD_EXECUTABLE, deploy it like:

find libs/ -type f -print -exec adb push {} /data/local/tmp \;

and run it like

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.

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.

Doug-W
  • 1,318
  • 12
  • 14