Android Studio单元测试入门

概念

单元测试(英语:Unit Testing)又称为模块测试, 是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作。

意义

  • 减少bug
  • 快速定位bug
  • 提高代码质量
  • 减少调试时间

安卓单元测试

测试类型和位置

1.本地单元测试

代码位于 module-name/src/test/java/中。

这些测试在计算机的本地 Java 虚拟机 (JVM) 上运行。

2.仪器测试

代码位于 module-name/src/androidTest/java/中。

这些测试在硬件设备或模拟器上运行。

测试

1.打开想测试的代码的 Java 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MathUtil {

public static int max(int a, int b, int c) {
if (a > b) {
if (a > c) {
return a;
} else {
return c;
}
} else {
if (b > c) {
return b;
} else {
return c;
}
}
}

}

2.选中测试的类或方法,右击Go To > Test

3.在出现的菜单中,点击 Create New Test。

4.在 Create Test 对话框中,编辑任何字段并选择任何要生成的方法,然后点击 OK。

5.在 Choose Destination Directory 对话框中,点击想创建的测试类型对应的源集。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class MathUtilTest {
@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void max() throws Exception {
assertEquals(3, MathUtil.max(1, 2, 3)); // add this test
}

}

运行测试

1.点击工具栏中的 Sync Project ,确保项目已同步。

2.在 Project 窗口中,右键点击测试,然后点击 Run。

1
1 test passed - 2ms

测试完成.

Mockito测试

Mockito是一个优秀的用于单元测试的mock框架。

mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。

1.添加Mockito依赖

1
2
3
4
5
6
7
8
9
dependencies {
// junit
testCompile 'junit:junit:4.12'

// required if you want to use Mockito for unit tests
testCompile 'org.mockito:mockito-core:2.19.0'
// required if you want to use Mockito for Android tests
androidTestCompile 'org.mockito:mockito-android:2.19.0'
}

2.测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import org.junit.Test;

import java.util.List;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class ExampleTest {

@Test
public void justTest() throws Exception {
// mock creation
List mockedList = mock(List.class);

mockedList.add("one");
mockedList.clear();

verify(mockedList).add("one");
verify(mockedList).clear();
}

}

Github地址:https://github.com/mockito/mockito
官网:http://mockito.org

Espresso单元测试

Espresso是一个Google官方提供的Android应用UI自动化测试框架。

  • 收录在Android Testing Supporting Library底下的测试框架
  • 模拟用户操作
  • 自动等待,直到UI线程Idle,才会执行测试代码

1.build.gradle例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apply plugin: 'com.android.application'

android {
compileSdkVersion 26

defaultConfig {
applicationId "com.my.awesome.app"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}

dependencies {
// App's dependencies, including test
implementation 'com.android.support:support-annotations:27.1.1'

// Testing-only dependencies
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

2.测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

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.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule<>(
SettingActivity.class);

//检验tv_cache的本文内容是否匹配“清除缓存”
@Test
public void checkCacheText() throws Exception{
onView(withId(R.id.tv_cache)).check(matches(withText("清除缓存")));
}
}

右击Run,Select Deployment Target即可

遇到的问题

Execution failed for task ‘:app:preDebugAndroidTestBuild’

1
2
3
Error:Execution failed for task ':app:preDebugAndroidTestBuild'.
> Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (27.1.0) and test app (27.1.1) differ.
See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.

see: https://d.android.com/r/tools/test-apk-dependency-conflicts.html

在dependencies中添加resolutionStrategy.force

1
2
3
4
5
6
7
8
9
10
dependencies {

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:27.1.0'
}
}

后语

结合Jacoco代码覆盖率单元测试.

  • 分析未覆盖部分的代码
  • 检测出程序中的废代码
  • 提升代码质量

相关主题