Eclipse 中的 Android 单元测试:“无法启动测试”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5513491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Unit Testing in Eclipse: "Failed to launch test"
提问by Brian
I've just started trying to set up some unit testing in what is essentially my first android app. I've had a hell of a time finding resource for this, but ultimately was able to scrape together what I hopedwas the right path forward.
我刚刚开始尝试在本质上是我的第一个 android 应用程序中设置一些单元测试。我花了很长时间为此寻找资源,但最终能够拼凑出我希望的正确前进道路。
First, this is what I've done.
首先,这就是我所做的。
In Eclipse, I right-clicked my project that I'd like to create a test project for. 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. Everything else was left as default.
在 Eclipse 中,我右键单击要为其创建测试项目的项目。我选择了 AndroidTools -> New Test Project 我填写了必要的信息,为新项目选择了 ../MyApp/tests 的位置,并选择了 MyApp 作为要测试的项目。其他一切都保留为默认值。
While this was executing I received the below as an error:
在执行此操作时,我收到以下错误消息:
[2011-04-01 08:13:02 - WPMSTest] R.java was modified manually! Reverting to generated version!
[2011-04-01 08:13:02 - WPMSTest] R.java 被手动修改!恢复到生成的版本!
But everything seemed okay. I had a new source tree in my tests folder.
但一切似乎都很好。我的测试文件夹中有一个新的源代码树。
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] Failed to launch test
[2011-04-01 08:23:04 - WPMSTest] 在设备模拟器 5554 上启动仪器 android.test.InstrumentationTestRunner
[2011-04-01 08:23:04 - WPMSTest] 无法启动测试
My two manifest files:
我的两个清单文件:
WPMSTest:
WPMS测试:
<?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!
谢谢!
回答by Brian
I was missing a TestSuite in my Test Project. Once I had my AllTests class extend TestSuite I got past the error.
我的测试项目中缺少一个 TestSuite。一旦我的 AllTests 类扩展了 TestSuite,我就克服了错误。
回答by Emmanuel
For me the problem was that I was using JUnit 4. When I changed to JUnit 3 it started working. I hope this helps.
对我来说,问题是我使用的是 JUnit 4。当我改用 JUnit 3 时,它开始工作了。我希望这有帮助。
回答by Qinwei Gong
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, i.e. 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”前缀重命名测试方法,测试启动并运行测试!
回答by Rashmi.B
[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. At times, when you clean the project, R.java file goes missing. 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 文件中手动进行更改时,您会收到此错误。有时,当您清理项目时,R.java 文件会丢失。在这种情况下,您必须复制相同程序的 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. then the device got recognised.
对于您的第二个问题,当我在设备上测试我的应用程序时,我也遇到了类似的错误。我不得不使用 $adb 在 shell 中进行更改。然后设备被识别。