简体   繁体   English

android build.gradle中testImplementation和androidTestImplementation的区别

[英]android difference between testImplementation and androidTestImplementation in build.gradle

I am using andorid studio 3.2 and I am new to testing.我正在使用 andorid studio 3.2,我是测试新手。 I want to add a testing library but I am not getting what should i write in the dependencies section testImplentation or androidTestImplementation .我想添加一个测试库,但我没有得到我应该在依赖项部分testImplentationandroidTestImplementation 中写什么。 What is the difference between these two?这两者有什么区别?

If you create android project using Android Studio.如果您使用 Android Studio 创建 android 项目。 You can find three directories like following.您可以找到如下三个目录。 (each directory called Source Set ) (每个目录称为Source Set

  • app/src/main应用程序/源代码/主
  • app/src/androidTest : for android UI test. app/src/androidTest : 用于安卓 UI 测试。 It needs virtual or real device.它需要虚拟或真实设备。 (Espresso, UI automator) (Espresso, UI 自动机)
  • app/src/test : for android independent test. app/src/test : 用于安卓独立测试。

The androidTest directory contains test cases that touch the screen or check what is displayed on the screen. androidTest目录包含触摸屏幕或检查屏幕上显示内容的测试用例。 In the test directory, we mainly do unit tests such as testing one function.test目录下,我们主要做unit tests比如测试一个功能。

But test directory is not only for Unit test.但是测试目录不仅用于单元测试。 You can also write Integration test like HTTP call.您还可以编写集成测试,如 HTTP 调用。 Even you can UI test in test directory using Robolectric library.(It's really fast rather than Espresso)甚至你可以使用Robolectric库在test目录中进行UI test 。(它真的比 Espresso 快)

So what is testImplementation and androidTestImplementation ?那么什么是testImplementationandroidTestImplementation呢? (each called Configuration ) (每个都称为Configuration

  • testImplementation : adds dependency for test source set testImplementation :为test源集添加依赖项
  • androidTestImplementation : adds dependency for androidTest source set androidTestImplementation : 为androidTest源集添加依赖androidTest

See this articles for details.有关详细信息,请参阅此文章。

implementation —The dependency is available in all source sets, including the test source sets. implementation - 依赖项在所有源集中可用,包括测试源集。

testImplementation —The dependency is only available in the test source set. testImplementation — 依赖项仅在测试源集中可用。

androidTestImplementation —The dependency is only available in the androidTest source set. androidTestImplementation — 依赖项仅在androidTest源集中可用。

Android source sets are : Android源集是:

main : Contains your app code. main :包含您的应用程序代码。 This code is shared amongst all different versions of the app you can build (known as build variants)此代码在您可以构建的所有不同版本的应用程序之间共享(称为构建变体)

androidTest : Contains tests known as instrumented tests. androidTest :包含称为插桩测试的测试。

test : Contains tests known as local tests. test :包含称为本地测试的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM