2015-10-28 42 views
0

我需要在主活動上單擊按鈕後開始運行我的JUnit測試,這樣我才能從活動的文本字段收集信息並按按鈕啓動根據輸入的值運行JUnit測試。不知道如何處理這個到目前爲止,我有以下幾點:啓動按鈕單擊(Android)上的JUnit測試

import android.test.SingleLaunchActivityTestCase; 
import android.widget.Button; 
import android.widget.EditText; 
import pso.algo.MainActivity; 
import pso.algo.R; 

public class Test extends SingleLaunchActivityTestCase<MainActivity>{ 

    EditText editKilos, editPounds; 
    MainActivity activity; 
    private String amount = ""; 


    public Test(String name) { 
     super("pso.algo", MainActivity.class); 
     setName(name); 
    } 

    protected void setUp() throws Exception { 
     super.setUp(); 
     // Find views 
     activity = getActivity(); 
     // Test needs to start after button press 
     Button button = (Button) activity.findViewById(R.id.button); 
     // Get text from view to later convert to integer 
     EditText text = (EditText) activity.findViewById(R.id.editText); 
     this.amount = text.getText().toString(); 
    } 

    protected void tearDown() throws Exception { 
     super.tearDown(); 
    } 

    public void test(){ 
     // Test code goes here based on the amount given 
    } 
} 

到目前爲止,活動打開,但關閉之前可以輸入信息/按鈕按下。所以我正在尋找的是停止活動/測試,直到活動按鈕被按下。

謝謝!

回答

0

在進行單元測試時,您不應該與應用程序進行交互。您應該啓動單元測試並讓應用程序運行這些測試並將這些結果報告給您。你試圖做的是遠離最佳實踐,我建議你閱讀單元測試的內容。