Android中的单元测试

2015519

23:10

   

Android中,已经内置了Junit所以不需要在导包。只要继承AndroidTestCase类就可以了。

   

首先需要修改AndroidManifest.xml

添加配置

<!-- android:name是固定的写法

android:targetPackage表示要调试的包-->

<instrumentation

android:name="android.test.InstrumentationTeatRunner"

android:targetPackage="com.example.junittest"/>

   

<!-- uses-library的写法是固定的-->

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

   

AndroidManifest.xml的代码如下

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.junittest"

android:versionCode="1"

android:versionName="1.0" >

   

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="18" />

 

<!-- android:name是固定的写法

android:targetPackage表示要调试的包-->

<instrumentation

android:name="android.test.InstrumentationTeatRunner"

android:targetPackage="com.example.junittest"/>

   

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

 

<!-- uses-library的写法是固定的-->

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

 

<activity

android:name="com.example.junittest.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>

</application>

   

</manifest>

   

   

Junit的测试类

package com.example.junittest;

   

import junit.framework.Assert;

import android.test.AndroidTestCase;

   

public class Junittest extends AndroidTestCase {

   

public void test1()

{

double a=10/2;

Assert.assertEquals(5, a);

}

}

posted @ 2015-05-21 15:41  Vonnie_Jade  阅读(242)  评论(0编辑  收藏  举报