src/androidtest 和 src/test 文件夹有什么区别?

What's the difference between src/androidtest and src/test folders?

在一个项目中,在AndroidStudio中,默认有两个test文件夹。

第一个是src/androidTest。此文件夹已存在于先前版本的 Android Studio 中。不过,现在有一个新的测试文件夹,默认情况下,src/test,以及新的依赖,testCompile 'junit: junit: 4.12' in build.gradle

我使用哪个文件夹进行测试?两者有什么区别?

src/androidTest 用于涉及 android 检测的单元测试。

src/test 用于不涉及 android 框架的纯单元测试。您可以在此处 运行 进行测试,而无需 运行 在真实设备或模拟器上进行测试。

您可以使用这两个文件夹。使用第一个来测试使用 Android 框架的代码。使用第二个测试纯 java 类 的代码。编写测试的方法几乎相同。

更多信息在这里:http://developer.android.com/tools/testing/testing_android.html

关于 android 测试的重要信息来源是开发者页面 Best Practices for Testing:

  • Local unit tests (/src/test/java/)

Unit tests that run locally on the Java Virtual Machine (JVM). Use these tests to minimize execution time when your tests have no Android framework dependencies or when you can mock the Android framework dependencies.

  • Instrumented tests (/src/androidTest/java/)

Unit tests that run on an Android device or emulator. These tests have access to Instrumentation information, such as the Context of the app you are testing. Use these tests when your tests have Android dependencies that mock objects cannot satisfy.

Android Studio 中的一个典型项目包含两个放置测试的目录。

1。仪器化测试 (/src/androidTest/java/)

androidTest 目录应包含 运行 在真实或虚拟设备上的测试。此类测试包括集成测试、end-to-end 测试,以及仅靠 JVM 无法验证应用功能的其他测试。

通过仪器测试,我们能够验证需要真实设备的应用程序逻辑,因此我们主要会验证 UI。我们还将使用 JUnit 并添加 Espresso.


2。单元测试 (/src/test/java/)

test 目录应该包含 运行 在您本地机器上的测试, 例如单元测试。

单元测试用于验证业务逻辑是否正常工作,而无需使用真实的 device.e。我们将使用 JUnithamcrestmockito-kotlin 来实现这一点。


More info read this article

Android 测试 src 文件夹测试与 androidtest

src/test - 单元测试

src/androidtest - Android 仪器测试