android 建测试类,Android单元测试

本文介绍了Android单元测试和Instrumentation测试的方法。单元测试方面,先在build.gradle加入JUnit4依赖,创建测试类并添加测试内容,最后运行测试。Instrumentation测试则需在build.gradle配置Espresso,编辑布局文件和代码,创建测试类并运行,可在手机屏幕看到执行动作及测试结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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选择生成的测试类的包路径

如下图:

e8f4db41579d

1.PNG

创建成功后会在app/src/test/java/me/peace/junit下生成测试类(me/peace/junit即Destionation package)

一般与javas相关的单元测试选择test/java目录,与android相关的选择androidTest/java,若缺少这两个目录请自行创建

e8f4db41579d

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

测试结果:

e8f4db41579d

3.PNG

补充

Generate选择setUp/@Before和tearDown/@After

其运行顺序是setUp->测试的方法->tearDown(即Before->Test->After,且每个Test方法都会去执行一次setUp和tearDown方法)

测试结果:

e8f4db41579d

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的布局文件,效果如图

e8f4db41579d

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 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上输出测试结果。

e8f4db41579d

6.PNG

Addition

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值