zoukankan      html  css  js  c++  java
  • adroid单元测试

    adroid单元测试

    1,

    重要的是在AndroidManifest.xml中添加


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

    记住是在 <application 里面  在<activity  上面 还有就是在<application外面添加 

    <instrumentation
            android:name="android.test.InstrumentationTestRunner"
            android:label="Tests for My App"
            android:targetPackage="com.dd.dd" />
    在此处 
    android:name 值就为  android.test.InstrumentationTestRunner
    
    
    android:targetPackage 值为你的包名,跟AndroidManifest.xml上面默认的包名一致,

    2,接下来就是写测试类,这个类一定要继承AndroidTestCase类


    下面看看代码

    AndroidManifest.xml的代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.dd.dd"         //这个包名
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="17" />
    
        <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" />   //这个要添加  不然就会提示ClassCastException异常
    
            <activity
                android:name="com.dd.dd.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>
    
            <service android:name=".ExampleService" />
            <service android:name=".ExampleIntentService" />
            <service android:name=".StatusBarService" />
        </application>
    
        <instrumentation
            android:name="android.test.InstrumentationTestRunner"   //这个值就为android.test.InstrumentationTestRunner
            android:label="Tests for My App"
            android:targetPackage="com.dd.dd" />  //这个包名和上面的包名一致(我在上面用注释标记过的)
    
    </manifest>

    接下来就是测试类了 


    package com.dd.dd.dao;
    
    import android.test.AndroidTestCase;
    import android.util.Log;
    
    
    public class StudentDaoTest extends AndroidTestCase {    //切记,一定要继承 AndroidTestCase类
    
    	private static final String TAG = "StudentDaoTest";
    
    	public void test() {
    		Log.i(TAG, "单元测试测试成功!");
    	}
    }
    



    接下来就是点击测试了

    打开那个测试类的方法,右击选择 

    然后



    然后点击run 运行  此刻就看控制台和logcat你要输出来到标签  输出来了没有


    ok

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    docker1
    Ubuntu中安装deb包程序
    Linux性能评测工具之一:gprof篇介绍
    Lua在Linux下的安装
    gprof的使用介绍
    Linux性能评测工具之一:gprof篇
    google-perftools 分析JAVA 堆外内存
    NetHogs下载和监控
    Google perf tools for nginx
    ECLIPSE中添加TPTP插件
  • 原文地址:https://www.cnblogs.com/shipeng22022/p/4614053.html
Copyright © 2011-2022 走看看