標籤:
1.首先建立一個要被測試的工程,命名為”robotium“;一個很簡單的Android 應用程式;首頁面只有個 TextView 控制項;
2. 在建一個用於測試的工程 ,命名為”robotiumTestCase" ;檢查被測apk的頁面是否有某個字 如“hello ” “world” 等;
2.1 選擇file-》new--》project----》Android test project
2.2 輸入測試工程的名字 robotiumTestCase
2.3 選擇被測試的項目 “robotium” 然後點擊finish 完成測試專案的建立;
2.4 建立一個用於測試的測試案例 。在剛剛建立的工程 右鍵 選擇---new ---->class ------》finish
3. 匯入robotium ;
3.1 將下載的 robotium-solo-xxx.jar 儲存到測試工程的根目錄下的libs 中,如果沒有可以新建立一個libs 檔案夾;
3.2 把robotium-solo-xxx.jar 加入到測試工程的引用路徑中來;右鍵測試工程----選擇----》build path----》configure build;
3.3 在彈出Java build path 的對話方塊中選擇 libraries 的那個tab選項------》點擊 add external JARS。。。那個按鈕 添加剛剛在步驟3.1 中的儲存的robotium-solo-xxx.jar
3.4 同樣的在Java build path 的對話方塊中選擇 orderand export的那個tab選項----》選擇robotium-solo-xxx.jar 並且讓其置頂;
具體中步驟
4.添加了開原庫 robotium-solo-xxx.jar ,接下來就是編寫測試案例了,檢查一下首頁面是否有某個字;
代碼如下:
package com.example.robotium.test;import com.robotium.solo.Solo;import android.test.ActivityInstrumentationTestCase2; public class test extends ActivityInstrumentationTestCase2 { private Solo solo; public test() throws Exception { super(Class.forName("com.example.robotium.MainActivity")); // TODO Auto-generated constructor stub } @Override protected void setUp() throws Exception { solo = new Solo(getInstrumentation(),getActivity()); } @Override protected void tearDown() throws Exception { solo.finishOpenedActivities(); } public void test測試案例() throws Exception{ boolean expected =true; boolean actual =solo.searchText("debbie")&&solo.searchText("xie"); assertEquals("debbie is are not found", expected, actual); } }
5. 最後就是運行我們的測試工程了。右鍵測試工程,選擇run as -----> andriod junit test .執行完成後,將看到一個測試結果;
看到的結果是錯誤的 ;因為被測試的apk 只有hello world 這些字 而沒有“Debbie” or “xie"
Android test---robotium----簡單例子