• Android中使用自身携带的Junit新建一个测试工程


    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
    《汇编语言》--程序范例
    《黑客攻防技术-系统实战》开篇讲解
    ptrace理解
    DPDK初始化
    C++ 对象内存模型
    DPDK学习路线计划
    DPDK学习开篇
  • 原文地址:https://www.cnblogs.com/shellway/p/4036372.html
Copyright © 2020-2023  润新知