1、新建立一个Android工程

package com.shellway.junit;public class Service {      public int divide(int a,int b){          return a/b;      }}
Service.java.作为被测试类
package com.shellway.junit;import junit.framework.Assert;import android.test.AndroidTestCase;public class TestT extends AndroidTestCase {    public void test1(){        Service service = new Service();        System.out.println(service.divide(10, 2));    }    public void test2(){        Service service = new Service();        System.out.println(service.divide(10, 0));    }    public void test3(){        Service service = new Service();        Assert.assertEquals(2.5, service.divide(10, 4));    }}
TestT继承AndroidTestCase,作为测试类
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.shellway.junit"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="16"        android:targetSdkVersion="21" />       <instrumentation        android:targetPackage="com.shellway.junit"        android:name="android.test.InstrumentationTestRunner" />        <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=".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>
AndroidManifest.xml

注意:在配置文件中要加入的内容是:

 <instrumentation      android:targetPackage="com.shellway.junit"      android:name="android.test.InstrumentationTestRunner" /><uses-library android:name="android.test.runner" />
View Code

并且要注意他们加入的位置。这是一种测试自身工程的方法。

另一种方法是:新建一个Android Junit工程来测试另外的一个工程,这里需要注意的是建立这个测试工程名的时候不能以Test来命名,不然出现错误都很难找出问题所在,下面给出以Test来命名工程的出错信息。

更多相关文章

  1. 关于Android中res等几种文件通过InputStream方式读入工程中
  2. Android(安卓)Texting(2)Testing Fundamentals 测试基础篇
  3. Android(安卓)studio 工程配置相关问题-.grade
  4. Android(安卓)studio 工具中的“Attach Debugger to Android(安
  5. android白盒测试所需其他安卓开发内容链接整理
  6. AndroidGPS定位详解 (4)
  7. 翻译 Migrating to Android(安卓)Studio
  8. Android的单元测试
  9. eclipse导入已有的android工程出错

随机推荐

  1. Android精通教程V
  2. 从源码角度分析Activity、Window和DecorV
  3. android surfaceView+mediaPlayer 自定义
  4. Android Gallery 3D 特效精华
  5. Android/winPhone7开发资源索引
  6. Android(安卓)数字签名
  7. Android课设之校园二手交易app
  8. 升级Android(安卓)Studio3.5后连接不上真
  9. 跨平台框架---uniapp--打包
  10. 实现简易的android 直播技术