Android单元测试
创建android studio工程,在app目录下的build.gradle中加入JUnit4的依赖。
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.0.0-beta1' }
创建Unit Test
创建测试类Tools
public class Tools { public double sum(double a ,double b){ return 0; } public double abs(double value){ return 0; } }
在待测试的类(Tools)代码编辑器内,右键选择Go to -> Test
或者使用快捷键Ctrl + Shift + T,Create New Test 。
Testing library选择JUnit4
Generate选择setUp/@Before
Destionation package选择生成的测试类的包路径
如下图:
1.PNG
创建成功后会在app/src/test/java/me/peace/junit下生成测试类(me/peace/junit即Destionation package)
一般与javas相关的单元测试选择test/java目录,与android相关的选择androidTest/java,若缺少这两个目录请自行创建
2.PNG
在这个测试类中加入具体的测试内容
public class ToolsTest { private Tools mTools; @Before public void setUp() throws Exception { mTools = new Tools(); System.out.println("Before - setUp"); } @Test public void testSum() throws Exception { assertEquals(48d,mTools.sum(46d,2d),0); System.out.println("Test - testSum"); } @Test public void testAbs() throws Exception { assertEquals(46,mTools.abs(-46),0); System.out.println("Test - testAbs"); } }
实现Tools类
public class Tools { public double sum(double a ,double b){ return a + b; } public double abs(double value){ return Math.abs(value); } }
Run Test Unit
右键ToolsTest类,选择Run->ToolsTest
测试结果:
3.PNG
补充
Generate选择setUp/@Before和tearDown/@After
其运行顺序是setUp->测试的方法->tearDown(即Before->Test->After,且每个Test方法都会去执行一次setUp和tearDown方法)
测试结果:
4.PNG
使用Espresso进行Instrumentaion Test
在build.gradle中配置Espresso,在defaultConfig中添加
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
在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'
完整的内容
apply plugin: 'com.android.application' android { compileSdkVersion 24 buildToolsVersion "24.0.2" defaultConfig { applicationId "me.peace.espressostudy" minSdkVersion 15 targetSdkVersion 24 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.0.0-beta1' androidTestCompile 'com.android.support:support-annotations:24.0.0-beta1' 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' }
编辑layout的布局文件,效果如图
5.PNG
在MainActivity中加入如下代码
//按钮点击事件处理 public void print(View v){ TextView textView = (TextView)findViewById(R.id.textview); EditText editText = (EditText)findViewById(R.id.edittext); textView.setText("message => " + editText.getText().toString().trim()); }
在androidTest/java目录下创建MainActivityTest并且添加如下代码
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 MainActivityTest{ private String mExpectedString = "Hello"; @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class); @Test public void print(){ onView(withId(R.id.edittext)).perform(typeText(mExpectedString),closeSoftKeyboard()); onView(withText(R.string.print)).perform(click()); onView(withId(R.id.textview)).check(matches(withText("message => " + mExpectedString))); } }
选择MainActivityTest,右键Run 'MainActivityTest'
手机屏幕就会看到被执行的动作,最后会看到as上输出测试结果。
来自:http://www.jianshu.com/p/e8f4db41579d
- 文章2300
- 用户1336
- 访客10862077
真正的成功是激励他人行动。
语法错误: 意外的令牌“标识符”
全面理解Gradle - 定义Task
Motrix全能下载工具 (支持 BT / 磁力链 / 百度网盘)
谷歌Pixel正在开始起飞?
获取ElementUI Table排序后的数据
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is
亲测!虚拟机VirtualBox安装MAC OS 10.12图文教程
华为手机app闪退重启界面清空log日志问题
android ndk开发之asm/page.h: not found
手机屏幕碎了怎么备份操作?
免ROOT实现模拟点击任意位置
新手必看修改DSDT教程
thinkpad t470p装黑苹果系统10.13.2