Java 如何创建robotium测试套件?
使用下面的代码,测试不会按照我想要的顺序执行。 在测试屏幕之前执行测试屏幕 我想指定要运行的测试及其执行顺序。 我相信我需要创建一个测试套件,但我不知道如何实现它Java 如何创建robotium测试套件?,java,android,testing,junit,robotium,Java,Android,Testing,Junit,Robotium,使用下面的代码,测试不会按照我想要的顺序执行。 在测试屏幕之前执行测试屏幕 我想指定要运行的测试及其执行顺序。 我相信我需要创建一个测试套件,但我不知道如何实现它 package com.myapp.test; import com.jayway.android.robotium.solo.Solo; import android.test.ActivityInstrumentationTestCase2; import com.myapp.R; public class myTest ext
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();
}
}
这篇文章似乎接近我想要的,但我一直无法利用它。太笼统了 正如我们所知,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);
}
您可以这样命名测试用例: 公共无效测试1_whatever() public void test3_other() public void test2\u mytest() 当您运行它们时,顺序将是: test1_whatever() test2_mytest() 测试3_其他()
首先,依赖以特定顺序运行的测试是不好的。如果它们需要一个接一个地运行,你应该问问自己,为什么它们是单独的测试?如果他们依赖于之前的测试状态,则之前测试中的任何失败都将导致下一个测试失败 话虽如此,你可能是在说我不在乎,我只是想让它发挥作用。所以对于你,我无论如何都会给你答案。当然,您可以像其他人所说的那样,将测试重命名为按字母顺序运行。但你似乎想要更多的控制,因此:
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;
}
}
这有很多问题,因为您必须以字符串形式给出测试名称,因此如果您重构测试名称,您的套件将崩溃(还有很多其他原因)。通常,测试套件更多地用于在一次运行中将测试类分组在一起。我更多的是询问如何实现测试套件,列出了特定的测试及其执行顺序。否则,做一个测试套件有什么意义呢?它的分辨率很低,因为只有当你有少于11个测试时它才能工作。测试是按字母顺序运行的,所以test13将在test7之前启动,在每个数字前面使用0:test007和test013,并且它将按顺序运行