Build Robotium Test Project from command line - android

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

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

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!

Android - generate coverage with emma (without ant)

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

Command not found in phonegap

I followed a tutorial to use command to build, run, ... in phonegap at http://docs.phonegap.com but I can't use those commands, except the Create command.
The Create command is:
create ./test1 com.test1 test1
the original command is: $ /path/to/cordova-android/bin/create.bat /path/to/my_new_cordova_project com.example.cordova_project_name CordovaProjectName
After created project successfully, I run the command:
cd test1/cordova/build
the original command is: $ /path/to/my_new_cordova_project/cordova/build.bat
but it throws an error:
'build' is not recognized as an internal or external command,
operable program or batch file.
Then I go to my new project folder, go in folder: cordova to look for build.bat, but I see nothing.
Can you show me how to fix this stuff, i want to use command to build my phonegap project. Thanks!
Additional info:
Using phonegap 2.6, android platform.
Windows 7 64bit.
I'm able to run command: ant, java, javac, and some in the android platform tools,...

android.util.AndroidException: INSTRUMENTATION_FAILED:

I have a simple android app and I am testing it using my phone. So, there are two ways to do that :
Using eclipse
Using CLI
Problem:
When I run unit test case using Eclipse, it installs app on my phone at runtime and run junit test and after that if I use command on CLI:
adb -d shell am instrument -w com.abc.xyz.test/android.test.InstrumentationTestRunner, it runs fine.
However, if I directly run the above command in CLI without first running the unit test cases in Eclipse, I am getting error:
android.util.AndroidException: INSTRUMENTATION_FAILED: com.abc.xyz.test/android.test.InstrumentationTestRunner
at com.android.commands.am.Am.runInstrument(Am.java:586)
at com.android.commands.am.Am.run(Am.java:117)
at com.android.commands.am.Am.main(Am.java:80)
at com.android.internal.os.RuntimeInit.finishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:263)
at dalvik.system.NativeStart.main(Native Method)
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation target package: com.abc.xyz
INSTRUMENTATION_STATUS_CODE: -1
AndroidMAnifest.xml contains:
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.abc.xyz"
inside instrumentation tag
Could anyone please help me
I suppose that you will have solved it since january, but I work with command-line tools, found similar problem (error message is different) and solved it like I explain in following steps. I do the whole process from creating a dummy project with its empty test until a successful test run. I hope it can be useful for someone:
First step, create the project:
android create project
--name MyExample
--target "Google Inc.:Google APIs:17"
--path MyExample
--package com.example
--activity MyExampleActivity
Second step, create test project:
android create test-project
--path MyExampleTest
--name MyExampleTest
--main ../MyExample
Third step, access to your project directory, build it and check that the process ends successfully:
cd MyExample && ant debug
Fourth step, install it to the emulator:
adb -s emulator-5554 install -r bin/MyExample-debug.apk
Fifth step, access to your test project directory and try to run the tests:
cd ../MyExampleTest &&
adb shell am instrument -w com.example.tests/android.test.InstrumentationTestRunner
That yields:
INSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.example.tests/android.test.InstrumentationTestRunner}
INSTRUMENTATION_STATUS_CODE: -1
android.util.AndroidException: INSTRUMENTATION_FAILED: com.example.tests/android.test.InstrumentationTestRunner
at com.android.commands.am.Am.runInstrument(Am.java:676)
at com.android.commands.am.Am.run(Am.java:119)
at com.android.commands.am.Am.main(Am.java:82)
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)
Sixth step, list your instrumentation clases and ensure that your current project is missing:
adb shell pm list instrumentation
That in my machine yields:
instrumentation:com.android.emulator.connectivity.test/android.test.InstrumentationTestRunner (target=com.android.emulator.connectivity.test)
instrumentation:com.android.emulator.gps.test/android.test.InstrumentationTestRunner (target=com.android.emulator.gps.test)
instrumentation:com.android.example.spinner.tests/android.test.InstrumentationTestRunner (target=com.android.example.spinner)
instrumentation:com.android.smoketest.tests/com.android.smoketest.SmokeTestRunner (target=com.android.smoketest)
instrumentation:com.android.smoketest.tests/android.test.InstrumentationTestRunner (target=com.android.smoketest)
instrumentation:com.example.android.apis/.app.LocalSampleInstrumentation (target=com.example.android.apis)
As you can see, the instrumentation for com.example.tests doesn't exist, so we will have to create it.
Seventh step, build you test project and check that it did successfully:
ant debug
Eigth step, install it to the emulator:
adb -s emulator-5554 install -r bin/MyExampleTest-debug.apk
Ninth step, list your instrumentation classes and look for the one of your project:
adb shell pm list instrumentation
That yields:
instrumentation:com.android.emulator.connectivity.test/android.test.InstrumentationTestRunner (target=com.android.emulator.connectivity.test)
instrumentation:com.android.emulator.gps.test/android.test.InstrumentationTestRunner (target=com.android.emulator.gps.test)
instrumentation:com.android.example.spinner.tests/android.test.InstrumentationTestRunner (target=com.android.example.spinner)
instrumentation:com.android.smoketest.tests/com.android.smoketest.SmokeTestRunner (target=com.android.smoketest)
instrumentation:com.android.smoketest.tests/android.test.InstrumentationTestRunner (target=com.android.smoketest)
instrumentation:com.example.tests/android.test.InstrumentationTestRunner (target=com.example)
instrumentation:com.example.android.apis/.app.LocalSampleInstrumentation (target=com.example.android.apis)
Look at the second to last, instrumentation:com.example.tests, it's which we wanted.
Tenth step, run your tests:
adb shell am instrument -w com.example.tests/android.test.InstrumentationTestRunner
That yields:
Test results for InstrumentationTestRunner=
Time: 0.0
OK (0 tests)
That is all. Now implement your tests, compile and install as usual. Additionally you can remove them like:
adb shell pm uninstall com.example.tests
But you will need to create instrumentation classes again to avoid the same error.
A more precise explanation/approach is the following:
Make sure you do
adb install -r bin/<>-debug.apk
from both from tests and the app directory.
After that ant test should work from the tests directory. (My guess is that there was a missing dependency to the app from test package - which was causing the failure).
Other than the above little hack, rest of the procedure I followed came from the android testing introduction on http://developer.android.com/.
Make sure you uninstall the previous app and reinstall or kick off the test only after uninstalling the previous app

Categories

Resources