Zach
Zach

Reputation: 4079

Android instrumentation tests not running due to Ambiguous arguments exception

I am unable to run instrumentation tests in my Android project in two cases:

  1. When running individual tests (i.e. a single @Test)
  2. 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!

Upvotes: 2

Views: 1077

Answers (1)

Zach
Zach

Reputation: 4079

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:

  1. Remove the arguments.putString("notPackage", "net.bytebuddy") workaround (which makes mockito not work in some cases for my tests).
  2. Refactor your tests to not need the custom runner, thereby doing step 1 as well.
  3. Not be able to run tests for specific tests/files.

I ended up doing step 2, FWIW.

Upvotes: 3

Related Questions