如何正确使用android.test.ServiceTestCase?

how to correctly use android.test.ServiceTestCase?

我的应用程序是在 /system/app

中运行的服务
com.abc.def.MyApp

尝试为其编写单元测试时,我在 logcat 而 运行.

中收到此错误
I/TestGrouping( 5647): TestCase class com.abc.def.test.MyAppTest is missing a public constructor with no parameters or a single String parameter - skipping

使用的命令:

D:\tmp_install>adb shell am instrument -w com.abc.def.test/android.test.InstrumentationTestRunner
WARNING: linker: memtrack.jacinto6.so: unused DT entry: type 0xf arg 0x115
WARNING: linker: libsrv_um.so: unused DT entry: type 0xf arg 0xc4e
WARNING: linker: gralloc.jacinto6.so: unused DT entry: type 0xf arg 0x5d9
WARNING: linker: libpvr2d.so: unused DT entry: type 0xf arg 0x778

Test results for InstrumentationTestRunner=
Time: 0.0

OK (0 tests)

我的代码片段是:

public class MyAppTest extends ServiceTestCase<MyApp> {
public MyAppTest(Class serviceClass) {
      super(serviceClass);
      Log.d(tag, "hello world! MyAppTest ctor");
}
public MyAppTest() {
  super(com.abc.def.MyApp.class);
}

.....

我已经关注了这个答案

我是否错误地使用了 ServiceTestCase?

据我所知,您使用 ServiceTestCase 是正确的!

通常,您只需要没有参数的构造函数即可正常工作,而您已经提供了它。

public MyAppTest() {
    super(com.abc.def.MyApp.class);
}

您收到的 linker 警告通常与 NDK 相关。对于您的测试来说,它们仍然不是问题。

虽然不是完整的解决方案,但我建议您尝试 运行使用 gradle connectedAndroidTest 进行相同的测试。如果他们 运行 正确,至少我们知道问题出在 am instrument - 也许没有正确配置 TEST project.

编辑:如果您使用 Gradle 构建您的项目(默认情况下,如果您使用 Android Studio),只需进入您的主项目目录并执行以下命令,这将 运行 您所有的 Instrumentation 测试(如上面的 ServiceTestCase 示例)。

./gradlew connectedAndroidTest

正如Vesko所说,代码是正确的;然而,问题的根本原因是 Dex 预优化 - 对我的代码的更新没有显示在运行时的行为中。

解决方案?我必须将 odex 文件和 apk 推送到设备。