2012-11-05 47 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

我编辑了我的答案,希望这可以解决您的问题!如果它不让我知道问题是什么。 –

相关问题