Google Guide:Test Your App (个人翻译,请多指教)

来源:互联网 发布:sql distinct 效率 编辑:程序博客网 时间:2024/06/05 07:25

官方地址:https://developer.android.com/studio/test/index.html#add_a_new_test

Test Your App 测试你的app

In this document

  1. Test Types and Location
  2. Add a New Test
    1. Create instrumented test for a build variant
  3. Run a Test
  4. Change the Test Build Type

Android Studio is designed to make testing simple. With just a few clicks, you can set up a

JUnit test that runs on the local JVM or an instrumented test that runs on a device

安卓Studio很容易实现测试。只需要几个步骤,你就能初始化Junit测试在你的本地虚拟机,或运行在你的真机上进行instrumented 测试。

 Of course, you can also extend your test capabilities by integrating test frameworks such as Mockito to test Android API calls in your local unit tests, and Espresso or UI Automator to exercise user interaction in your instrumented tests.

当然,你也可以利用开源的框架扩展你的测试,像Mockito 用于测试安卓api,它可调用本地的单元测试,还有 Espresso(浓咖啡) 或者 UIAutomator 用于在你的Instrumented测试中实现人机交互练习。

This page provides the basic information about how to add new tests to your app and run them from Android Studio.

这篇文章只提供关于怎么新增一个测试到你的app和在安卓Studio中运行的基础信息。

For a more complete how-to guide for writing tests, see Getting Started with Testing.

需要更多关于如何完成编写测试的指南,请参照 Getting Strated with Testing。

Test Types and Location 测试分类与位置


The location of your test code depends on the type of test you are writing. Android Studio provides source code directories (source sets), for the following two types of tests:

你测试代码的位置取决于你要测试代码的类型.安卓Studio提供的代码资源目录(源设置),用于下面这两种test类型:

Local unit tests 本地单元测试

Located at module-name/src/test/java/.

These are tests that run on your machine's local 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.

位置在 module-name/src/test/java/. (Android中的test目录)

放在这个目录下的测试,运行在你机器本地的Java的JVM中.当你的测试在没有Android framework依赖或这当你可以mock(模拟)Anroid framework依赖时,使用这些测试可以最大限度的节约时间。(有道理哈,不需要framework层的,节约

Instrumented tests    Instrumented 测试(不知道该怎么翻译Instrumentd了,就干脆叫安卓FrameWork层得了)

Located at module-name/src/androidTest/java/.

These are tests that run on a hardware device or emulator. These tests have access to Instrumentation APIs, give you access to information such as the Context of the app you are testing, and let you control the app under test from your test code. Use these tests when writing integration and functional UI tests to automate user interaction, or when your tests have Android dependencies that mock objects cannot satisfy.

Because instrumented tests are built into a stane-alone APK, they must have an AndroidManifest.xml file. However, Gradle automatically generates this file during the build so it is not visible in your project source set. You can add your own manifest file if necessary, such as to specify a different value for `minSdkVersion` or register run listeners just for your tests. When building your app, Gradle merges multiple manifest files into one manifest.

位置位于 module-name/src/androidTest/java/.

放在这个目录下的测试用例是运行在真机或者模拟器上.这些测试用例拥有Instrumentation的 APIS权限,给你的权限信息有你要测试的app的Context,还可以通过测试代码控制app进行测试.当你编写集成到功能里的Ui测试用于自动的人机交互,或者当你的测试需要Android依赖而用mock的对象又不能满足。

因为instrumentd测试是建立在独立的apk中的,它们必须有 AndroidManifest.xml文件.然而,Gradle会在你的project资源上不显示AndroidManifest文件的时候自动地构建该文件.如果必须要该文件,那就你可以添加你自己的manifest文件,就像为minSdkVersion指定不同的值 或是 注册运行的监听器用于你的测试.当构建你的app的时候,Gradle merges 多个manifest文件合并成一个manifest.



The Gradle build interprets these test source sets in the same manner as it does for your project's app source sets, which allows you to create tests based on build variants.


When you create a new project or add an app module, Android Studio creates the test source sets listed above and includes an example test file in each. You can see them in the Project window as shown in figure 1.

当你创建一个项目或者添加一个app模块,Android Studio将创建上面列举的测试资源目录,而且每次包含一个测试文件的例子。你可以在Project窗口下看到它们,如图1所示

Figure 1. Your project's (1) instrumented tests and (2) local JVM tests are visible in either the Project view (left) or Android view (right).

图示1. 你的项目(1)instrumented测试 和 (2)JVM本地测试可以显示在项目视图(左)或者Android视图(右)之间任意一个窗口下

Add a New Test 

添加一个新的测试

To create either a local unit test or an instrumented test, you can create a new test for a specific class or method by following these steps:

无论是创建一个本地单元测试或者instrumented测试,你都可以创建一个新的测试用于指向一个类或者方法,通过下面的步骤:

  1. Open the Java file containing the code you want to test. 打开你想要测试代码的java文件
  2. Click the class or method you want to test, then press Ctrl+Shift+T (⇧⌘T) 单击你想要测试的类或者方法,然后按下Ctrl+Shift+T(mac快捷键)
  3. In the menu that appears, click Create New Test.在出现的菜单中,单击 Create New Test
  4. In the Create Test dialog, edit any fields and select any methods to generate, and then click OK. 在Create Test对话框中,编辑任意字段然后选择任意方法去生成,最后单击OK。
  5. In the Choose Destination Directory dialog, click the source set corresponding to the type of test you want to create: androidTest for an instrumented test or test for a local unit test. Then click OK.                    在Choose Destination Directory 对话框,单击你想要创建符合测试类型的资源:androidTest用于instrumented测试或者test目录用于本地的单元测试.然后单击OK。

Alternatively, you can create a generic Java file in the appropriate test source set as follows:

另外,你还可以在适当的测试资源里创建通用的Java文件,像这样做:

  1. In the Project window on the left, click the drop-down menu and select the Project view.在左侧的项目窗口,单击下拉菜单选择Projcet视图
  2. Expand the appropriate module folder and the nested src folder. To add a local unit test, expand the test folder and the nested java folder; to add an instrumented test, expand the androidTest folder and the nested java folder.展开相应的module文件夹和嵌套的src文件夹.要添加本地的单元测试,展开test文件夹和在里面的java文件夹;想要添加一个instrumented测试的话,就展开androidTest文件夹,然后再展开java文件夹
  3. Right-click on the Java package directory and select New > Java Class. 右键单击Java包目录然后选择New->Java Class
  4. Name the file and then click OK. 命名文字然后单击OK.

For more information about how to write your tests, see Building Local Unit Tests and Building Instrumented Unit Tests.

需要更多关于如何编写测试文件的信息,请看 Building Local Unit Tests 和 Building Instrumented Unit Test.

Create instrumented test for a build variant

创建instrumented测试到变种版本中(build variant)

If your project includes build variants with unique source sets, then you might want corresponding instrumented test source sets. Creating instrumented tests in source sets that correspond to your build variants helps keep your test code organized and allows you to run only the tests that apply to a given build variant.
如果你的项目包含 build variants 这种独特的来源设置,那么你可能想设置适合 的instrumented测试资源

To add a testing source set for your build variant, follow these steps:

为你的build variant添加测试资源,可以按照下面的步骤做:

  1. In the Project window on the left, click the drop-down menu and select the Project view. 在左侧的Project窗口,单击下拉菜单然后选择Project 视图。
  2. Within the appropriate module folder, right-click the src folder and click New > Directory.在合适的module文件夹内,右键单击src文件夹然后单击New > Directory.
  3. For the directory name, enter "androidTestVariantName." For example, if you have a build variant called "MyFlavor" then the directory name should be "androidTestMyFlavor." Then click OK.  对于目录的名字,输入 "androidTestVariantName" 例子,如果你的build vaiant 叫做 “MyFlavor”然后这个目录名字最好叫做“androidTestMyFlavor”,然后单击OK.
  4. Right-click on the new directory and click New > Directory. 在新建的目录下右键单击然后再单击 New >Directory
  5. Enter "java" as the directory name, and then click OK. 输入"java"用于做为目录名字,然后单击OK

Now you can add tests to this new source set by following the steps above to add a new test. When you reach the Choose Destination Directory dialog, select the new variant test source set.

现在你能按照上面的步骤添加的新的测试用例.当你到选择目录对话框这一步时, 选择设置新建test资源目录.

The instrumented tests in src/androidTest/ source set are shared by all build variants. When building a test APK for the "MyFlavor" variant of your app, Gradle combines both the src/androidTest/ and src/androidTestMyFlavor/ source sets.

这个instrumented测试在src/androidTest/下的资源是可以共享所有的build variants. 在通过你的app的“MyFlavor” variant 创建一个测试APK时,Gradle将把 src/androidTest/ 和 src/androidTestMyFlaver/ 资源设置 全部合并在一起

For example, the following table shows how instrumentation test files should reside in source sets that correspond to the app's code source sets.

举例来说,下面的表格显示怎么把instrumentation测试文件与app的代码资源相互的这么关联起来,就是他们的名字关联

Path to app class app类的路径Path to matching instrumentation test class instrumentation测试类的路径src/main/java/Foo.javasrc/androidTest/java/AndroidFooTest.javasrc/myFlavor/java/Foo.javasrc/androidTestMyFlavor/java/AndroidFooTest.java

Just as it does for your app source sets, the Gradle build merges and overrides files from different test source sets. In this case, theAndroidFooTest.java file in the "androidTestMyFlavor" source set overrides the version in the "androidTest" source set. For more information about how source sets are merged, see [Configure Your Build] (/studio/build/index.html#sourcesets).

就像上面那样设置你的app源配置,这个Gradle将从不同的test源里合并与覆盖文件.在这个测试用例里, 在“androidTestMyFlavor”源的theAndroidFooTest.java 文件配置会覆盖在“androidTest”源设置的版本。更多关于如何设置源的合并等信息,请看 /studio/build/index.html

Another reason you should use build variants for your app and test source sets is to create hermetic tests through mock dependencies. That is, you can create a product flavor for your app that contains fake implementations of dependencies (such as network requests or device sensor data that is ordinarily flaky), and then add a corresponding mock test source set. For more information, see the blog post about leveraging product flavors for hermetic testing.


Run a Test 运行测试用例


To run a test, proceed as follows:

运行测试用例,跟我的进度走:

  1. Be sure your project is synchronized with Gradle by clicking Sync Project  in the toolbar. 确定你的project进行Gradle同步,在toolbar上面点击Sync Project 图标。
  2. Run your test in one of the following ways: 以下任意方法可运行的你测试用例
    • In the Project window, right-click a test and click Run . 在Project窗口,右键单击一个用例,然后点击Run 图标
    • In the Code Editor, right-click a class or method in the test file and click Run  to test all methods in the class.在代码编辑时,在测试文件中右键单击一个类或者一个方法,单机 Run 图标可以测试 类中的所有方法.
    • To run all tests, right-click on the test directory and click Run tests .要运行所有测试用例,右键单击测试目录然后单机Run tests 图标

By default, your test runs using Android Studio's default run configuration. If you'd like to change some run settings such as the instrumentation runner and deployment options, you can edit the run configuration in the Run/Debug Configurations dialog (click Run > Edit Configurations).

在默认情况下,你用默认配置就能执行测试用例。 如果你想改变一些设置,就像instrumentation执行配置和部署选项,你可以在Run/Debug Configurations对话框中编辑运行配置。(点击 Run>Edit Configurations)。

Change the Test Build Type


By default, all tests run against the debug build type. You can change this to another build type by using the testBuildType property in your module-level build.gradle file. For example if you want to run your tests against your "staging" build type, edit the file as shown in the following snippet.

默认情况下,所有的测试用例适用于debug构建类型。你可以在你的module级别的build.gradle文件中,通过testBuildType属性,变为其它构建的类型。像这样,如果你想要在staging构建类型下再次运行测试用例,在下面展示片段里,编辑文件,走起。

android {    ...    testBuildType "staging"}
0 0
原创粉丝点击