2012-11-05 90 views
0

在黑箱測試環境中需要包括CODE 1CODE結束2通過運行Android JUnit測試進行測試(從Robotium網站解釋):我如何抽象Robotium安裝運行多個測試文件

CODE 1:

public class ConnectApp extends ActivityInstrumentationTestCase2 { 
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.example.android.notepad.NotesList"; 
private static Class<?> launcherActivityClass; 
private Solo solo; 
static { 
    try { launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME); } 
    catch (ClassNotFoundException e) { throw new RuntimeException(e); } 
} 
public ConnectApp() throws ClassNotFoundException { 
    super(launcherActivityClass); 
} 
public void setUp() throws Exception { 
    this.solo = new Solo(getInstrumentation(), getActivity()); 
} 

CODE 2:

public void testNumberOne() { … } 
public void testNumberTwo() { … } 

} 

然而,我想代碼的抽象CODE 1(其包括getInstrumentation()和getAcitvity()),這樣我可以簡單地調用它們在單獨的測試文件,然後運行CODE 2。這是因爲我想在單獨的文件中進行測試,並且不想繼續添加相同數量的代碼,只需調用一個方法/構造函數即可啓動該過程。

有沒有辦法做到這一點?先謝謝你。

回答

2

是的,有一種方法可以做到這一點。你將需要做的是創建一個空的測試類,如:

public class TestTemplate extends ActivityInstrumentationTestCase2 { 

    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.example.android.notepad.NotesList"; 
    private static Class<?> launcherActivityClass; 
    private Solo solo; 

    static { 
     try { launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME); } 
     catch (ClassNotFoundException e) { throw new RuntimeException(e); } 
    } 
    public ConnectApp() throws ClassNotFoundException { 
     super(launcherActivityClass); 
    } 

    public void setUp() throws Exception { 
     super.setUp();//I added this line in, you need it otherwise things might go wrong 
     this.solo = new Solo(getInstrumentation(), getActivity()); 
    } 

    public Solo getSolo(){ 
     return solo; 
    } 
} 

然後你想要的,而不是延長ActivityInstrumentationTestCase2您將擴展TestTemplate在未來的每一個測試類。

例如:

public class ActualTest extends TestTemplate { 
    public ActualTest() throws ClassNotFoundException { 
    super(); 
} 

    public void setUp() throws Exception { 
     super.setUp(); 
     //anything specific to setting up for this test 
    } 

    public void testNumberOne() { … } 
    public void testNumberTwo() { … } 
} 
+0

謝謝!我會嘗試的! – user1754960

+0

你是否想將'connectApp()'構造函數改爲'TestTemplate()'?如果沒有,我會得到'隱式超級構造函數ActivitiyInstrumentationTestCase2未定義...'的錯誤,但是如果我按照我所說的做了,然後在測試中擴展TestTemplate給了我'默認構造函數無法處理異常類型ClassNotFoundException'。我該怎麼辦,對不起,我是新手,並再次感謝您的高級 – user1754960

+0

我編輯了我的答案,希望這可以解決您的問題!如果它不讓我知道問題是什麼。 –

相關問題