安卓单元测试错误Test run failed: Unable to find instrumentation target package解决办法

时间:2022-09-13 20:44:32

写好了数据库的代码,进行单元测试

FriendInfoDAOTest.java

package com.example.sqlite.activity;

import java.util.List;

import com.example.sqlite.dao.FriendInfoDAO;
import com.example.sqlite.model.FriendInfo;

import android.test.AndroidTestCase;
import android.util.Log;

public class FriendInfoDAOTest extends AndroidTestCase
{
	private final static String TAG="FriendInfoDAOTest";
	
	public void testAdd()
	{
		FriendInfoDAO FriendInfoDAO=new FriendInfoDAO(this.getContext());
		FriendInfo FriendInfo=new FriendInfo(001,"GuGuTian","男生","代码","1993-06-22","sd/images");
		FriendInfoDAO.add(FriendInfo);
		Log.i(TAG, "add successed");
	}

	public void testUpdate()
	{
		FriendInfoDAO FriendInfoDAO=new FriendInfoDAO(this.getContext());
		FriendInfo FriendInfo=FriendInfoDAO.find(1);
		FriendInfo.setNickname("xing634325131");
		FriendInfoDAO.update(FriendInfo);
		Log.i(TAG, "update successed");
	}

	public void testFind()
	{
		FriendInfoDAO FriendInfoDAO=new FriendInfoDAO(this.getContext());
		FriendInfo FriendInfo=FriendInfoDAO.find(1);
		if (null==FriendInfo)
		{
			Log.i(TAG, "not find");
		}
		else
		{
			Log.i(TAG, FriendInfo.toString());
		}
	}

	public void testDetele()
	{
		FriendInfoDAO FriendInfoDAO=new FriendInfoDAO(this.getContext());
		FriendInfoDAO.delete(1,2,3);
		Log.i(TAG, "delete successed");
	}

	public void testGetScrollData()
	{
		FriendInfoDAO FriendInfoDAO=new FriendInfoDAO(this.getContext());
		List<FriendInfo> FriendInfos=FriendInfoDAO.getScrollData(0, 1);
		for (FriendInfo FriendInfo : FriendInfos)
		{
			Log.i(TAG, FriendInfo.toString());
		}
	}

	public void testGetCount()
	{
		FriendInfoDAO FriendInfoDAO=new FriendInfoDAO(this.getContext());
		Long count=FriendInfoDAO.getCount();
		Log.i(TAG, ""+count);
	}

}

发现有这个错误:

安卓单元测试错误Test run failed: Unable to find instrumentation target package解决办法

以下为AndroidManifest.xml代码:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="android.test.runner"/>
        <activity
            android:name="com.example.test_intent.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.test_intent.login"
            android:label="@string/sec_string"
            ></activity>
        <activity
            android:name="com.example.test_intent.Friend"
            android:label="@string/my_friend"
            android:logo="@drawable/search_icon"
            ></activity>
         <activity
            android:name="com.example.test_intent.FriendSearch"
            ></activity>
    </application>
    
    <instrumentation android:name="android.test.InstrumentationTestRunner"
					 android:targetPackage="com.example.sqlite.activity"
					 android:label="Test My App"></instrumentation>
    
</manifest>

原因:是因为

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_intent"
的package的名字 com.example.test_intent

<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.sqlite.activity"

的targetPackage的名字 com.example.sqlite.activity不同的缘故
解决办法:把两者名字改为一样即可。一般把后者改为与前者相同,这样可以不必改变程序的主入口。