Android Unit Test Cache - android

I'm facing an issue whereby whenever I use adb command to run my android test case but it keeps a cache of the last test class. This is even though I have changed my code and rebuild.
I'm running below command
./adb shell am instrument -w -e class com.xxx.yyy.tests/SignInActivityTest com.xxx.yyy.test/android.test.InstrumentationTestRunner
I add some test method, it is still caching and run the old test case.

If you are using command line; Make sure to re-build the test application and not only the application under test and that both are deployed to the target before running tests again.

Related

Android instrumentation tests not running due to Ambiguous arguments exception

I am unable to run instrumentation tests in my Android project in two cases:
When running individual tests (i.e. a single #Test)
When running all tests in a class
I am able to run instrumentation tests by right clicking on the folder the test is in and selecting the Run 'Tests in PACKAGE_NAME' option
Here's an example of running an individual test:
Testing started at 6:35 PM ...
12/30 18:35:05: Launching getId()
$ adb push /Users/zach/Developer/Code/testapp/app/build/outputs/apk/develop/debug/app-develop-debug.apk /data/local/tmp/com.zao.testapp.develop
$ adb shell pm install -t -r "/data/local/tmp/com.zao.testapp.develop"
Success
APK installed in 4 s 42 ms
No apk changes detected since last installation, skipping installation of /Users/zach/Developer/Code/testapp/app/build/outputs/apk/androidTest/develop/debug/app-develop-debug-androidTest.apk
Running tests
$ adb shell am instrument -w -r -e debug false -e class 'com.zao.testapp.models.entities.impl.EntityParseTest#getId' com.zao.testapp.develop.test/com.zao.testapp.TestRunner
Client not ready yet..
Started running tests
Test running failed: Fatal exception when running tests
java.lang.IllegalArgumentException: Ambiguous arguments: cannot provide both test package and test class(es) to run
at android.support.test.internal.runner.TestRequestBuilder.validate(TestRequestBuilder.java:773)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:742)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:354)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:260)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2075)
Empty test suite.
And when I run via the right click option to run all tests in the package, here is what I see:
Testing started at 6:40 PM ...
12/30 18:40:38: Launching Tests in 'com.zao.testapp.models.entities.impl'
$ adb push /Users/zach/Developer/Code/testapp/app/build/outputs/apk/develop/debug/app-develop-debug.apk /data/local/tmp/com.zao.testapp.develop
$ adb shell pm install -t -r "/data/local/tmp/com.zao.testapp.develop"
Success
APK installed in 4 s 471 ms
No apk changes detected since last installation, skipping installation of /Users/zach/Developer/Code/testapp/app/build/outputs/apk/androidTest/develop/debug/app-develop-debug-androidTest.apk
Running tests
$ adb shell am instrument -w -r -e package com.zao.testapp.models.entities.impl -e debug false com.zao.testapp.develop.test/com.zao.testapp.TestRunner
Client not ready yet..
Started running tests
Tests ran to completion.
The only "gotcha" here might be that I am using a custom test runner (TestRunner.java that is referenced above). I'm not sure that matters too much though. I'll try removing this (some of the tests need the custom runner, but I can disable those for now...)
Any thoughts? Thanks!
I debugged this issue and figured out the root cause. I was using a custom runner that was ignoring a package from being tested. I implemented a workaround described in https://github.com/mockito/mockito/issues/922 to get my mockito stuff working with the custom runner, and this resulted in the error.
Putting arguments.putString("notPackage", "net.bytebuddy"); in the runner's onCreate resulted in TestRequestBuilder.java throwing an exception in the validate method, which does this:
if ((!mIncludedPackages.isEmpty() || !mExcludedPackages.isEmpty()) && !classNames.isEmpty()) {
throw new IllegalArgumentException(AMBIGUOUS_ARGUMENTS_MSG);
}
Where mExcludedPackages has a size of 1 (net.bytebuddy), while classNames is also not empty and contains the class I am running tests in.
Therefore to fix this you can either do one of two things:
Remove the arguments.putString("notPackage", "net.bytebuddy") workaround (which makes mockito not work in some cases for my tests).
Refactor your tests to not need the custom runner, thereby doing step 1 as well.
Not be able to run tests for specific tests/files.
I ended up doing step 2, FWIW.

gradle connectedAndroidTest returns "No test found", however adb shell am instrument can find the tests

We have a library project and multiple applications depends on it. And the unit tests are in the library project.
We're able to run the tests from dependent projects in Android Studio, but
./gradlew :[DependentProject]:connectedAndroidTest
always returns "No test found, nothing to do”.
Through observation, I found in Android Studio, seems that it only executes gradle tasks:
:[DependentProject]:assembleDebug, :[DependentProject]assembleDebugTest
then uses adb to install the target and test apk, and adb shell am instruments to run the tests.
Since connectedAndroidTest depends on these two tasks, I install the target and test apks it produced, and manually invoked instrument command, tests got started.
adb shell am instrument -w com.package.test/android.test.InstrumentationTestRunner
Then the question comes, where does connectedAndroidTest look for tests, and why it cannot find the tests while adb instrument can? How to resolve this issue?
I have the same issue and I solve it by add a method starting with "test"
#Test
public void testWTF() throws Exception {
assertTrue(true);
}
And all other method with #Test annotation work too !
Amazing no ? I found the answer here : No tests found with test runner 'JUnit 4'

cannot run android unit test if I set the shareduserId as "android.uid.system"

I got a problem when I was running a test that the target Package's sharedUserId is "android.uid.system"
when I type this in shell
adb shell am instrument -w -e class com.lewa.security2.holder.ClearanceHolderTest com.lewa.dunit.test/android.test.InstrumentationTestRunner
it never return a result.
but when I remove the "android.uid.system", it just works
can any one help me ?
a unit test in android need to run after you kill the application.
but applicaions with the shareduid android.uid.system cannot be killed in this way.
so,you need to force close the application before run unit tests.

Is it possible to enter debug mode for android when running junit test?

Usually i run a junit test using adb shell am instrument -w com.android.contacts.tests/android.test.InstrumentationTestRunner. And it actually works, it will run all my tests.
But when i make a breakpoints and wish to enter debug mode when running junit, it failed. The way i make breakpoints works when i debug normal android app.
So i searched web, and try something like adb shell am instrument -e debug true -e class com.android.contacts.AndroidUtilsTest -w com.android.contacts.tests/android.test.InstrumentationTestRunner, but still without luck. How do yours solve this problem? I not just want to debug the junit class, but also the code in normal project.
I guess you are trying to debug your tests using Eclipse. If this is the case you can just select you test project, right click, Debug As... -> Android JUNit test and the execution will stop at the breakpoints you set in your tests or in your code.
I found the answer just now. It will block when i start instrument since it is waiting for me to set a breakpoint. So After i make a breakpoint, then the junit start to run. So the sequence of debug a android junit project is :
start instrument with debug set true.
make a break point using command line or eclipse.
the instrument will start to run automatically.
To Debug Android JUnit test don't forget to put debuggable=true in the testable project.

Perl Script to run Junit test cases and unistall apk

What I want my Perl script to do is..
Run the specific Junit test cases from windows command line.
Uninstall the Apk file from android device or emulator.
I am having difficulty in getting started with 1.For step2 , I guess I can use
adb uninstall package name
Thanks.
You don't have to launch eclipse to run junit test cases.
Try running the Junit tests through script using command line.
More information here. http://www.jsystemtest.org/?q=node/44

Categories

Resources