Robotium在AndroidStudio中搭建及参数化测试实践

目前网上的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 {
	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值