Android 测试类的使用

来源:互联网 发布:网络上icbc是什么意思 编辑:程序博客网 时间:2024/06/06 12:32

一、编写一个需要被测试的类

//需要测试的类package com.example.helloworld;public class Person {    public int add(int i, int j){        return i+j;    }}

二、编写一个测试类

package junit.test;import com.example.helloworld.Person;import android.test.AndroidTestCase;import android.util.Log;//测试类需要继承AndroidTestCasepublic class TestPerson extends AndroidTestCase {    //测试方法    public void test1(){        Person p = new Person();        int res = p.add(1, 3);        System.out.println(res);    }}

三、修改AndroidMainFest.xml文件
在标签中添加如下内容
targetPackage里的为测试的包名

<instrumentation android:name="android.test.InstrumentationTestRunner"        android:targetPackage="com.example.helloworld"></instrumentation>

在应用程序标签中添加如下内容

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

四、打开模拟器,运行测试类中的方法
选中测试类中的方法,右击选择Run as ->Android JUnit Test,即可运行测试方法。

原创粉丝点击