Running unit tests from native code

621 views
Skip to first unread message

Ulf Magnusson

unread,
Sep 11, 2011, 9:53:58 PM9/11/11
to andro...@googlegroups.com
Hi,

I'm porting a C++ application with unit tests written in the same
language. The unit test framework is embedded as part of the
executable, and the results are written as a textual report to stderr
(though it could of course be directed elsewhere). I need to be able
to automatically launch the application remotely and capture the
results on my development machine for processing.

What would be a nice scheme for the above? I've got everything except
the unit testing part set up. I guess I could use the <android/log.h>
functions and extract the results from the system log, or write the
results to a file which I then transfer to my development machine -
but both of those approaches feel a bit more tedious than they ought
to be. Is there something nicer available?

Thanks,
Ulf

Olivier Guilyardi

unread,
Sep 12, 2011, 9:12:10 AM9/12/11
to andro...@googlegroups.com
Hi,

__android_log_* functions are pretty handy. The tag argument allow you to group
the output. So you could use a specific tag for your unit tests, say "MyUnitTests".

For automating your tests, you can write a small script like:

$ adb install -r your.apk
$ adb shell "am start -a android.intent.action.MAIN -n com.your.app/.SomeActivity"
$ adb logcat MyUnitTests:D '*':S | tee testlog.txt

The logcat command supports a variety of options, see:
http://developer.android.com/guide/developing/tools/adb.html#logcat

Hope that helps

--
Olivier

Ulf Magnusson

unread,
Sep 12, 2011, 3:16:04 PM9/12/11
to andro...@googlegroups.com

That's very helpful - thanks! I never realized the logging system was
that flexible. I guess a simple scheme would be to clear the log (to
avoid picking up results from previous runs), run the application
remotely, and then examine the output of logcat, filtered on a
unit-test-specific tag.

/Ulf

Smallrui

unread,
Sep 12, 2011, 9:42:40 PM9/12/11
to android-ndk
how about crack the root permission? try to use application named
"z4root", you can directly run ./app from shell commands.

On 9月13日, 上午3时16分, Ulf Magnusson <ulfali...@gmail.com> wrote:

Ken Turkowski

unread,
Nov 7, 2011, 1:44:12 PM11/7/11
to andro...@googlegroups.com
I do the following.
Note that adb doesn't return the status, so I have to parse the output to see if anything failed.
Note how the non-android (Mac. windows, linux) is much simplified.

#!/bin/bash
...
# Test
if $doTest
then
Progress "Testing"
case $testTarget in
images) flags=--write_images ;;
esac
$verbose && flags="$flags --verbose"
flags="$flags $flagArgs"
num_tests=0
num_failed=0
case ${machDir} in
android) cd "${F_HOME}/bin/${machDir}/release"
esac
for test in $testList
do
adb push ${test} /data/local/tmp || ExitOnError "Pushing ${test} to device"
done
for test in $testList
do
num_tests=`expr $num_tests + 1`
adb shell "export LD_LIBRARY_PATH=.; cd /data/local/tmp; ./${test} --gtest_color=yes --out_dir=/sdcard/gtest ${flags}" | tee /tmp/TMP$$
grep -q '\[ FAILED \]' /tmp/TMP$$ && num_failed=`expr $num_failed + 1`
done
rm -f /tmp/TMP$$
;;
*) cd "${F_HOME}/bin/${machDir}/${buildType}"
for test in $testList
do
num_tests=`expr $num_tests + 1`
./$test $flags || num_failed=`expr $num_failed + 1`
done
;;
esac
test $num_failed = 0 && echo -e "\n${GREEN_COLOR}ALL $num_tests TESTS PASSED.${END_COLOR}\n" \
|| { echo -e "\n${RED_COLOR}$num_failed TESTS FAILED, ${GREEN_COLOR}`expr $num_tests - $num_failed` TESTS PASSED.${END_COLOR}\n"; exit 1; }
fi

> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>

Reply all
Reply to author
Forward
0 new messages