Android单元测试
来源:互联网 发布:手机禁网软件 编辑:程序博客网 时间:2024/06/11 01:20
代码案列
package com.example.android.testing.espresso.BasicSample;import org.junit.Before;import org.junit.Rule;import org.junit.Test;import org.junit.runner.RunWith;import android.support.test.rule.ActivityTestRule;import android.support.test.runner.AndroidJUnit4;...@RunWith(AndroidJUnit4.class)@LargeTestpublic class ChangeTextBehaviorTest { private String mStringToBetyped; **第一步:把要测试的Activity作为ActivityTestRule的泛型,比如我这里是MainActivity** @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>( MainActivity.class);**before注解表示在进行测试之前,可以做一些初始化的动作,也可以不写** @Before public void initValidString() { // Specify a valid string. mStringToBetyped = "Espresso"; } @Test public void changeText_sameActivity() { // Type text and then press the button. onView(withId(R.id.editTextUserInput)) .perform(typeText(mStringToBetyped), closeSoftKeyboard()); onView(withId(R.id.changeTextBt)).perform(click()); // Check that the text was changed. onView(withId(R.id.textToBeChanged)) .check(matches(withText(mStringToBetyped))); }}
对UI界面的测试,主要就是找到View,然后执行action,最后check 结果是否正确
怎么找到view呢
为了找到view,你需要调用onView方法并且传递一个指定了view的viewMatcher的对象。详细指定一个View Matcher的细节会在后面讲解。onView方法执行完毕,会返回一个ViewInterface的对象,你就可以拿这个对象和view进行交互测试。通过onView方法找不到布局中的recyclerView,需要通过onData方法来实现,后天会详细介绍。To find the view, call the onView() method and pass in a view matcher that specifies the view that you are targeting. This is described in more detail in Specifying a View Matcher. The onView() method returns a ViewInteraction object that allows your test to interact with the view. However, calling the onView() method may not work if you want to locate a view in an RecyclerView layout. In this case, follow the instructions in Locating a view in an AdapterView instead**下面的代码片段,向你展示写一个简单的测试,访问EditText控件,向它里面输入text文本,输入完毕关闭虚拟键盘,最后执行按钮的点击的事件**The following code snippet shows how you might write a test that accesses an EditText field, enters a string of text, closes the virtual keyboard, and then performs a button click.
public void testChangeText_sameActivity() { // Type text and then press the button. onView(withId(R.id.editTextUserInput)) .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); onView(withId(R.id.changeTextButton)).perform(click()); // Check that the text was changed. ...}
0 0
- android 单元测试
- Android单元测试
- Android单元测试
- android单元测试
- Android单元测试
- Android单元测试
- android单元测试
- android单元测试
- Android单元测试
- Android单元测试
- android 单元测试
- android 单元测试
- Android--单元测试
- android单元测试
- Android单元测试
- Android单元测试
- Android单元测试
- ANDROID 单元测试
- 线上问题排查命令总结
- Qcom android L ro.sf.lcd_density属性修改
- J. City traffic tarjan缩点 11TH BUPT Collegiate Programming Contest
- Linux中的软件下载(g++,gdb,TagList插件,WinManager ,ctags)
- MySQL查看、创建和删除索引的方法(转载)
- Android单元测试
- 自定义WEB MVC框架 二 简单扫描和映射绑定
- 基于STM32CubeMX创建STM32L496ZGTx的工程
- virtualBox下ubuntu系统安装redis非关系型数据库
- Python代码风格规范
- fineui Get*****Reference 函数
- Caused by: java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
- 指针
- Python进阶(二十九)-Python时间&日期&时间戳处理