how do kick of Android JUnit Test in automated build? - android

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!

Related

Build UiAutomator 2.0 from command line

I want to build, install and run the tests of a UiAutomator project through the command line.
In the previous version I was doing:
android create uitest-project -n <project_name> -p <project_path> -t <target_id>
ant build
to build
and then
adb push <jar_path> /data/local/tmp
to install and finally
adb shell uiautomator runtest <jar_name> -c <main_class>
However, right now I'm stuck in the building part.
The result is
-check-env:
[checkenv] Android SDK Tools Revision 24.1.2
[checkenv] Installed at C:\Android
-build-setup:
[getbuildtools] Using latest Build Tools: 22.0.0
[echo] Resolving Build Target for AndroidExplorerTester...
[getuitarget] Project Target: Android 5.0.1
[getuitarget] API level: 21
[echo] ----------
[echo] Creating output directories if needed...
-pre-compile:
compile:
-post-compile:
-dex:
[dex] input: <test_path>\bin\classes
[dex] Converting compiled files and external libraries into <test_path>\bin\classes.dex...
[dx] no classfiles specified
BUILD FAILED
C:\Android\tools\ant\uibuild.xml:198: null returned: 1
Total time: 1 second
I don't know if there is a better way to do it now since the new version of UiAutomator.
Note: I don't know if it matters but I was using Eclipse before and now I'm using IntelliJ (Android Studio if you prefer lol)
Here is one more way for those, who don't want to move to gradle and wish to stay with ant. Btw, main reason, why old way doesn't work, is moving of uiautomator starting from its 2.0 version from standalone test runner for jars to standard android 'am instrument' test runner for apps. This move has only one 'contra'. Now test projects should be bound to a definite target app (see workaround in the first step). So here is a plan.
First of all, you should have a target project, for which your test is designed. In fact it can be an empty app, which will not be shown at all, neither in apps menu, nor during testing. I've managed to create one in Eclipse, without creating any activity in wizard. To make ant's build.xml run:
android update project --target 0 --path %path_to_empty_app%
For more information about android tool see http: //developer. android. com/ tools/ projects/ projects-cmdline .html
Note: you may want to give necessary permissions to your target app, which will be spread to test app. Now test is not run with shell user permissions.
Second step is creating a test project. As I've mentioned, uiautomator is now integrated in standard android testing scheme. Thus, it uses a standard command for creating test apps:
android create test-project -m %path_to_target_app% -n %test_app_name% -p %path_to_test_app%
A usual app structure will be created in %path_to_test_app%, including ant's build.xml
For more information see http://developer.android.com/tools/testing/testing_otheride.html
Third: copy uiautomator classes jar to test app libs. The jar can be extracted from *.aar archive situated in SDK in \extras\android\m2repository\com\android\support\test\uiautomator\uiautomator-v18\2.1.0 or similar.
Fourth: put your test class *.java to test app src folder. Note the following changes in uiautomator here:
package is renamed from com.android.uiautomator to
android.support.test.uiautomator
UiAutomatorTestCase class is
left for compatibility, but is deprecated; extend your test class
from InstrumentationTestCase, to get UiDevice instance, use
UiDevice.getInstance(getInstrumentation())
Fifth: install and run your test. Simple way is the following:
cd %path_to_test_app%
:: Here 'ant instrument install' builds and installs both target and test apps.
ant instrument install
ant test
or the last line can be modified to
adb shell am instrument -w %target_app_full_name%.tests/android.test.InstrumentationTestRunner
For more information see http://developer.android.com/reference/android/test/InstrumentationTestRunner.html
Well I finally figured it out.
From the command line, in the main folder of the project (where you can find gradlew.bat file) run the following commands
build:
.\gradlew.bat assembleDebug
install on device:
.\gradlew.bat installDebug
(if you want Release version just replace Debug for Release, I didn't try it but the options exist and so I suppose they work)
run:
.\gradlew.bat connectedCheck
If you want to know other options you may have run
.\gradlew.bat tasks
Extra information
To do it programmatically (Java) use Runtime.getRuntime().exec(String command, String[] envp, File dir). For instance,
Runtime.getRuntime().exec(shell_command + " <path_to_test_folder>\gradlew.bat assembleDebug", null, new File(<path_to_test_project>));
Where shell_command depends on the operating system (the command for the command line):
- Windows: cmd /C
- Unix-based: /bin/sh -c

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

Issues building tess-two via osx command line

I am currently attempting to build tess-two for an OCR android application via command line as instructed on https://github.com/rmtheis/tess-two. This is done on my macbook, OSX 10.9.4.
In order to build tess-two, this must be typed into command line:
$ 1git clone git://github.com/rmtheis/tess-two tess
$ cd tess
$ cd tess-two
$ ndk-build
$ android update project --path .
$ ant release
I encountered a problem as soon as I enter:
$ android update project --path .
This is the error displayed:
-bash: android: command not found
I have spend many hours trying to remedy this, and so far nothing fruitful has come of it. I appreciate any suggestions. Thanks.
This means the android command is not in your PATH. Try typing which android. It should give you the location if it is within your PATH.
Usually android can be found in your SDK directory under tools
You've got two options:
run the command with your_sdk_path/tools/android update project --paht
add android to your PATH with
export PATH=$PATH:/your_sdk_path/tools
You must have downloaded Android Development Toolkit bundle first. https://developer.android.com/sdk/index.html?hl=sk#mac-bundle
Then add /sdk/tools to your path.
I hope you know how to configure your path. If you are not sure please have a look at this : http://www.cyberciti.biz/faq/appleosx-bash-unix-change-set-path-environment-variable/

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

Android CTS : downloading of CTS, environment setup, building and execution

If you know about Compatibility Test Suit for Android. Please send us the information regarding source code downloading of CTS, environment setup, building and execution.
You can find all the info about setting up and running CTS here.
If you are in a hurry,here are the commands you use most often:
Running CTS:
First, add the android-sdk-linux/platform-tools to the PATH using export PATH=$PATH:/your-path-goes-here
1- Navigate to the platform-tools directory and use
./android-cts/tools/startcts to start the CTS shell
2- run cts --plan CTS to initiate CTS
3- (OPTIONAL) ls --plan CTS to list all the individual test packages
4a- In case you want to run the entire test suite/plan:
start --plan CTS
4b- In case you want to run the test for a single package:
start --plan CTS -p package-name-goes-here
Download source code of CTS:
It's now available. You can just repo sync it, or git clone git://android.git.kernel.org/platform/cts.git
Env Setup and build
I also feel problems at building the cts. I build successfully once but other times I can't build it. You can see question here, I also record the step of my building.
Execution
Since I didn't got the cts tool, I can only recommend you read this, this is a 0xlad people's article.
To download the CTS compilable code you follow the instructions out here
repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.3_r1
If you work for some of the OEM's Google will provide special cts branches, if not, you'll have to do with the above branch.
To build and run CTS :
cd /path/to/android/root
./cts/development/ide/eclipse/genclasspath.sh > .classpath
chmod u+w .classpath
cd /path/to/android/root
make cts
cts
This answer obviously applies to 4.0.3 version of Android. Things may or may not change from Jelly Bean onwards.
Download cts source code :
$ mkdir <dir_name>
$ cd <dir_name>
$ repo init -u https://android.googlesource.com/platform/manifest -b <tag_name> ( tag_name :- android-cts-8.0_r2, android-cts-7.1_r10)
$ repo sync -d -c –q
Compile complete cts package:
$ cd <dir_name>
$ . build/envsetup.sh
$ make cts -j TARGET_PRODUCT=aosp_arm64
Compile particular cts :
$ cd <dir_name>
$ . build/envsetup.sh
$ cd <testcase_dir_name>
$ mm
CTS setup includes 3 steps
Step 1 : CTS Dowloads
Step 2 : Desktop Machine Setup
Step 3 : Android Device Configuration
Step1 : Compatibility Test Suite Downloads
i)Download and open the CTS packages matching your device’s Android version and all the Application Binary Interfaces (ABIs) your devices support from following link
https://source.android.com/compatibility/cts/downloads.html
Then Unzip it and paste the android-cts to your workspace directory
ii)Download and open the latest version of the CTS Media Files.Unzip it and paste into your workspace directory
Step 2 : Desktop Machine Setup:
CTS currently supports 64-bit Linux and Mac OS host machines. CTS will not work on Windows OS.
i)Before running the CTS, make sure you have recent versions of both Android Debug Bridge (adb) and Android Asset Packaging Tool (AAPT) installed and those tools' location added to the system path of your machine.Ensure adb and aapt are in your system path
ii)set the path using the following command
$ export PATH=$PATH:/home/ramakrishna/Android/Sdk/build-tools/27.0.3
where 27.0.3 is
iii)Install the proper version of the Java Development Kit (JDK). For Android 7.0—
On Ubuntu, use OpenJDK 8.
On Mac OS, use jdk 8u45 or newer.
For details, see the JDK requirements.
please follow below link for complete details of Android Device configuration and Running CTS
Android CTS : downloading of CTS, environment setup, building and execution

Categories

Resources