由于歧义参数异常,Android工具测试未运行

时间:2018-12-31 00:06:24

标签: android unit-testing

在两种情况下,我无法在我的Android项目中运行仪器测试:

  1. 运行单个测试(即单个@Test)时
  2. 在一个类中运行所有测试时

am 能够通过右键单击测试所在的文件夹并选择Run 'Tests in PACKAGE_NAME'选项来运行仪器测试

这是运行单个测试的示例:

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.

当我通过右键单击选项运行包中的所有测试时,会看到以下内容:

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.

这里唯一的“陷阱”可能是我正在使用自定义测试运行程序(上面引用的TestRunner.java)。我不确定这太重要了。我将尝试删除它(某些测试需要自定义运行器,但我现在可以禁用它们……)

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我调试了此问题并找出了根本原因。我使用的自定义运行程序忽略了测试包。我实施了https://github.com/mockito/mockito/issues/922中所述的变通办法,以使我的Mockito东西与自定义运行器一起使用,这导致了错误。

arguments.putString("notPackage", "net.bytebuddy");放在跑步者的onCreate中会导致TestRequestBuilder.java在validate方法中引发异常,这将执行以下操作:

if ((!mIncludedPackages.isEmpty() || !mExcludedPackages.isEmpty()) && !classNames.isEmpty()) {
    throw new IllegalArgumentException(AMBIGUOUS_ARGUMENTS_MSG);
}

mExcludedPackages的大小为1(net.bytebuddy),而classNames也不为空,并且包含我在其中运行测试的类。

因此,要解决此问题,您可以执行以下两项操作之一:

  1. 删除arguments.putString("notPackage", "net.bytebuddy")解决方法(在某些情况下,对于我的测试,mockito无效)。
  2. 将测试重构为不需要自定义运行器,因此也执行步骤1。
  3. 不能为特定的测试/文件运行测试。

我结束了FWIW的第2步。