目前网上的Robotium在AndroidStudio部署配置大部分已经过时,虽然可以运行但会引入很多问题,临时记录搭建及实践过程,后续再详细写下。
第一步:部署AndroidStudio环境
1、开发工具首先要使用AndroidStudio,在eclipse里ADT已经不再更新,eclipse中的junit依然停留在3.8版本,AndroidStudio的junit版本是4.12,可以支持参数化测试。
2、请使用androidTest包开发测试脚本,在AndroidStudio下重新划分了工程的结构,分为androidTest、main、test三个文件夹,其中androidTest为Instrmentation测试包,main为源码包,test为单元测试包,Robotium是在Instrmentation基础上二次开发的测试框架,所以我们在这个包内写测试脚本。
注:从eclipse导出的AndroidStudio工程虽然可以运行,新建的包也可以正确显示,但是导入junit包始终不识别,我这里没有解决,最后新建工程无问题。
第二步:开发配置部署
3、修改build.gradle
defaultConfig { minSdkVersion 19 targetSdkVersion 24 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:24.2.1' androidTestCompile 'junit:junit:4.12' androidTestCompile 'com.android.support.test:rules:0.4.1'因为我这里直接使用Robotium源码,不引入jar,如果需要引用jar加入下面这句
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.6.0'
注:注意这里testInstrumentationRunner使用的是android.support.test.runner.AndroidJUnitRunner
而不是android.test.InstrumentationTestRunner,这个要特别注意,虽然一样可以运行
第三步:开发case代码
4、新的执行环境不需要继承ActivityInstrumentationTestCase2
详细可以参考官方的源码,链接如下:http://dl.bintray.com/robotium/generic/ExampleTestProject_Eclipse_v5.5.1.zip
注:一定要有@RunWith(AndroidJUnit4.class),否则无法找到testcase,参数化执行修改为@RunWith(Parameterized.class)
@RunWith(AndroidJUnit4.class)
public class NotePadTest {
private static final String NOTE_1 = "Note 1";
private static final String NOTE_2 = "Note 2";
@Rule
public ActivityTestRule<NotesList> activityTestRule =
new ActivityTestRule<>(NotesList.class);
private Solo solo;
@Before
public void setUp() throws Exception {
//setUp() is run before a test case is started.
//This is where the solo object is created.
solo = new Solo(InstrumentationRegistry.getInstrumentation(),
activityTestRule.getActivity());
}
@After
public void tearDown() throws Exception {
//tearDown() is run after a test case has finished.
//finishOpenedActivities() will finish all the activities that have been opened during the test execution.
solo.finishOpenedActivities();
}
@Test
public void testAddNote() throws Exception {
5、执行修改
Run-Edit Configurations-Android Tests-Test类-Specific instrument runner(optional)为android.support.test.runner.AndroidJUnitRunner,一般默认就是这个
6、参数化执行方案源码如下,执行通过:
@RunWith(Parameterized.class) public class TestCase { int direction ; boolean fresh; boolean forward; String url; public TestCase (int direction, boolean fresh, boolean forward, String url){ this.direction = direction; this.fresh = fresh; this.forward = forward; this.url = url; } @Parameterized.Parameters public static Collection data(){ return Arrays.asList(new Object[][]{ {1,false,false,"https://www.baidu.com/"}, {1,false,true,"https://www.baidu.com/"}, {1,true,false,"https://www.baidu.com/"}, {1,true,true,"https://www.baidu.com1111/"}}); } @Test public void test_AAA() throws Exception { }