单元测试(Android)_JUnit
生活随笔
收集整理的這篇文章主要介紹了
单元测试(Android)_JUnit
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
1 1.首先在AndroidManifest.xml中加入下面紅色的代碼:2 <?xml version="1.0" encoding="utf-8"?>
3 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
4 package="com.senk.junitest"
5 android:versionCode="1"
6 android:versionName="1.0" >
7 <uses-sdk android:minSdkVersion="8" />
8 <application
9 android:icon="@drawable/ic_launcher"
10 android:label="@string/app_name" >
11 <activity
12 android:name=".JUnitestActivity"
13 android:label="@string/app_name" >
14 <intent-filter>
15 <action android:name="android.intent.action.MAIN" />
16 <category android:name="android.intent.category.LAUNCHER" />
17 </intent-filter>
18 </activity>
19 <uses-library android:name="android.test.runner"/>
20 </application>
21 <instrumentation
22 android:name="android.test.InstrumentationTestRunner"
23 android:targetPackage="com.senk.junitest"
24 android:label="Test JUnit"/>
25 </manifest> 注意:上面的targetPackage指定的包要和應用的package相同。
?
2.先貼出一個Activity中要測試的方法,也可以是其他的類,請讀者舉一反三 1 package com.senk.junitest;2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.util.Log;
6
7 public class JUnitestActivity extends Activity {
8 /** Called when the activity is first created. */
9 @Override
10 public void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 setContentView(R.layout.main);
13 }
14
15 /**
16 * 要進行單元測試的方法->求和方法
17 * @ author hongj
18 * @param a
19 * @param b
20 * @return
21 */
22 public double testSummation(double a,double b){
23 double c = a + b;
24 Log.i("TestJUnit", c+"");
25 return c;
26 }
27 }
?
1 3.// 編寫用例測試代碼,2 package com.senk.junitest;
3
4 import android.test.AndroidTestCase;
5 import android.util.Log;
6
7 public class TestJUnit extends AndroidTestCase
8 {
9 public void testSomething() throws Throwable
10 {
11 Log.i("TestJUnit", "在這里寫單元測試的代碼");
12 JUnitestActivity jUnitestActivity = new JUnitestActivity();
13 jUnitestActivity.testSummation(1, 2);
14 }
15 }
16
17 //注意:測試類需要繼承AndroidTestCase,為了讓程序有更好的穩定性,最好要向外拋出異常,即加入“throws Throwable”
18 // 運行步驟:選擇要測試的方法,右鍵點擊“Run As”->"Android JUnit Test"
?
運行后打印出下面的語句:
?
轉載于:https://www.cnblogs.com/jh5240/archive/2012/03/21/2410698.html
總結
以上是生活随笔為你收集整理的单元测试(Android)_JUnit的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈自考学习方法(二)
- 下一篇: 创建一个windows服务的小程序及注意