當前位置:首頁 » 安卓系統 » junit測試android

junit測試android

發布時間: 2022-09-08 22:02:35

A. android junit測試用例怎麼寫

JUnit 基本原理
一個 JUnit 測試包含以下元素:
測試用例組成
開發代碼部分 測試代碼部分 測試工具部分
待測試類 A 通過擴展 TestCase 或者構造 TestSuit 方法
編寫測試類 B 一個測試運行器(TestRunner)R,可以選擇圖形界面或文本界面
操作步驟:
將 B 通過命令行方式或圖形界面選擇方式傳遞給 R,R 自動運行測試,並顯示結果。
首先看下junit測試類庫和android中單元測試類庫:
SDK 功能說明
junit.framework JUnit測試框架
junit.runner 實用工具類支持JUnit測試框架
android.test Android 對JUnit測試框架的擴展包
android.test.mock Android的一些輔助類
android.test.suitebuilder 實用工具類,支持類的測試運行
在這些包中最為重要的是: junit.framework、 android.test,其中前者是JUnit的核心包,後者是Andoid SDK在Junit.framework的基礎上擴展出來的包。

B. android junit單元測試怎麼獲得context

package com.test.hellojunit;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

C. 如何對Android應用程序進行JUnit測試

package com.junit.test;
public class CalService {
public int add(int x, int y) {
return x + y;
}
}
測試類代碼:
import android.app.AlertDialog;
import com.junit.test.CalService;
import junit.framework.TestCase;
public class testCalService extends TestCase {
public void testAdd() throws Exception{
CalService cal = new CalService();
int result = cal.add(2, 3);
assertEquals(5, result);
}
}
寫測試類時候需要注意兩點:
1. 要想使用JUnit測試,需要將測試類繼承TestCase
2. 測試方法需要捕獲異常

D. android studio junit 怎麼測試

java開發中使用junit進行單元測試是常有的事,那麼android中呢?答案是肯定的,也可以!
使用方式也非常的簡單,只需要在AndroidManifest.xml幾加入兩行配置,然後寫個一類繼承AndroidTestCase類即可,其它的跟java使用junit是一樣的(比如)。
AndroidManifest.xml示例代碼:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javake.hzy.filesave"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<!-- junit測試配置關鍵配置第一處 -->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.javake.hzy.filesave" android:label="my app test"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- junit測試配置關鍵配置第二處 -->
<uses-library android:name="android.test.runner" />
<activity android:name=".FileSave"
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>

單元測試類示例代碼:

package com.javake.hzy.filesave;
import java.io.FileOutputStream;
import android.content.Context;
import android.test.AndroidTestCase;

/**
* 測試類用於測試文件讀寫相關操作
* 單元測試只需要繼承AndroidTestCase類
* 測試方法前用test做為前綴即可
* 測試時右鍵run as彈出菜單中選擇Android JUnit Test
* @author hzy
*
*/
public class MyTest extends AndroidTestCase {
/**
* 測試方法1,創建文件並寫入字元串
*/
public void test01() {
Context context = this.getContext();
System.out.println(context);
try {
FileOutputStream out = context.openFileOutput("hello.txt", Context.MODE_PRIVATE);
out.write("hello world my name is hzy".getBytes());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

E. android 測試junit只能測試輸入輸出嗎 能不能測網路連接

1.在AndroidManifest.xml中添加兩處:
(1)

[html] view plain
<!-- 使用單元測試類庫 -->
<uses-library android:name="android.test.runner" />

這個要添加在<application>標簽裡面,在四大組件前面聲明

(2)

[html] view plain
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="niemi"
android:targetPackage="com.gracker.androidtestproject" >
</instrumentation>

這個在<application>標簽外面寫,注意name和targetPackage是必須的,而且需要注意的是targetPackage的內容是你應用程序的包名,和測試類無關。

2.寫測試類
(1)單元測試方法需要聲明微public類型
(2)單元測試方法的返回值類型為void
(3)按照JUnit3的規范要求單元測試方法的命名需要以test開頭
(4)單元測試的方法必須是無參數的
(5)單元測試方法需要聲明向單元測試框架拋出異常
(6)可以在setUp中做測試的初始化工作,比如打開資料庫鏈接或者鏈接網路等
(7)可以在tearDown中做清理工作,如斷開資料庫鏈接或者網路等

F. 如何在Android中利用Instrumentation來進行測試

Android單元測試是通過junit框架來測試的。Android中建立JUnit測試環境有以下方法。集成步驟:1.在androidManifest.xml文件中添加以下代碼:2.新建一個測試測試類並繼承AndroidTestCase類,編寫測試方法,在測試方法內使用斷言assert來測試要測試的方法。3.點擊右面的大綱視圖,選擇要測試的方法,右鍵,runas--->AndroidJUnittest。

G. Android Junit Test測試原理

我也在弄這個啊,android的單元測試是要把測試的目標apk和編譯出來的那個測試apk都要安裝上才可以測試的啊,你這不用模擬器在么測試啊,那就只能是在真機上面測試了啊,測試程序會調用你的被測試的程序代碼的,你如果不信可以打log看看啊。

H. as中android junit單元測試怎麼用

進行Android單元測試 Menifest.xml中加入: <application>中加入: <uses-library android:name="android.test.runner" /> <application>外面加入: <uses-permission android:name="android.permission.RUN_INSTRUMENTATION" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="name.feisky.android.test" android:label="Test for my app"/> 編寫單元測試代碼:必須繼承自AndroidTestCase類 package name.feisky.android.test; import android.test.AndroidTestCase; import junit.framework.Assert; public class MyTest extends AndroidTestCase { private static final String Tag="MyTest"; public void testSave() throws Throwable { int i=4+8; Assert.assertEquals(5,i); } public void testSomethingElse() throws Throwable { Assert.assertTrue(1 + 1 == 12); } } 執行測試 IntelliJ中: eclipse中:右鍵 run as Android JUnit Test 命令行工具: adb shell am instrument -w name.feisky.android.test/android.test.InstrumentationTestRunner 也可以新建一個測試項目進行測試 New > Project > Android > Android Test Project. 添加測試用例類 添加新類,基類設置為android.test.<HelloAndroid> 添加構造函數 添加setUp()方法,這個方法在所有的測試之前進行變數和測試環境的初始化。 @Override protected void setUp() throws Exception { super.setUp(); mActivity = this.getActivity(); mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview); resourceString = mActivity.getString(com.example.helloandroid.R.string.hello); } 添加testPreconditions()方法,檢查初始化環境,只執行一次 public void testPreconditions() { assertNotNull(mView); } 添加單元測試 public void testText() { assertEquals(resourceString,(String)mView.getText()); } 測試 Run As... > Android JUnit Test

I. Android Studio的兩種單元測試:Androud單元測試和Junit單元測試有什麼區別

測試環境都是依賴於某一個環境的,junit就是和main函數一樣,沒有什麼環境依賴直接運行,安卓單元測試的話無非也是測試某些函數,測試介面是否正常,那麼就可以用到 mockito這個框架,也就是說可以模擬一些平時不好debug的情況 例如

DbConnect conn = mock(DbConnect);

when(conn.query(anyInt())).thenReturn("{json數據或者其他}");

這樣我們就能夠比較科學的測試我們想要的函數行為是否正常了,貼一個我的博客鏈接,你可以直接點mockito官網 網頁鏈接也可以看看我的blog哦網頁鏈接

熱點內容
返校vlog腳本 發布:2024-05-09 04:15:53 瀏覽:618
vps雲伺服器免費租用 發布:2024-05-09 04:10:42 瀏覽:207
空調壓縮機排量 發布:2024-05-09 04:08:42 瀏覽:538
android使用靜態庫 發布:2024-05-09 04:05:40 瀏覽:213
原生安卓開機動畫在哪裡 發布:2024-05-09 03:52:19 瀏覽:394
微信收藏在哪個文件夾 發布:2024-05-09 03:47:03 瀏覽:826
ftp遠程登錄 發布:2024-05-09 03:44:40 瀏覽:227
linuxoracle配置環境變數配置 發布:2024-05-09 03:44:38 瀏覽:499
分類信息網站的源碼 發布:2024-05-09 03:31:18 瀏覽:99
sqlupdate日期 發布:2024-05-09 03:27:14 瀏覽:880