简体   繁体   English

Eclipse中的Android单元测试:“无法启动测试”

[英]Android Unit Testing in Eclipse: “Failed to launch test”

I've just started trying to set up some unit testing in what is essentially my first android app. 我刚开始尝试在基本上是我的第一个Android应用程序中设置一些单元测试。 I've had a hell of a time finding resource for this, but ultimately was able to scrape together what I hoped was the right path forward. 我有一段时间为此找到资源,但最终还是能够把我希望的正确道路向前推进。

First, this is what I've done. 首先,这就是我所做的。

In Eclipse, I right-clicked my project that I'd like to create a test project for. 在Eclipse中,我右键单击了我想为其创建测试项目的项目。 I selected AndroidTools -> New Test Project I filled out the necessary information selecting a location of ../MyApp/tests for the new project and selected MyApp as the project to test. 我选择了AndroidTools - > New Test Project我填写了必要的信息,为新项目选择../MyApp/tests的位置,并选择MyApp作为要测试的项目。 Everything else was left as default. 其他一切都被保留为默认值。

While this was executing I received the below as an error: 在执行此操作时,我收到以下错误:

[2011-04-01 08:13:02 - WPMSTest] R.java was modified manually! [2011-04-01 08:13:02 - WPMSTest]手动修改R.java! Reverting to generated version! 恢复生成的版本!

But everything seemed okay. 但一切似乎都没问题。 I had a new source tree in my tests folder. 我的tests文件夹中有一个新的源代码树。

So I tried to execute it (first on hardware, then on the emulator) by RunAs -> Android jUnit test. 所以我尝试通过RunAs - > Android jUnit测试执行它(首先在硬件上,然后在模拟器上)。

In both runs I received the below in my eclipse console: 在两次运行中,我在我的eclipse控制台中收到了以下内容:

[2011-04-01 08:23:04 - WPMSTest] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554 [2011-04-01 08:23:04 - WPMSTest]在设备模拟器-5554上启动检测android.test.InstrumentationTestRunner
[2011-04-01 08:23:04 - WPMSTest] Failed to launch test [2011-04-01 08:23:04 - WPMSTest]无法启动测试

My two manifest files: 我的两个清单文件:

WPMSTest: WPMSTest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.WPMS.test"
  android:versionCode="1"
  android:versionName="1.0">

<instrumentation android:targetPackage="com.WPMS" android:name="android.test.InstrumentationTestRunner" />
<application android:icon="@drawable/icon" android:label="@string/app_name">

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

WPMS: WPMS:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.WPMS"
  android:versionCode="1"
  android:versionName="1.0">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher_wpms">
    <activity android:name=".WPMS"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>    

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

I'm hoping someone has seen something like this before and can shed some light on what I'm doing wrong. 我希望有人之前见过这样的事情,并且可以解释我做错了什么。 Please let me know if you need any more files and I'll be sure to post them. 如果您需要更多文件,请告诉我,我一定会发布。

Thanks! 谢谢!

I was missing a TestSuite in my Test Project. 我在测试项目中错过了一个TestSuite。 Once I had my AllTests class extend TestSuite I got past the error. 一旦我的AllTests类扩展了TestSuite,我就错过了。

For me the problem was that I was using JUnit 4. When I changed to JUnit 3 it started working. 对我来说问题是我使用的是JUnit 4.当我改为JUnit 3时,它开始工作了。 I hope this helps. 我希望这有帮助。

I had the same problem: JUnit 3 TestCase works, but when running JUnit 4 TestCase, I got "Failed to launch test" error. 我有同样的问题:JUnit 3 TestCase工作,但是当运行JUnit 4 TestCase时,我得到“无法启动测试”错误。

But after following Ken's suggestion, ie make the JUnit 4 test class extends TestCase, and rename the test method with the "test" prefix, the test launches and runs the test! 但遵循Ken的建议,即使JUnit 4测试类扩展TestCase,并使用“test”前缀重命名测试方法,测试启动并运行测试!

[2011-04-01 08:13:02 - WPMSTest] R.java was modified manually! Reverting to generated version!

You get this error when you manually make changes in the R.java file. 手动在R.java文件中进行更改时出现此错误。 At times, when you clean the project, R.java file goes missing. 有时,当您清理项目时,R.java文件会丢失。 At such occasion you must either copy the same program's R.java(if stored somewhere as a copy) or create an entire new project. 在这种情况下,您必须复制相同程序的R.java(如果存储在某处作为副本)或创建一个完整的新项目。

For your second issue, I too have got similar error when I was testing my app on the device. 对于您的第二个问题,当我在设备上测试我的应用时,我也遇到了类似的错误。 I had to make changes in the shell using $adb. 我不得不使用$ adb在shell中进行更改。 then the device got recognised. 然后设备得到了认可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM