Android - generate coverage with emma (without ant) - android

After running ant clean emma debug and installing the generated apk file on my emulator, I am running the following command to run my unit tests:
adb shell am instrument -w -e package org.company.projectname.test.unit -e coverage true \
-e coverageFile ProjectNameTest/bin/coverage.xml org.company.projectname.tests/com.neenbedankt.android.test.InstrumentationTestRunner'
The InstrumentationTestRunner I am using is a subclass of android.test.InstrumentationTestRunner.
When I run this command, all the tests pass, but then no coverage is generated and I get the error:
Error: Failed to generate emma coverage. Is emma jar on classpath?
I am not looking for a solution that uses ant test, unless that solution can address the reason I' not using it, which is that I wanted to be able to specify a package within org.company.projectname.test to test (in this case the unit package).

This worked for me:
ant instrument
ant emma installi test
Edit:
If you do not want to use Ant you have to build the instrumented apk manually, check $ANDROID_HOME/tools/ant/build.xml for more info and make sure emma.jar is under libs of your test project.

Try :
ant all clean emma debug install test
and also please check http://code.google.com/p/android/issues/detail?id=21640 , it seems like this issue is fixed latest commit

Related

Android R.string ids mix in multi-module tests

My app is implemented as a multi-module project (with dynamics module), where there is a shared module with espresso tests. These espresso tests give me different outcomes depending if I run them from Android Studio or from the command line.
The issue I find when I run the tests from the command line is that it tries to assert against the wrong string resources.
The actual tests code is asserting this, where R.string.home_activity_title value is "Hello":
assertDisplayed(R.string.home_activity_title)
However when it is run from the command line, the assertion fails with this output
No views in hierarchy found matching: with string from resource id: <2131886676>[send_message_hint] value: Type a message…
Notice how the tests from the command line is trying to assert against a different string that the one is defined on the test.
I tried to understand the difference between running the tests from AndroidStudio and running them from the command line but I am clearly missing something here. This is how I run the tests from the command line
apk="path/to/apk"
testApk="path/to/test/apk"
testRunner="my.custom.test.runner"
# Generate universal APK
echo "Generating universal APK"
sh build_develop.sh
# Install universal APK on emulator
echo "Installing universal APK"
adb uninstall <appPackage>
adb install -t $apk
echo "Building test APK"
./gradlew :testModule:assembleDebugAndroidTest
echo "Installing Test APK"
adb uninstall <testApkPackage>
adb install -t $testApk
echo "Running Acceptance Tests"
# Run all acceptance tests
adb shell am instrument -w $testRunner
Try specifying the module for which you want the string to be obtained.
E.g. com.example.module.R.string.home_activity_title instead of R.string.home_activity_title

Which gradle task builds AndroidTest .*apk and doesn't do anything additional

Let's say I have a build variant named debugAutomation.
I wan't to build this variant into .*apk file using gradle command. I do:
./gradlew assembleDebugAutomation
This provides me with application .*apk. But to perform automation tests in espresso I also need test .*apk. So I do again:
./gradlew assembleDebugAutomationAndroidTest
And .*apk with -androidTest is in my outputs/apk/ folder of my project.
Where is the trick and problem?
a) while performing assembleDebugAutomation I have only application .*apk built.
b) while performing assembleDebugAutomationAndroidTest then during build process THIS APK BEING INSTALLED ON ALL CURRENTLY VISIBLE DEVICES IN ADB.
My question is: How can I build androidTest .*apk with gradle command so it ONLY BUILDS APK and DOESN'T INSTALL IT on currently visible devices in ADB during build process?
To just assemble into an APK:
./gradlew assembleDebug
That will build however, but won't do any automation tests, nor would it install to devices.
./gradlew assembleDebug
Builds the debug APK.
./gradlew assembleDebugAndroidTest
Builds the test APK
Or if you have a flavour in your gradle file it would be:
./gradlew assemble<FlavourName>DebugAndroidTest

Build Robotium Test Project from command line

I'm exploring the Android build system and want to move everything to Jenkins server. I've managed to build the project and run instrumentation tests (white box testing) on Jenkins. And now stuck at the integration testing..
I followed the tutorial to create a Android Test Project on Eclipse. And the tests run successfully. But I need to be able to run it from command line so that I can trigger the tests on Jenkins.
If I run the project on Eclipse first, and later I will be able to use the command:
adb shell am instrument -w com.example.uitests/Android.test.InstrumentationTestRunner
However, if just want to start everything from command line, I don't know how to build the project...It looks like there's no build file generated by Eclipse..
Do I need to use ant or gradle? if so, what's the right way of doing so?
Edit:
When calling to list all instrumentation info
adb shell pm list instrumentation
I'm able to find my tests package:
instrumentation:com.example.uitests/android.test.InstrumentationTestRunner
(target=com.exmaple.android)
But when running the first command, I get
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for:
ComponentInfo{com.example.uitests/Android.test.InstrumentationTestRunnerpm}
INSTRUMENTATION_STATUS_CODE: -1 android.util.AndroidException:
INSTRUMENTATION_FAILED:
com.android.uitests/Android.test.InstrumentationTestRunnerpm
at com.android.commands.am.Am.runInstrument(Am.java:802) at
com.android.commands.am.Am.onRun(Am.java:242) at
com.android.internal.os.BaseCommand.run(BaseCommand.java:47) at
com.android.commands.am.Am.main(Am.java:75) at
com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235) at
dalvik.system.NativeStart.main(Native Method)
The solution I have now is to add ant build.xml to this test project, follow the tutorial
android update project --name --target
--path
After updating the project, I'm able to build and install the apk to emulators
ant clean debug install

Error: Failed to generate emma coverage. Is emma jar on classpath? while generating code coverage report

I am getting
Error: Failed to generate emma coverage. Is emma jar on classpath?
while generation coverage report for android test project i am using adb shell am instrument -w -e coverage true com.android.foo/android.test.InstrumentationTestRunner command for run the test and get the coverage any idea what would be the fix for this.
Its because you apk and instrumented code is not EMMA-instrumented.

how do kick of Android JUnit Test in automated build?

An Android JUnit test uses the Android Context object so it needs to get this from a device or emulator. Does anyone know how one could automate this? Would there need to be an emulator on the build machine? Any sample script would be helpful, and I would like to know how folks are doing this. Thanks
Yes, you can install and execute your unit test project from the command prompt using ant.
First update your already existing project
$ android update project --path $PWD --name [YOUR PROJECT] --target android-17 --subprojects
Then create a test project
$ android create test-project -m ../ -n [YOUR TEST PROJECT] -p tests
Then you can build and execute the code by issuing
$ ant clean instrument
$ ant debug install test
Cheers!

Categories

Resources