android对应用进行单元测试

1.新建一个android工程



 2.创建两个包,一个service包,用来存放业务逻辑类,一个test包,用来存放我们         写的单元测试类



 3.编写一个业务逻辑类

package com.xiaobo.app.service;

public class PersonService {
	public String subString(String str) throws Exception{
		return str!=null ? str.substring(6) : "";
	}
	
	public int getSum(int a, int b) throws Exception{
		return a+b;
	}
}

 

 4.编写测试用例

 

package com.xiaobo.app.junittest;

import junit.framework.Assert;

import com.xiaobo.app.service.PersonService;

import android.test.AndroidTestCase;

public class PersonServiceTest extends AndroidTestCase{
	PersonService personService = new PersonService();
	
	public void testSubString() throws Exception{
		String subStr = personService.subString(null);
		assertNotNull(subStr);
	}
	
	public void testGetSum() throws Exception{
		Assert.assertEquals(3, personService.getSum(1, 2));
	}
	
}

 
 5.在
AndroidManifest.xml进行配置


 

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

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

    <uses-permission android:name="android.permission.RUN_INSTRUMENTATION" /> 

	<instrumentation android:name="android.test.InstrumentationTestRunner" 
	    			 android:targetPackage="com.xiaobo.app.junittest" 
					 android:label="JUnit Test"/> 
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <!-- 但如单元测试的library -->
        <uses-library android:name="android.test.runner" /> 
        <activity
            android:name="com.xiaobo.app.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>

 

6.运行



 

猜你喜欢

转载自yellowxiaotian.iteye.com/blog/1826443
今日推荐