Cannot run androidTest in certain module - android

I am having a problem running an androidTest. It cannot locate my tests. The weird thing is, that it's only the case for a certain module. Other modules which I put the test into and configure their build.gradle files eqully are working. Here are some informations. Any help is very appreciated!
The output is as follows:
Running tests
$ adb shell am instrument -w -r -e debug false -e class my.package.MainActivityTest my.package.module.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Test running failed: Unable to find instrumentation info for: ComponentInfo{my.package.test/android.support.test.runner.AndroidJUnitRunner}
Empty test suite.
The test is located under
myModule/src/androidTest/java/my/package/MainActivityTest.java
and looks as follows:
#LargeTest
#RunWith(AndroidJUnit4.class)
public class MainActivityTest {
#Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
#Test
public void thisTestIsntExecuted() {
assertEquals(1, 2);
}
}
The modules build.gradle file has the following settings:
android {
defaultConfig {
(...)
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
(...)
testImplementation "junit:junit:4.12"
androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.1"
androidTestImplementation "com.android.support.test:runner:1.0.1"
}
Instrumentation runner on device:
instrumentation:my.package.test/android.support.test.runner.AndroidJUnitRunner (target=my.package.module)
//edit: I had a closer look at the output from the run command and the instrumentation runner on the device. I forgot to add .module on two places.

I found out to solve the problem. As stated in the comment above I was able to start my tests manually with the command:
adb shell am instrument -w -r -e debug false -e class my.package.module.MainActivityTest my.package.test/android.support.test.runner.AndroidJUnitRunner
The run configuration which Android Studio created for me executed the command:
adb shell am instrument -w -r -e debug false -e class my.package.module.MainActivityTest my.package.module.test/android.support.test.runner.AndroidJUnitRunner
So as you can see a path was wrong, the module part was too much.
This path is the testApplicationId and can be changed via gradle, see here. When it's not set, the value is automatically created by taking the applicationId and add a .test to it. This resulted in a wrong path in my case, maybe because I am generating the applicationId based on the build variant.
Don't forget to sync and clean.

Related

how to execute individual Junit test in android using gradle

My project contains multiple test classes and each class contains some test methods.
I can run all the test using command line
gradlew testDebugUnitTest
Is there any gradlew command to run specific testMethod from a class?
Is there any gradlew command to run all test mmethods from a specific class?
Here is a class Foo2 and bar3 is the particular method you would like to test. This was taken from the Google developer Junit unit test command line documentation
$ adb shell am instrument -w \
> -e class com.android.demo.app.tests.Foo2#bar3 \
> com.android.demo.app.tests/android.support.test.runner.AndroidJUnitRunner

Instrumented tests not found with custom instrumentation - conditional instrumentation?

I have a working Android app with a manifest containing this <instrumentation> node
<instrumentation
android:name=".MyInstrumentation"
android:targetPackage="my.package"/>
I have instrumented tests also, but when I run those I get this
$ adb shell am instrument -w -r-e debug false my.package.test/my.package.MyInstrumentation
Client not ready yet..
Started running tests
Test running failed: Unable to find instrumentation info for: ComponentInfo{my.package.test/my.package.MyInstrumentation}android.util.AndroidException: INSTRUMENTATION_FAILED: my.package.test/my.package.MyInstrumentation
. onError: commandError=true message=INSTRUMENTATION_FAILED: my.package.test/my.package.Instrumentation
and tests are not run.
If I remove the instrumentation declaration from the manifest it works fine, when I run the tests like this I get this
$ adb shell am instrument -w -r -e debug false my.package.test/androidx.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Tests ran to completion.
androidx.test.runner.AndroidJUnitRunner is the instrumentation that gets executed.
This happens when I run the tests from Android Studio and also when I execute ./gradlew connectedAndroidTest.
How can I tell gradle or Android Studio that I want to run the tests with the AndroidJUnitRunner instrumentation? With still allowing my own.
add this line in your build.gradle file, and try
android {
...
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
...
}
...
}

How to build and create APK for a library module using command line?

In my Android studio project I have a librarymodule (Name:Network-sdk) and have some test cases for the library module. I am creating an automation script for running test cases from the bash for the library module.
I done the following script but its gives test case not found error.
#To clean the project
./gradlew clean
#Build the project
./gradlew packageDebug
#Install the APK
adb install ./IrisReferenceClient/app/build/outputs/apk/app-debug.apk
#Test case which in library module
adb shell am instrument -w -r -e debug false -e class 'package.ConnectivityTestSenderTest' package/android.support.test.runner.AndroidJUnitRunner
Log information:
android.util.AndroidException: INSTRUMENTATION_FAILED: package.test/android.support.test.runner.AndroidJUnitRunnerINSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{package.test/android.support.test.runner.AndroidJUnitRunner}
INSTRUMENTATION_STATUS_CODE: -1
at com.android.commands.am.Am.runInstrument(Am.java:890)
at com.android.commands.am.Am.onRun(Am.java:400)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:51)
at com.android.commands.am.Am.main(Am.java:121)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:262)
I want to know how to build and create APK for file library module and run my test case which in library module.

Test FAILED when running Robotium tests on circle-ci

Here is my circleci config file:
# Build configuration file for Circle CI
# needs to be named `circle.yml` and should be in the top level dir of the repo
general:
artifacts:
- "~/build_output.zip" # Save APK's, Lint Results, and Test Results
machine:
environment:
ANDROID_HOME: /usr/local/android-sdk-linux
java:
version: oraclejdk8
dependencies:
cache_directories:
- ~/.android
- ~/android
override:
- (echo "Downloading Android SDK v24...")
- (source environmentSetup.sh && getAndroidSDK)
test:
override:
# start the emulator
- emulator -avd circleci-android22 -no-audio -no-window:
background: true
parallel: true
# wait for it to have booted
- circle-android wait-for-boot
# unlock the emulator screen
- sleep 30
- adb shell input keyevent 82
# run tests against the emulator.
- ./gradlew connectedAndroidTest
# run jUnit tests
- ./gradlew test
# copy the build outputs to artifacts
- cp -r app/build/outputs $CIRCLE_ARTIFACTS
# copy the test results to the test results directory.
- cp -r app/build/outputs/androidTest-results/* $CIRCLE_TEST_REPORTS
Here is a test method:
#Test
public void test_ListRepos_ClickOnOneRepo_DisplayDetailWithOnlyThisRepo() {
Given:
{
final String lsOneRepoJSONData = mLocalifyClient.localify().loadRawFile(fr.guddy.androidstarter.test.R.raw.repos_octocat);
final MockResponse loMockResponseWithOneRepo = new MockResponse().setResponseCode(200);
loMockResponseWithOneRepo.setBody(lsOneRepoJSONData);
mMockWebServer.enqueue(loMockResponseWithOneRepo);
try {
mMockWebServer.start(4000);
} catch (#NonNull final Exception loException) {
loException.printStackTrace();
}
mActivity = mActivityTestRule.launchActivity(null);
}
When:
{
mSolo.clickOnText("git-consortium");
}
Then:
{
mSolo.assertCurrentActivity("should be on ActivityRepoDetail", ActivityRepoDetail.class);
final boolean lbFoundTheRepo = mSolo.waitForText("This repo is for demonstration purposes only.", 1, 5000L, true);
assertThat(lbFoundTheRepo).isTrue();
}
}
And I get test failures in the following builds: https://circleci.com/gh/RoRoche/AndroidStarter/tree/master
Running the same commands locally, I get the following results:
junit.framework.AssertionFailedError:
[
GIVEN A single GitHub repo from the API
WHEN Click on its name and on the back button
THEN It should display the list
]
[
Message: Text string: 'git-consortium' is not found!
]
at junit.framework.Assert.fail(Assert.java:50)
at com.robotium.solo.Clicker.clickOnText(Clicker.java:451)
at com.robotium.solo.Solo.clickOnText(Solo.java:1502)
at fr.guddy.androidstarter.tests.ui.TestActivityRepoList.test_DetailRepos_ClickOnBack_DisplayListRepos(TestActivityRepoList.java:156)
Any idea of how can I configure my circleci build to make it pass?
Thanks in advance.
Looking right at the top of one of your failed builds you'll see the following message: "Your build has exceeded the memory limit of 4G on 1 container.".
Your builds are running out of memory. I'd try and reduce the amount of memory used if possible (https://circleci.com/docs/oom/). If that's a blocker (which sometimes happens with Android), I'd contact CircleCI support to see if they can help you out.

Running android unit tests from the command line?

I'm trying to run unit tests on the android platform in accordance with tutorial. Say, for example, I want to run tests for Email application. I open /apps/Email/tests/AndroidManifest.xml file, look for the <manifest> element, and look at the package attribute, which is com.android.email.tests, and in the <instrumentation> element I look at the android:name attribute, which is android.test.InstrumentationTestRunner. Now I open the console, and run
$ . build/envsetup.sh
$ lunch 1
$ adb shell am instrument -w com.android.email.tests/android.test.InstrumentationTestRunner
But that fails:
INSTRUMENTATION_STATUS: id=ActivityManagerService
android.util.AndroidException: INSTRUMENTATION_FAILED: com.android.email.tests/android.test.InstrumentationTestRunner
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.android.email.tests/android.test.InstrumentationTestRunner}
So.. What am I doing wrong?
Please run
python development/testrunner/runtest.py email
and then you will see it works :).
Basically you do not have com.android.email.tests package installed.
You can see what is available for instrumentation
pm list instrumentation
And you should see
instrumentation:com.android.email.tests/android.test.InstrumentationTestRunner
(target=com.android.email)
And when doing
pm list packages
package:com.android.email
package:com.android.email.tests
You may need to setup a test project with the android create test-project command first. Check this page on the Android Dev site: Testing In Other IDE's for more info. I've used this method to enable command line testing with ant.
What I actually forgot to do was building and installing that test packages onto my device/emulator. Discovered that after doing:
$ adb shell
# cd data/packages
# ls
And no com.android.email.tests package there.
My issue was this tag:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="Tests for app.under.test.package"
android:targetPackage="app.under.test.package" />
Firstly I had the android:name attribute wrong then the target package wrong (above is the correct solution)
Test Package Not Installed on the Emulator
I had the exact same issue and realized that the test package hadn't been installed on the emulator, which is what #marekdef is saying.
Instead of using the command found in the generated test file, I used the following:
ant debug install test
*I had my test project in <project_home>/tests so the following command is what I ended up using from my project home directory:
(cd tests && ant debug install test;)
Hope that helps.
I received the "Unable to find instrumentation info" error under this condition: I defined my test project with a src package name that was the same as that of the project-under-test. For example, the source for both projects was in package com.mycompany.android. This parallel-src-package setup worked fine in Eclipse, but on the emulator it very much appeared that the test apk was overwriting the app apk.
Fix: I changed the src packge of the test project to test.mycompany.android.
Note that, in either case, the Android manifest for the test project defines:
< manifest package="pkg-of-project-under-test" ...>
and
< instrumentation android:targetPackage="pkg-of-project-under-test" ...>
For gradle user you can use the following tasks:
$ gradle :project:installDebug :project:installDebugAndroidTest
I have this run_all_test.sh script to run all unit and instrumented test:
#!/bin/bash
./gradlew --no-daemon clean test connectedCheck --stacktrace;
if [ $? -eq 0 ]
then
echo "tests are successful"
else
echo "tests FAILED"
exit 1
fi
Explanation:
test -> execute all unit test
connectedCheck -> execute all instrumented test
You can start from here to customize it based on your needs following the documentation here: https://developer.android.com/studio/test/command-line
[Android test types]
To run all tests from Command Line using gradlew[About]
JUnit test (UnitTest suffix)
./gradlew test
./gradlew :<moduleName>:test<variantName>UnitTest
Instrument test(AndroidTest suffix)
./gradlew connectedAndroidTest
./gradlew :<moduleName>:connected<variantName>AndroidTest
If you want just to build tests and don't run them use assemble
//Unit
./gradlew :<moduleName>:assemble<variantName>UnitTest
//functional
./gradlew :<moduleName>:assemble<variantName>AndroidTest

Categories

Resources