目录

  • 单元测试

  • UI 测试

原文链接: Unit and UI Testing in Android Studio

2. UI 测试

  • 配置

  • 编码

  • 测试

2.1 配置

2.1.1 IDE 配置
Build Variants => Test Artifact => Android Instrumentation Tests

2.1.2 build.gradle

apply plugin: 'com.android.application'android {compileSdkVersion 22buildToolsVersion "22.0.1"defaultConfig {applicationId "com.example.testing.testingexample"minSdkVersion 15targetSdkVersion 22versionCode 1versionName "1.0"//ADD THIS LINE:testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}//ADD THESE LINES:packagingOptions {exclude 'LICENSE.txt'}
}dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])compile 'com.android.support:appcompat-v7:22.0.0' //← MAKE SURE IT’S 22.0.0testCompile 'junit:junit:4.12'//ADD THESE LINES:androidTestCompile 'com.android.support.test:runner:0.2'androidTestCompile 'com.android.support.test:rules:0.2'androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
}

重要:由于一些依赖版本冲突,你需要确认com.android.support:appcompat-v7库的版本号是22.0.0,像上面的代码片段一样。

2.2 编码

2.2.1 为 app 添加交互

activity_main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><TextViewandroid:id="@+id/textView"android:text="@string/hello_world" android:layout_width="wrap_content"android:layout_height="wrap_content" /><EditTextandroid:hint="Enter your name here"android:id="@+id/editText"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/textView"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="Say hello!"android:layout_below="@+id/editText"android:onClick="sayHello"/>
</RelativeLayout>

MainActivity.java

public void sayHello(View v){TextView textView = (TextView) findViewById(R.id.textView);EditText editText = (EditText) findViewById(R.id.editText);textView.setText("Hello, " + editText.getText().toString() + "!");
}

2.2.2 编写测试

androidTest 包中新建测试类
MainActivityInstrumentationTest.java

import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.action.ViewActions;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityInstrumentationTest {private static final String STRING_TO_BE_TYPED = "Peter";@Rulepublic ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);@Testpublic void sayHello(){onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); //line 1onView(withText("Say hello!")).perform(click()); //line 2String expectedText = "Hello, " + STRING_TO_BE_TYPED + "!";onView(withId(R.id.textView)).check(matches(withText(expectedText))); //line 3}}

2.3 进行测试

测试文件上点右键 Run 测试就可以了。
稍后会看到手机自动运行 app,并按照测试代码中写的进行自动测试。

2.4 测试结果

使用 Android Studio 进行测试 (二) UI 测试相关推荐

  1. android espresso跨程序,Android中使用Espresso进行UI测试

    在使用Android Studio创建项目时,Android Studio一般都会自动创建测试相关的包名和类,可见测试在Android Studio开发中具有很重要的地位了,但我却从来没有使用过. 今 ...

  2. android如何用真机测试,android studio如何使用真机测试app

    在使用android studio进行项目的调试的时候,模拟器真的有点慢,有时还很卡,大家可能还是更想使用真机进行调试,那么下面来看看android studio如何使用真机测试app的教程. 第一大 ...

  3. android studio for mac无法真机调试,Android studio for mac真机测试

    mac系统真机测试需要配置adb环境. 配置方法: 1.首先找到adb的文件目录,打开Android studio启动界面点击Configure 选择project defaults 选择projec ...

  4. 软件测试基础 按照测试对象划分 界面测试(UI测试.响应式页面 可靠性测试 容错性测试 文档测试 平台测试 易用性测试等

    软件测试进阶 1.APP 的测试 2. 按照测试对象划分 2.1界面测试(UI测试) 3.响应式页面 4.可靠性测试 5.容错性测试 6.文档测试 7.平台测试: 7.1 PC: 7.2.手机端: 8 ...

  5. Android studio使用心得(二)— 打包签名apk发布

    1.-–Android Studio菜单   Build->Generate Signed APK 2.--Create new.. 3.---跟eclipse里面一样,添加keystore 信 ...

  6. Android Studio 开发(二)问题

    目录 一.Mac下de的java路径 二.android studio的编译命令 三.在cocos3.1.5中 android-studio中的build-cfg.json找不到了 四.导入第三方ja ...

  7. Android Studio系列(二)使用AS开发/调试整个android系统源代码(不定时更新)

    转载自:http://blog.csdn.net/aaa111/article/details/43227367 一.修改AS的配置 由于Android源码太大了,在过导入源码和后续工作中,AS需要占 ...

  8. Android Studio系列(二)使用Android Studio开发/调试整个android系统源代码(不定时更新)

    本文是以源码中development/tools/idegen/README作为指导文档,给出了使用Android Studio导入Android源码的方法步骤. 环境: Ubuntu 12.04,o ...

  9. zxing集成到Android Studio中实现二维码扫一扫功能

    详情请看:Android Studio集成Zxing扫一扫 但是,上面那篇博客只有有一个扫一扫功能,而且在低分辨率手机上会出现变形的问题.扫描速度也比较慢,功能不是很全,没有闪光灯,生成二维码,解析二 ...

最新文章

  1. 论坛第20000名幸运儿是谁?
  2. think in java笔记_Thinking in java读书笔记 PDF 下载
  3. SpringMVC上传文件以流方式判断类型附常用类型
  4. 自动化WiFI钓鱼工具——WiFiPhisher源码解读
  5. 【BZOJ3894】文理分科
  6. linux sed 空间模式,整理:SED的模式空间与缓冲区及n,N,d,D,p,P,h,H,g,G,x解析...
  7. Flask 富文本编辑器
  8. devops_DevOps专业人员如何成为安全冠军
  9. Python实现基于HDFS的云盘系统
  10. labelme批量转换json
  11. GO语言学习之路22
  12. 侧信道攻击,从喊666到入门之——Unicorn的环境构建
  13. OEA ORM 框架中的冗余属性设计
  14. python写TCP协议
  15. php聊天功能界面,php实现聊天室功能完整代码
  16. roslyn生成html,通过Roslyn将字符串生成可以执行的C#代码
  17. elasticsearch启动报错:master not discovered yet
  18. 膨胀卷积 / 空洞卷积(Dilated convolution)
  19. 【RISC-V】SiFive Unmatched开发板开发手记02
  20. 洛谷 P1645 序列 贪心

热门文章

  1. 这家研究院太年轻,竟跟世界级选手“叫板”
  2. GitHub的AI程序员“抄袭”算法大神代码,连原版注释都抄上了
  3. 北航成AAAI 2021最大赢家,两篇一作斩获最佳论文、提名奖,研究皆与Transformer相关...
  4. 李开复Bengio大咖对话:下一阶段AI最大机遇在这4个领域
  5. 一张照片生成积木的你!5个在校生2个月做的AI项目,李开复看了赞不绝口
  6. 只要一句话、一段文字,想让奥巴马说啥他就说啥
  7. 旷视唐文斌:你到底给谁创造了什么样的价值?这是AI产品的灵魂拷问丨MEET2020...
  8. Spring4整合Hibernate4出现的错误的解决
  9. 句柄(Handle)
  10. 采集音频和摄像头视频并实时H264编码及AAC编码[转]