你的位置:首页 > 软件开发 > 操作系统 > Getting Started with Testing ——开始单元测试

Getting Started with Testing ——开始单元测试

发布时间:2016-07-18 17:00:08
Android tests are based on JUnit, and you can run them either as local unit tests on the JVM or as instrumented tests on an Android device ...

Getting Started with Testing ——开始单元测试

Android tests are based on JUnit, and you can run them either as local unit tests on the JVM or as instrumented tests on an Android device. This page provides an introduction to the concepts and tools for building Android tests.

// Android的测试基于JUnit

Test Types


When using Android Studio to write any of your tests, your test code must go into one of two different code directories (source sets). For each module in your project, Android Studio includes both source sets, corresponding to the following test types:

//对于每一个项目的模块,Androidstudio都会包含这两个类型的source sets

Local unit tests
Located at module-name/src/test/java/.

These tests run on the local JVM and do not have access to functional Android framework APIs.

// 这些测试运行在本地JAVA虚拟机上,不会访问Android框架层的api

To get started, see Building Local Unit Tests.

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

These are all tests that must run on an Android hardware device or an Android emulator.

//这些测试必须运行在Android硬件设备上或者模拟器上

Instrumented tests are built into an APK that runs on the device alongside your app under test. The system runs your test APK and your app under tests in the same process, so your tests can invoke methods and modify fields in the app, and automate user interaction with your app.

//instruments test构建了一个apk和你的app一起安装,系统会让你的app和测试apk一同运行在同一个进程,这样你的测试可以调用应用中的方法并且 可以修改变量,让和应用的交互变得自动化

For information about how to create instrumented tests, see the following topics:

  • Building Instrumented Unit Tests: Build complex unit tests with Android dependencies that cannot be satisfied with mock objects.
  • Automating User Interface Tests: Create tests to verify that the user interface behaves correctly for user interactions within a single app or for interactions across multiple apps.
  • Testing App Component Integrations: Verify the behavior of components that users do not directly interact with, such as a Service or aContent Provider.
Getting Started with Testing ——开始单元测试

However, the local unit tests and instrumented tests described above are just terms that help distinguish the tests that run on your local JVM from the tests that run on the Android platform (on a hardware device or emulator). The real testing types that you should understand when building a complete test suite are described in the following table.

//然而上面描述的local unit test and instrumentes test只是帮助区分他们一个是运行在本地虚拟机,一个是运行在Android平台上。真正的测试类型的分类应该是如下表描述的:

TypeSubtypeDescription
Unit tests
Local Unit TestsUnit 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 unit testsUnit tests that run on an Android device or emulator. These tests have access to Instrumentationinformation, such as the Context of the app you are testing. Use these tests when your tests have Android dependencies that mock objects cannot satisfy.
Integration Tests
Components within your app onlyThis type of test verifies that the target app behaves as expected when a user performs a specific action or enters a specific input in its activities. For example, it allows you to check that the target app returns the correct UI output in response to user interactions in the app’s activities. UI testing frameworks like Espressoallow you to programmatically simulate user actions and test complex intra-app user interactions.
Cross-app ComponentsThis type of test verifies the correct behavior of interactions between different user apps or between user apps and system apps. For example, you might want to test that your app behaves correctly when the user performs an action in the Android Settings menu. UI testing frameworks that support cross-app interactions, such as UI Automator, allow you to create tests for such scenarios.

Test APIs


The following are common APIs used for testing apps on Android.

JUnit

You should write your unit or integration test class as a JUnit 4 test class. The framework offers a convenient way to perform common setup, teardown, and assertion operations in your test.

A basic JUnit 4 test class is a Java class that contains one or more test methods. A test method begins with the @Test annotation and contains the code to exercise and verify a single functionality (that is, a logical unit) in the component that you want to test.

The following snippet shows an example JUnit 4 integration test that uses the Espresso APIs to perform a click action on a UI element, then checks to see if an expected string is displayed.

@RunWith(AndroidJUnit4.class)    @Rule    @Test        onView(withId(R.id.textView)).check(matches(withText("Hello, World!")));  
         
        
        

原标题:Getting Started with Testing ——开始单元测试

关键词:get

get
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录