Android 单元测试之Espresso - Google官方UI测试框架
2016-08-29 09:02
573 查看
Android 单元测试之Espresso - Google官方UI测试框架
Android 单元测试之JUnit和MockitoAndroid 单元测试之Roboletric 环境配置
Android 单元测试之Roboletric的简单使用
Android 单元测试之Roboletric RxJava、Retrofit、访问真实网络、虚拟服务器
Android 单元测试之Espresso - Google官方UI测试框架
Espresso是Google官方推出的Instrumentation UI测试框架,在API支持方面有着天然的优势,在推出后很大程度上将替代Robotium。
Espresso官方文档
配置
android { ... defaultConfig { ... testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' } packagingOptions { exclude 'LICENSE.txt' } } dependencies { ... androidTestCompile 'com.android.support.test:runner:0.5' androidTestCompile 'com.android.support.test:rules:0.5' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') { exclude module: 'support-annotations' exclude module: 'design' exclude module: 'recyclerview-v7' exclude module: 'support-v4' } androidTestCompile ('com.android.support.test.espresso:espresso-idling-resource:2.2.2') { exclude module: 'support-annotations' } compile 'com.android.support:design:24.1.1' }
构建时,可能会出现annotations 相关报错,加上下面的就好
android { configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:23.0.1' } }
原因是目前 Espresso 对Android 部分代码只支持到 23.0.1 而我们项目中有些android版本为23.1.1,所以出现了报错
最简单的使用
新建布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/greet_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="greet" android:text="@string/greet" /> <TextView android:id="@+id/greeting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="28sp" /> </LinearLayout>
新建Activity
public class MainActivity extends AppCompatActivity { private TextView greetingView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle(R.string.app_name); greetingView = (TextView) findViewById(R.id.greeting); } public void greet(View v) { greetingView.setText(R.string.hello); } }
新建单元测试
在androidTest中新建@RunWith(AndroidJUnit4.class) public class MainActivityTest { @Rule public ActivityTestRule<MainActivity> activityRele = new ActivityTestRule<>(MainActivity.class); @Test public void greet() { onView(withId(R.id.greeting)).check(matches(withText(""))); onView(withId(R.id.greet_button)).check(matches(withText(R.string.greet))).perform(click()); onView(withId(R.id.greeting)).check(matches(withText(R.string.hello))); } @Test public void toolbarTitle() { CharSequence title = InstrumentationRegistry.getTargetContext().getString(R.string.app_name); matchToolbarTitle(title); } private static ViewInteraction matchToolbarTitle(CharSequence title) { return onView(isAssignableFrom(Toolbar.class)) .check(matches(withToolbarTitle(is(title)))); } private static Matcher<Object> withToolbarTitle(final Matcher<CharSequence> textMatcher) { return new BoundedMatcher<Object, Toolbar>(Toolbar.class) { @Override public boolean matchesSafely(Toolbar toolbar) { return textMatcher.matches(toolbar.getTitle()); } @Override public void describeTo(Description description) { description.appendText("with toolbar title: "); textMatcher.describeTo(description); } }; } }
然后,运行,发现测试通过
其他
源码Demo
相关文章推荐
- Android UI测试框架Espresso以及代码覆盖率jacoco的使用
- Android UI Design Tips(Google官方出品:Android UI 设计指导)——第一部分
- Google C++单元测试框架(Gtest)系列教程之三——测试固件(Test fixture)
- android下测试方法及junit单元测试框架配置方法
- 【转】玩转Google开源C++单元测试框架Google Test系列(gtest)之五 - 死亡测试
- [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)
- Android UI Design Tips(Google官方出品:Android UI 设计指导)——第一部分
- Android自动化测试中Espresso框架学习笔记(1)
- Android UI,界面辅助设置工具,可随意拖动控件,比google官方提供的方便
- Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之五 - 死亡测试
- Google C++单元测试框架(Gtest)系列教程之三——测试固件(Test fixture)
- Android UI 测试框架
- Android 中Junit 测试框架(单元测试)
- 玩转 Google开源C++单元测试框架Google Test系列(gtest)之五 - 死亡测试(转载)
- Google C++单元测试框架(Gtest)系列教程之二——断言、函数测试
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之五 - 死亡测试
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之五 - 死亡测试
- Google推出iOS功能性UI测试框架EarlGrey
- [置顶]android下测试方法及junit单元测试框架配置方法