Espresso小试

Espresso开源了,那就试着用一下,

1. 下载Espresso

Espresso没有提供单独的jar包下载,建议clone整个项目或者下载zip包

git clone https://code.google.com/p/android-test-kit/

或从这里下载:https://code.google.com/p/android-test-kit/source/browse/

我们一般用espresso-1.1-bundled.jar这个包

Espresso小试

2. 使用ADT创建一个Android应用项目EspressoDemo, lib里加入espresso-1.1-bundled.jar

Espresso小试

这个android项目我没干什么,就是加了一个按钮

Espresso小试

 

3. 编写Test,Espresso其实是一个API,通过调用它来完成UI的操作

Espresso小试
 1 package com.example.espressotest;
 2 
 3 import com.example.espressodemo.MainActivity;
 4 import com.example.espressodemo.R;
 5 import com.google.android.apps.common.testing.ui.espresso.action.ViewActions;
 6 import com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions;
 7 import com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers;
 8 import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
 9 
10 import android.test.ActivityInstrumentationTestCase2;
11 
12 @SuppressWarnings("rawtypes")
13 public class EspressoTest extends ActivityInstrumentationTestCase2 {
14     @SuppressWarnings("unchecked")
15     public EspressoTest() {
16         super(MainActivity.class);
17     }
18 
19     @Override
20     protected void setUp() throws Exception {
21         super.setUp();
22         getActivity();
23     }
24 
25     public void testClickButton() throws InterruptedException {
26         onView(ViewMatchers.withId(R.id.button1)).perform(ViewActions.click()).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
27     }
28 
29     @Override
30     protected void tearDown() throws Exception {
31         super.tearDown();
32     }
33 }
Espresso小试

EspressoTest没干什么,就是判断添加的按钮是否存在

4. 配置AndroidManifest.xml文件,需要添加2段内容, application里添加uses-library,再添加一个instrumentation

1 <application
2     <uses-library android:name="android.test.runner" />
3 </application>
Espresso小试
1 <instrumentation
2         android:name="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
3         android:targetPackage="$YOUR_TARGET_APP_PACKAGE" />
Espresso小试

5. 配置Runner,如下图,选择GoogleInstrumentationTestRunner

Espresso小试

6. 运行测试,Espresso会自动启动AVD,安装被测APK,在AVD上执行测试

Espresso小试

说到AVD,还是慢,建议及早启动

Espresso小试

 

7. 更多信息

https://code.google.com/p/android-test-kit/

API查看https://android-test-kit.googlecode.com/git/docs/javadocs/apidocs/index.html 

Espresso小试

上一篇:利用nat123解决微信公众号对接服务器必须为80端口的问题


下一篇:本人的微信订阅号