25

这是我第一次设置一个Android测试项目来测试一个Android项目。

我创建了一个非常基本的测试用例,我试图运行它,但是我没有运行。我得到一个Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'.

我会更多地解决这个问题,但是我没有得到更多信息,例如它试图找到哪个类等。关于如何从错误中获取更多信息的任何想法?我应该查看的任何公共区域,或者我忽略的任何可能需要配置的区域?

以下是来自控制台的信息:

[2013-04-16 13:21:49 - XyzProgramTest] Android Launch!
[2013-04-16 13:21:49 - XyzProgramTest] adb is running normally.
[2013-04-16 13:21:49 - XyzProgramTest] Performing android.test.InstrumentationTestRunner JUnit launch
[2013-04-16 13:21:49 - XyzProgramTest] Automatic Target Mode: launching new emulator with compatible AVD 'GalaxyNexusAPI_17'
[2013-04-16 13:21:49 - XyzProgramTest] Launching a new emulator with Virtual Device 'GalaxyNexusAPI_17'
[2013-04-16 13:21:53 - Emulator] extension WGL_ARB_make_current_read was not found
[2013-04-16 13:21:53 - Emulator] extension WGL_EXT_swap_control was not found
[2013-04-16 13:21:53 - Emulator] Failed to create pbuf surface for FB 0x3004
[2013-04-16 13:21:53 - Emulator] emulator: WARNING: Could not initialize OpenglES emulation, using software renderer.
[2013-04-16 13:21:54 - XyzProgramTest] New emulator found: emulator-5554
[2013-04-16 13:21:54 - XyzProgramTest] Waiting for HOME ('android.process.acore') to be launched...
[2013-04-16 13:22:55 - XyzProgramTest] HOME is up on device 'emulator-5554'
[2013-04-16 13:22:55 - XyzProgramTest] Uploading XyzProgramTest.apk onto device 'emulator-5554'
[2013-04-16 13:22:55 - XyzProgramTest] Installing XyzProgramTest.apk...
[2013-04-16 13:23:57 - XyzProgramTest] Success!
[2013-04-16 13:23:57 - XyzProgramTest] Project dependency found, installing: XyzProgram
[2013-04-16 13:23:57 - XyzProgram] Uploading XyzProgram.apk onto device 'emulator-5554'
[2013-04-16 13:23:58 - XyzProgram] Installing XyzProgram.apk...
[2013-04-16 13:24:05 - XyzProgram] Success!
[2013-04-16 13:24:05 - XyzProgramTest] Launching instrumentation android.test.InstrumentationTestRunner on emulator-5554
[2013-04-16 13:24:07 - XyzProgramTest] Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

补充说明

在我的 Android 测试项目中,我创建了一个简单的测试,其中包含一个与我尝试在我的 Android 项目中测试的类匹配的包。所以像com.company.android.projectname. 我已经在 Android 测试项目manifest文件的instrumentation部分中指定了这一点。

<instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="com.company.android.projectname" />

当我更改它以匹配它时,我收到一个新错误,上面写着Test run failed: Unable to find instrumentation target package: com.company.android.projectname

我不是 100% 确定targetPackage应该是什么,以及我是否需要为每个要测试的包使用多个仪器等。当我将它设置为时com.company.android,我得到java.lang.ClassNotFoundException.

4

7 回答 7

22

我已经升级到 androidx 库并开始收到此错误。

为了解决这个问题,在 build.gradle 我改变了这一行:

testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
于 2018-10-28T09:10:38.310 回答
8

仅供参考,我遇到了这个,不得不添加

<uses-library android:name="android.test.runner"/>

在我的<application/>标签内

于 2014-09-30T04:34:01.780 回答
7

就我而言,我试图在 Jelly Bean 模拟器上运行测试。Android 仪器似乎不支持 Jelly Bean。我可以在棒棒糖上运行测试。

您也可能无法通过发布构建类型运行 android 测试。确保调试构建类型处于活动状态。

于 2018-05-13T11:51:52.570 回答
6

找出我的问题并发布答案以用于文档目的......

我的问题的根源是两件事:

  1. 我做了一些重构,改变了我的 android.app.Application班级和活动的位置。这使得我的主程序无法运行,因为我需要更新我的清单 android project
  2. 在我的 android 项目中对清单进行更新后,我必须在我的中进行一些更新android test project以指向扩展类的更新位置android.app.Application

之后,做了一些清洁并再次测试,一切都很好。

于 2013-04-16T23:06:53.690 回答
3

我得到了同样的错误,因为我忘了添加androidx.test:runner测试依赖项。

androidTestImplementation "androidx.test:runner:1.2.0"
于 2020-03-11T17:21:20.890 回答
1

我今天遇到了这个问题,我采用了位于“${sdk.dir}/tools/ant/”目录中的android-specific ant build.xml ,在命令行中执行所有测试用例,但是STDOUT报告错误对我和你一样:

vinceMacBook ~/dev/aSQLite+-android/tests$ ant clean uninstall debug install test
...
test:
     [echo] Running tests ...
     [exec] INSTRUMENTATION_RESULT: shortMsg=java.lang.ClassNotFoundException
     [exec] INSTRUMENTATION_RESULT: longMsg=java.lang.ClassNotFoundException:
        android.test.InstrumentationTestRunner
     [exec] INSTRUMENTATION_CODE: 0

我花了一些时间来解决这个问题,我在根模块中找到了主要原因AndroidManifest.xml,我认为应用程序元素不需要配置,所以我在清单元素中省略了它,这导致了我的问题。

我不确定您是否与我共享相同的结构,所以我在这里发布我的项目结构,以及密钥文件的内容,希望可以帮助某人。

vinceMacBook ~/dev/aSQLite+-android$ tree
.
├── AndroidManifest.xml
│       <?xml version="1.0" encoding="utf-8"?>
│       <manifest
│           xmlns:android="http://schemas.android.com/apk/res/android"
│           package="com.vincestyling.asqliteplus"
│           android:versionCode="1"
│           android:versionName="0.1">
│
│           <uses-sdk android:minSdkVersion="8" />
│           
│           <!-- important configuration, cannot be omit even if nothing to say. -->
│           <application />
│
│       </manifest>
│
├── build.xml
│       unchanged, generated by "android create project <project_name>" command.
│
├── project.properties
│       unchanged, generated by "android create project <project_name>" command.
│
├── src
│   └── com
│       └── vincestyling
│           └── asqliteplus
│               ├── DBOperator.java
│               ├── DBOverseer.java
│               └── the library source code lie here...
│
└── tests
    ├── AndroidManifest.xml
    │       <?xml version="1.0" encoding="utf-8"?>
    │       <manifest
    │           xmlns:android="http://schemas.android.com/apk/res/android"
    │           package="com.vincestyling.asqliteplus.tests">
    │
    │           <application>
    │               <uses-library android:name="android.test.runner"/>
    │           </application>
    │
    │           <instrumentation
    │               android:name="android.test.InstrumentationTestRunner"
    │               android:targetPackage="com.vincestyling.asqliteplus"/>
    │
    │       </manifest>
    │
    ├── ant.properties
    │       # [Comments], [Comments], [Comments] which generated by command.
    │       # [Comments], [Comments], [Comments] which generated by command.
    │       # [Comments], [Comments], [Comments] which generated by command.
    │
    │       # important configuration, point to the root library module.
    │       tested.project.dir=../
    │
    ├── build.xml
    │       unchanged, generated by "android create test-project <project_name>" command.
    │
    └── src
        └── com
            └── vincestyling
                └── asqliteplus
                    ├── BaseDBTest.java
                    ├── QueryStatementTest.java
                    └── the test source code lie here...

我的项目结构遵循谷歌建议我们做的Android Test Projects的描述,由承载核心逻辑的根库模块和根模块内部的测试模块组成。以上是项目中的关键资源,但不全是文件,等我放出项目链接就放在这里。

------------ 2014-01-06 更新 --------------

目前,我的项目已经发布在我的 github 上:aSQLitePlus-android,有兴趣的可以查看详情。

于 2014-12-31T09:27:18.833 回答
0

合并后我遇到了这个问题,其中 build.gradle 指定了错误的测试包,因为我一直在使用默认值。

通过简单地从 build.gradle 中删除这些行,它能够在默认包中找到必要的类,即src/androidTest.

sourceSets {
    instrumentTest.setRoot('src/instrumentTest')
}
//removed this to use the default androidTest source set.

这可能不是确切的解决方案,但希望能引导某人朝着正确的方向前进。

于 2014-07-29T15:25:18.507 回答