在用Android studio 创建一个新项目的时候,会默认生成三个目录(每个目录都被称为是Source set(源码集合)):
app/src * ├── androidTest/java (仪器化单元测试、UI测试,比如Espresso) * ├── main/java (业务代码) * └── test/java (本地单元测试,Junit4、mockito、Robolectric)
这个 androidTest 目录包含了触摸屏幕或者检查在屏幕上显示了什么的测试用例 ,在test目录下,我们最主要是进行单元测试,比如测试一个函数的正确性等等。
But test directory is not only for Unit test. You can also write Integration test like HTTP call. Even you can UI test
in test
directory using Robolectric
library.(It's really fast rather than Espresso)
但是, 这个test目录并不是只能进行单元测试,我们也可以写一些诸如HTTP请求的集成测试。我们甚至可以进行UI测试,通过使用Robolectric类库(实验表明,Robolectric比使用
Espresso要快的多)。
了解完上面这些基础知识之后,那么
testImplementation 和androidTestImplementation 到有什么区别呢?
- testImplementation : 为
test
source set 添加依赖 - androidTestImplementation : 为
androidTest
source set 添加依赖
比如:
//本地化测试 // Required -- JUnit 4 framework testImplementation 'junit:junit:4.12' // Optional -- Mockito framework(可选,用于模拟一些依赖对象,以达到隔离依赖的效果) testImplementation 'org.mockito:mockito-core:3.1.0' //仪器化测试 //Google UI测试框架 androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'