Android studio run all tests

Running Tests In Android Studio

In this chapter, let us see how to run tests using Android studio.

Every android application has two type of tests −

Functional / Unit tests

Functional test does not need the actual android application to be installed and launched in the device or emulator and test the functionality. It can be launched in the console itself without invoking the actual application. However, instrumentation tests need the actual application to be launched to test the functionality like user interface and user interaction. By default, Unit tests are written in src/test/java/ folder and Instrumentation tests are written in src/androidTest/java/ folder. Android studio provides Run context menu for the test classes to run the test written in the selected test classes. By default, an Android application has two classes − ExampleUnitTest in src/test folder and ExampleInstrumentedTest in src/androidTest folder.

To run the default unit test, select ExampleUnitTest in the Android studio, right-click on it and then click the Run ‘ExampleUnitTest’ as shown below,

Run Unit Test

This will run the unit test and show the result in the console as in the following screenshot −

Unit Test Success

To run the default instrumentation test, select ExampleInstrumentationTest in the android studio, right-click it and then click the Run ‘ExampleInstrumentationTest’ as shown below,

Run Instrumentation Test

This will run the unit test by launching the application in either device or emulator and show the result in the console as in the following screenshot −

Источник

Developing Android unit and instrumentation tests — Tutorial

This tutorial describes how to write unit and instrumentation tests for your Android application. It describes how to execute these tests via Android studio and Gradle. This tutorial assumes that you are familiar with Android programming in general.

1. Introduction into Android testing

1.1. Testing Android applications

Android applications run on a variety of devices. Also the Android framework and the surrounding open source frameworks evolve at a high speed. To ensure that you application works well, it is import to write software tests. This helps you to enhance and maintain the Android application.

Unit testing for Android can be classified into:

Local unit tests — tests which can run on the JVM.

Instrumented unit tests — tests which require the Android system.

Local unit tests run much faster compared to the time required to deploy and run tests on an Android device. Prefer writing local unit tests and only run tests on Android, if you require a real Android system.

If you write local unit test and have dependencies to Android API, you need to replace them, e.g., via a mocking framework like Mockito.

1.2. What to test on Android applications

Your test should focus on the business logic of your application. A good rule of thumb is to have the following distribution of tests:

70-80 % unit tests to ensure stability of your code basis

20-30 % functional tests to ensure that the application really works

some cross functional tests if your application integrates intensively with other Application components

You should test your application at least on one device with the lowest possible configuration. In addition you should test on one device with the highest available configuration, e.g., pixel density, screen resolution to ensure that it works fine on these devices.

1.3. Tooling support for Android testing

The Android Testing Support library (ATSL) project from Google provides tooling for Android testing. For example, it ( AndroidJUnitRunner ).

If you run local unit tests, a special version of the android.jar (also known as the Android mockable jar ) is created by the tooling. This modified JAR file is provided to the unit test so that all fields, methods and classes are available. Any call to the Android mockable JAR results, by default, in an exception, but you can configure Android to return default values. See Activating default return values for mocked methods in android.jar for details.

The library provides a JUnit 4-compatible test runner ( AndroidJUnitRunner ), the Espresso test framework and the UI Automator test framework. Espresso test framework can be used to test the User Interface of your application. UI Automator allows to write cross application functional tests.

Читайте также:  Яндекс диск для андроид синхронизация папок

AndroidJunitRunner provides access to the instrumentation API, via the InstrumentationRegistery .

InstrumentationRegistry.getInstrumentation() , returns the Instrumentation currently running.

InstrumentationRegistry.getContext() , returns the Context of this Instrumentation’s package.

InstrumentationRegistry.getTargetContext() , returns the application Context of the target application.

InstrumentationRegistry.getArguments() , returns a copy of arguments Bundle that was passed to this Instrumentation. This is useful when you want to access the command line arguments passed to the instrumentation for your test.

It also gives access to the life cycle via the ActivityLifecycleMonitorRegistry .

1.4. Android project organization for tests

The following is the default directory structure for your application and test code:

app/src/main/java — for your source code of your main application build

app/src/test/java — for any unit test which can run on the JVM

app/src/androidTest/java — for any test which should run on an Android device

If you follow this conversion, the Android build system runs your tests on the correct target (JVM or Android device).

If you receive the following error message: «error duplicate files in path. Path in archive: LICENSE.txt» you can add the following to your app/gradle.build file.

2. Android unit testing

2.1. Unit testing in Android

A unit test verifies in isolation the functionality of a certain component. For example, assume a button in an Android activity is used to start another activity. A unit test would determine, if the corresponding intent was issued, not if the second activity was started

A unit tests for an Android application can be:

local unit test — which runs on the JVM

Android unit test — which runs on the Android runtime

If they run on the JVM, they are executed against a modified version of the android.jar Android library. In this version all final modifiers have been stripped off. This makes it easier to use mocking libraries, like Mockito.

The local unit tests of an Android project should be located in the app/src/test folder.

2.2. Required dependencies in the Gradle build file

To use JUnit tests for your Android application, you need to add it as dependency to your Gradle build file.

2.3. Running the unit tests

2.3.1. Using Gradle

Run your unit tests with the gradlew test command.

2.3.2. Using Android Studio

To run a unit test, right-click on your test class in the Project window and select Run.

2.4. Location of test reports

The Test reports are created in the app/build/reports/tests/debug/ directory. The index.html gives an overview and links to the individual test pages.

2.5. Activating default return values for mocked methods in android.jar

By default, all calls to methods in the modified android.jar file throw exceptions. This default should ensure that your unit tests only test your code and do not depend on any particular behavior of the Android platform. If you want to configure a certain behavior, you can use a mocking framework to replace these call.

You can also instruct the Gradle build system to return default values for method calls in the `android.jar`with the following configuration in your Gradle build file.

3. Exercise: Create unit test

In this exercise you learn how to create a simple JUnit4 test for an Android project.

3.1. Preparation: Create Android project

Create the Android project as described in Android temperature converter.

3.2. Add JUnit dependency

Ensure you have the dependency to Junit in your app/build.gradle file.

3.3. Create test

In your app/src/test directory create the following two test methods for the ConverterUtil class.

3.4. Run unit tests

Ensure your unit tests are correctly implemented by running test tests. They should run successfully.

4. Writing tests to run on the Android device

4.1. Instrumentation tests

The Android testing API provides hooks into the Android component and application life cycle. These hooks are called the instrumentation API and allow your tests to control the life cycle and user interaction events.

Under normal circumstances your application cannot control the life cycle events and the user drives the application. For example, if Android creates your activity the onCreate() method is called. Or if the user presses a button your corresponding code is called. Via instrumentation you can control these events via your test code. For example, your instrumentation test can start the activity. Afterwards, it can call the finish() and restart the activity to test if the instance state of the activity is correctly restored.

Instrumented unit tests are unit tests that run on Android devices and emulators instead of running on the Java virtual machine. These tests have access to the real device and its resources and are useful to unit test functionality which cannot be easily mocked by mocking frameworks. An example is a test which validates a Parcelable implementation.

An instrumentation-based test class allows you to send key events (or touch events) to the application under test.

With user interface testing framework like Espresso, the developer rarely has to use the instrumentation API directly.

4.2. How the Android system executes tests

The InstrumentationTestRunner is the base test runner for Android tests. This test runner starts and loads the test methods. Via the instrumentation API it communicates with the Android system. If you start a test for an Android application, the Android system kills any process of the application under test and then loads a new instance. It does not start the application, this is the responsibility of the test methods. The test method controls the life cycle of the components of the application.

Читайте также:  Wipe partition data android recovery

The test runner also calls the onCreate() method of the application and activity under test during its initialization.

4.3. Mocking objects in Android

The mocking framework Mockito can also be used for instrumentation tests. This allows you to replace parts of the Android system which are not interesting for the test. The Android framework provided in the past specialized mocking classes but these are not necessary anymore.

4.4. Location of instrumentation tests

As described in Android project organization for tests the unit tests of an Android project should be located in the app/src/androidTest/java folder.

4.5. Define dependencies and testInstrumentationRunner in the Gradle build file

To use JUnit tests for your Android application you need to add the dependency to JUnit to your Gradle build file. You also need to specify android.support.test.runner.AndroidJUnitRunner as testInstrumentationRunner in the build file.

4.6. Using the @RunWith(AndroidJUnit4.class)

It is also recommended annotating the test with the @RunWith(AndroidJUnit4.class) annotation. AndroidJUnit4 extends JUnit4, so if you use pure Junit4 syntax and ActivityTestRule it is not required. But you need it, if you want to run, i.e., Espresso tests with ActivityTestRule and JUnit4.

4.7. Run the tests on Android

Run your local unit tests via Gradle with the gradlew connectedCheck command.

To run your local unit tests via Android Studio, right-click on your test class in the Project window and select Run.

4.8. Location of test reports

The test reports are created in the app/build/reports/androidTests/connected/ directory. The index.html gives an overview and links to the individual test pages.

4.9. How to replace the application for instrumentation tests

You can replace the application class for the instrumentation tests by overriding the AndroidJUnitRunner and its newApplication method.

Источник

Android studio run all tests

Copy raw contents

Android Testing Blueprint

A collection of Google’s Android testing tools and frameworks, all integrated in a single application project.

  • app/ — Main application
  • app/test/ — Unit tests
  • app/androidTest/ — Instrumentation tests
  • app/androidTestFlavor2/ — Additional Instrumentation tests for Flavor2.
  • module-android-library/ — An Android module (typically a library)
  • module-android-library/androidTest — Android Tests for the module-android-library
  • module-flavor1-androidTest-only/ — Test-only module with Instrumentation tests for Flavor1
  • module-plain-java/ — A Java module for non-Android code (business logic, utils classes, etc.)
  • module-plain-java/test — Unit tests for module-plain-java

Running Instrumentation Tests

In Android Studio

  • In the Build Variants window, make sure the Android Instrumentation Tests option is selected.
  • Open a Instrumentation test class like EspressoTest.java or UiAutomatorTest.java
  • Right click on the class and Run as Android Test.

From command-line via Gradle

  • To run all the Instrumentation tests in the app module execute:

From command-line via adb

  • Install the app and the test app on the device. For example:
  • To run the Instrumentation tests in the app module execute:

See the Modules, Flavors and custom Gradle command-line arguments sections to learn how to execute tests for specific modules or flavors and pass custom arguments to AndroidJUnitRunner.

Running Unit Tests

In Android Studio:

  • In the Build Variants window, make sure the Unit Tests option is selected.
  • Open a unit/local test unit class like LocalUnitTest.java .
  • Right click on the class and Run as JUnit test.

From command-line via Gradle:

  • To run all the local unit tests in app execute:
  • To run the local unit tests in a module execute:
  • To filter local unit tests to run, you can use:

In general, in a project with no flavors:

Note that you cannot use ./gradlew :app:test or ./gradlew test with a filter. Read the gradle documentation on test filtering for more information.

Testing frameworks and APIs

Espresso is a part of the ATSL (Android Testing Support Library) and a framework for writing concise, beautiful, and reliable Android UI tests.

The core API is small, predictable, and easy to learn API which. Espresso enables testing of state expectations, interactions, and assertions clearly without the distraction of boilerplate content, custom infrastructure, or messy implementation details getting in the way.

Espresso tests run optimally fast! Leave your waits, syncs, sleeps, and polls behind and let Espresso gracefully manipulate and assert on the application UI when it is at rest.

To get started with Espresso, refer to our EspressoBasicSample

Espresso Contrib is an extension to Espresso which contains support for the most commonly used Android widgets and support library extension, for instance, RecyclerView.

Espresso Intents is a great way to do hermetic inter app testing. It works essentially like Mockito and allows for Intent verification and stubbing. To learn more, please refer to our Espresso Intents samples: IntentsBasicSample IntentsAdvancedSample

Espresso-web allows you to seamlessly test WebViews on Android. It uses the popular WebDriver API to introspect into, and control, the behavior of a WebView. To learn more, please refer to our Espresso Web sample: Espresso Intents samples: WebBasicSample

UI Automator testing framework provides a set of APIs to build UI tests that perform interactions on user apps and system apps. The UI Automator APIs allow you to perform operations such as opening the Settings menu or the app launcher in a test device. The UI Automator testing framework is well-suited for writing black box-style automated tests, where the test code does not rely on internal implementation details of the target app.

To learn more about UIAutomator refer to the BasicSample

Читайте также:  Starwind пульт для андроид

Android Test-Only Module

When developing your application, it is sometimes easier to split production and test code into separate modules.

The structure of such a test-only module is very similar to developing a vanilla application. Instead of putting your instrumentation tests into the androidTest source set, put all test code into the main source set ( src/main/java ) and add an AndroidManifest.xml ( src/main ).

A test only module cannot have a test APK itself so it cannot have a src/androidTest folder. Also, there are no variants for test modules so if you have different flavors in your main application, you need a different test module for each variant you want to test.

Note: Only Instrumentation-based tests are allowed in a test-only module.

Running test-only module tests

From Android Studio

Run normally (see above).

From command-line via Gradle

From command-line via adb

You need to install the app and test app first:

Execute the adb command:

Plain Java module

Moving code away from the Android framework is a good practice as it guarantees testability and relaxes coupling.

The module provided in module-plain-java has a class in module-plain-java/src/main and its own unit tests in module-plain-java/src/test .

Unit tests in a separate java module are run in the same way than unit tests in the app or any other module: run from Android Studio as a JUnit test or use the following gradle command:

This project has two: flavor1 and flavor2 . They simply have different application ID and R.string.app_name and they generate two APKs:

The module-flavor1-androidTest-only only targets flavor1 via the targetVariant in its build.gradle file.

The tests in app/androidTestFlavor2 only target flavor2.

The rest of the tests in app/androidTest will be executed once per flavor one a connected Android Test is executed.

In Android Studio, code coverage for unit tests (executed as JUnit tests) is shown in the editor, next to each line. Run your tests using the Run test with code coverage option. JaCoCo test reports can be generated changing the Coverage runner in the JUnit test configuration.

From the command-line via Gradle, unit tests for the module-plain-java are generated by executing:

A HTML report will be available in module-plain-java/build/reports/jacoco/test/html/index.html

Note: Generating JaCoCo reports from command-line via Gradle for the app module is currently only working for instrumentation but not for unit tests: [https://code.google.com/p/android/issues/detail?id=144664]

Test coverage reports for Android tests can be found in app/build/reports/coverage .

By default tests are run against a non-minified version of your production APK. In most cases, especially during development, this makes a lot of sense because it speeds up your build and therefore improves your cycle times. However it is important to know about breakages as a result of a minified production APK, thus it makes sense to run your tests against a minified version of your APK.

Since Gradle Android Plugin version 1.2.0 using ProGuard has become a lot easier. A new testProguardFile property was added to the DSL which can be used to provide specific ProGuard rules for your test APK. When running ProGuard, Gradle will now use testProguardFile rules and apply it to the test code, but also apply the obfuscation mapping from the production APK run. To try it out just uncomment the minify section for the debug build type in the app modules build.gradle file.

Custom Gradle command-line arguments

To pass a custom argument the -Pcom.android.tools.instrumentationTestRunnerArgs=argName=argValue property needs to be used, in conjunction with argName and argValue . Multiple custom arguments require one property definition per argument.

For instance, to run all tests annotated with the @Large test size qualifier in the app module, execute:

To only run tests for a specific test class, i.e. EspressoTest, execute:

To pass in an arbitrary argument which can be accessed in a test at runtime, execute:

All arguments passed through command line can also be specified in the project’s build.gradle file, which is great for specifying values which are required by the test harness itself. The argument1 from the previous example could also be permanently added using the testInstrumentationRunnerArgument property in the build.gradle file. Look at the app modules build.gradle file for usage.

See AndroidJUnitRunner documentation to learn more about custom command-line arguments.

  • Google+ Community: https://plus.google.com/communities/105153134372062985968
  • Stack Overflow: http://stackoverflow.com/questions/tagged/android-testing

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

Bugs and issues

Issues for this project should only be filed for problems with the actual samples.

For issues against the build tools use b.android.com. Pro-tip: fork this project with a failing test or broken build and include the URL in your bug report. This makes bug description, triaging and debugging much easier.

Copyright 2015 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the «License»); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an «AS IS» BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Источник

Оцените статью