Questions tagged [android-testing]

The Android testing framework

The Android testing framework, an integral part of the development environment, provides an architecture and powerful tools that help you test every aspect of your application at every level from unit to framework.

The testing framework has these key features:

  • Android test suites are based on JUnit. You can use plain JUnit to test a class that doesn't call the Android API, or Android's JUnit extensions to test Android components. If you're new to Android testing, you can start with general-purpose test case classes such as AndroidTestCase and then go on to use more sophisticated classes.
  • The Android JUnit extensions provide component-specific test case classes. These classes provide helper methods for creating mock objects and methods that help you control the lifecycle of a component.
  • Test suites are contained in test packages that are similar to main application packages, so you don't need to learn a new set of tools or techniques for designing and building tests.
  • The SDK tools for building and tests are available in Eclipse with ADT, and also in command-line form for use with other IDEs. These tools get information from the project of the application under test and use this information to automatically create the build files, manifest file, and directory structure for the test package.
  • The SDK also provides monkeyrunner, an API for testing devices with Python programs, and UI/Application Exerciser Monkey, a command-line tool for stress-testing UIs by sending pseudo-random events to a device.
2161 questions
28
votes
8 answers

How to disable the new Autofill feature from Android Oreo for espresso tests

Running tests on android devices with sdk 26 causes them to fail because of the new Autofill feature that hides the fields when espresso are trying to click them. Im running my tests on firebase test lab so I cannot disable them manually on my tests…
28
votes
1 answer

How to obtain coverage for Android project using Espresso tests

I used to write Android tests using Robotium and retrieve the coverage using Emma. Recently I changed to use Espresso tests and I'm having troubles to retrieve coverage of Instrumentation tests. I can only retrieve coverage for Unit tests that use…
28
votes
2 answers

Gradle: How to run instrumentation test for class

I'm running instrumentation test in Android Studio with Run Configuration defined as below (don't mind warning): So this is invoking test suit for a specific class. How can I achieve this with command line, I guess using ./gradlew command ?
Adam Styrc
  • 1,517
  • 2
  • 23
  • 38
28
votes
2 answers

How to run a test method or class as an Android Test in Android Studio

I am using Android Studio 0.2.4, but I assume this question can pertain to IntelliJ in general. My tests are under src/instrumentTest/java and all extend AndroidTestCase. When I run all the tests (say by right clicking on the source folder and…
Nathan Taylor
  • 879
  • 1
  • 9
  • 16
27
votes
3 answers

What do I need to import to use launchActivity<>() in UnitTests?

So I'm trying to test my activity following googles instructions here: https://developer.android.com/guide/components/activities/testing But the code launchActivity() does not work. Do I need to define launchActivity as a rule or is…
Dan Anderson
  • 1,062
  • 13
  • 26
27
votes
2 answers

A way to share code between multiple android test modules

I want to achieve something like this: [ComponentTestsModule] com.android.test [FunctionalTestsModule] com.android.test both depends on -> [TestLibraryModule] ? which depends on -> [AppModule] com.android.application Is there any way to do it…
27
votes
2 answers

What is touch mode and why is it important for the ActivityTestRule class?

The ActivityTestRule class takes in an initialTouchMode parameter in its constructor. This is not explained in the class reference (or anywhere online) except as follows: initialTouchMode - true if the Activity should be placed into "touch mode"…
27
votes
4 answers

How to switch/change testInstrumentationRunner dynamically with gradle

My project has 2 different groups of tests. One group runs only with the default AndroidJUnitRunner the other has to be run with a custom implementation TestRunner extends MonitoringInstrumentation. Currently I switch the testInstrumentationRunner…
thaussma
  • 9,756
  • 5
  • 44
  • 46
27
votes
7 answers

Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

This is my first time setting up an Android test project to test a Android project. I've created a very basic test case which I'm trying to get to run, however what I have does not run. I get a Test run failed: Instrumentation run failed due to…
James Oravec
  • 19,579
  • 27
  • 94
  • 160
26
votes
1 answer

AndroidJUnit4 and Parameterized tests

Google provide new classes to write tests for Android, and especially using jUnit 4: https://developer.android.com/tools/testing-support-library/index.html I was wondering if it is possible to use the AndroidJUnit4 runner, as well as the…
Gaëtan
  • 11,912
  • 7
  • 35
  • 45
25
votes
6 answers

Android Base64 encode and decode return null in Unit Test

I am attempting to decode a Base64 encoded string in Android using the http://developer.android.com/reference/android/util/Base64.html class. Both the encodeToString and decode methods are returning null, and I have no idea what's wrong, here's my…
brnby
  • 1,433
  • 1
  • 19
  • 35
25
votes
2 answers

Android Test Module (Gradle Plugin 1.3) doesn't work: "debug-classes not found"

I'm attempting to set up a unit test module as described in the android studio blog post. However, doing a gradle build fails telling me "Configuration with name 'debug-classes' not found". Debug is the name of the targetVariant it's trying to…
25
votes
7 answers

AndroidTest folder doesn't show on AndroidStudio

I'm setting up Android app structure with Gradle and Android Studio and Espresso UI testing for a project. No matter what I try, the androidTest folder never appears in AndroidStudio's project structure. Project (root) build.gradle: buildscript { …
RecuencoJones
  • 2,747
  • 3
  • 22
  • 21
24
votes
1 answer

Android: Inject fake camera preview for testing purpose

I am writing some black-box, acceptance tests that run on a physical android device. The application under test (AUT) relies on the camera's preview. Specifically, it uses the setOneShotPreviewCallback method of the android.hardware.Camera class. I…
lacton
  • 2,316
  • 2
  • 22
  • 24
24
votes
9 answers

Android Espresso, Wake up device before test. How to use a custom manifest for test?

I've been writing tests with androids new espresso framework and find that it works well. One annoying thing (not particular to espresso) is that I have to make sure my screen is awake and unlocked for the tests to run. I found a workaround (through…
Matt Wolfe
  • 8,924
  • 8
  • 60
  • 77