簡體   English   中英

如何創建機器人測試套件?

[英]how to create a robotium testsuite?

使用下面的代碼,測試不會按照我想要的順序執行。 test_homescreen在test_splashscreen之前執行。

我想指定要運行的測試及其執行順序。 我相信我需要創建一個測試套件,但我不知道如何實現它。

package com.myapp.test;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
import com.myapp.R;

public class myTest extends ActivityInstrumentationTestCase2{

    private static final String TARGET_PACKAGE_ID="com.myapp.test";
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.myapp.gui.SplashScreen";
    private static Class launcherActivityClass;
    static{
        try
        {
            launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }
    }
    public myTest ()throws ClassNotFoundException{
        super(TARGET_PACKAGE_ID,launcherActivityClass);
    }
    private Solo solo;

    @Override
    protected void setUp() throws Exception{
        solo = new Solo(getInstrumentation(),getActivity());
    }

    public void test_splashscreen() throws InterruptedException {
        TextView splashAppVersion = (TextView) solo.getView(R.id.AppVersion); 
        assertTrue(splashAppVersion.isShown());     
    }

    public void test_homescreen() throws InterruptedException {
        ListView lv = (ListView) solo.getView(R.id.List);
        assertTrue(lv.isShown());
        }   

    @Override
    public void tearDown() throws Exception {
        try { 
            solo.finishOpenedActivities();    
        } catch (Throwable e) { 
            e.printStackTrace(); 
        }       
        super.tearDown(); 
    }       
}
  1. 執行第一個test_splashscreen(),然后執行test_homescreen()

  2. 只執行test_homescreen()

這篇文章似乎接近我想要的但我還沒有能夠利用它。 太通用了。 Android Robotium - 如何管理測試用例的執行順序?

我們知道robotium按字母順序運行測試用例。 因此,為了獲得更好的結果,我們可以為不同的活動分別設 稍后,與該活動相關的其他測試用例可以保存在同一個包中(為單獨的活動保留單獨的包)。 這將有助於將相同活動的測試用例一起運行。 要更改測試順序,您可以在命名測試用例時始終使用字母表。 例如:“testAddSplash”將在“testHomeScreen”之前運行。

您還可以使用suite()方法:

public static final Test suite()
{ 
                TestSuite testSuite = new TestSuite(); 
                testSuite.addTest(new MyTestCase("test1")); 
                testSuite.addTest(new MyTestCase("test2")); 
                return testSuite; 
} 

您的測試用例必須具有no-arg構造函數和帶有字符串參數的構造函數,如下所示。

public MyTestCase(String name)
{ 
            setName(name); 
} 

首先,依賴於按特定順序運行的測試是不好的。 如果他們要求一個接一個地運行你應該問自己為什么他們分開測試? 如果他們依賴於先前的測試狀態,則先前測試中的任何失敗都將導致下一個測試失敗。

現在已經這么說了,你可能會說我不在乎我只想讓它起作用。 因此,無論如何,我會給你答案。 您當然可以像其他人所說的那樣做,並將您的測試重命名為按字母順序運行。 但是你似乎想要更多級別的控制,所以這里是:

import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
    public static Test suite() {
        TestSuite suite = new TestSuite(AllTests.class.getName());
        suite.addTest(TestSuite.createTest(myTest.class, "test_splashscreen"));
        suite.addTest(TestSuite.createTest(myTest.class, "test_homescreen"));
        suite.addTest(TestSuite.createTest(myTest.class, "test_splashscreen"));
        return suite;
    }
}

這有很多問題,因為您必須將測試名稱作為字符串給出,因此如果您重構測試名稱,您的套件將會中斷(還有很多其他原因)。 通常,測試套件更多地用於在一次運行中將測試類分組在一起。

你可以這樣命名測試用例:

public void test1_whatever()....

public void test3_other()...

public void test2_mytest()...

當你運行它們時,訂單將是:

test1_whatever()

test2_mytest()

test3_other()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM