简体   繁体   中英

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. 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. 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.

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!

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.

In both runs I received the below in my eclipse console:

[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

My two manifest files:

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:

<?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. Once I had my AllTests class extend TestSuite I got past the error.

For me the problem was that I was using JUnit 4. When I changed to JUnit 3 it started working. 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.

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!

[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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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