2012-10-31 26 views
3

下面的代码,测试不按照我想要的顺序执行。在test_splashscreen之前执行test_homescreen。如何创建一个robotium测试套件?

我想指定要运行的测试及其执行顺序。 我相信我需要创建一个测试套件,但我不知道如何实现该套件。只有

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 - How to manage the execution order of testcases?

回答

3

正如我们所知,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; 
} 

你的测试用例务必有一个无参数的构造函数,并用字符串参数看起来像这样的构造函数。

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

我更关心如何实现一个测试套件,列出特定的测试及其执行顺序。否则,做一个测试套件有什么意义? – Franck

0

你能说出你测试的情况是这样的:

公共无效test1_whatever()....

公共无效test3_other()...

公共无效test2_mytest()...

当你运行它们时订货会:

test1_whatever()

test2_mytest()

test3_other()

+0

这是非常微弱的分辨率,因为它只有在你少于11次测试的情况下才有效。测试按字母顺序运行,因此test13_将在test7_ –

+0

在每个数字前面使用0之前触发:test007_和test013_并且它将按顺序运行 – gionnut

1

首先,依靠运行在一个特定的顺序是坏的测试。如果他们需要一个接一个地跑,你应该问自己为什么他们分开测试呢?如果他们依赖于之前的测试状态,那么之前测试中的任何失败都会导致下一次测试失败。

现在说了这些,你可能会说我不在乎我只是想让它工作。因此,无论如何,我会给你答案。当然,你可以像其他人说的那样,将你的测试重新命名为按字母顺序运行。但你似乎希望控制更多的级别,所以在这里它是:

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; 
    } 
} 

这有很多的问题,因为你必须给该测试的名称为字符串,所以如果你重构测试命名套件将打破(还有很多其他原因)。通常,测试套件更多地用于在一次运行中将测试类别分组在一起。

相关问题